ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 워드프레스와 GPTs를 연동하여 자동 포스팅 시스템 구축 2-2
    IT-Information/chatGPT 2024. 3. 15. 13:44

    기능 추가 스키마 내용 및 functions.php 변경 내용

    워드프레스와 GPTs를 연동하여 자동 포스팅 시스템 구축 2-2

    더보기

    추가 기능 확장(기능 확장시 wp 설치 폴더에 function.php 파일에 수정이 들어가야합니다.)

    {
      "openapi": "3.0.0",
      "info": {
        "title": "워드프레스 자동 포스팅",
        "description": "워드프레스 OAuth를 사용해 자동으로 글을 생성합니다",
        "version": "v1.0.0"
      },
      "servers": [
        {
          "url": "https://ai.goldsystem.org/"
        }
      ],
      "paths": {
        "/wp-json/wp/v2/posts": {
          "get": {
            "operationId": "getAllPosts",
            "description": "게시물 목록 조회",
            "parameters": [
              {
                "name": "after",
                "in": "query",
                "description": "이 시간 이후에 발행된 게시물만 조회합니다 (형식: YYYY-MM-DDTHH:MM:SS) 전체 글 조회시 비워둡니다",
                "default": "",
                "schema": {
                  "type": "string"
                }
              },
              {
                "name": "before",
                "in": "query",
                "description": "이 시간 이전에 발행된 게시물만 조회합니다 (형식: YYYY-MM-DDTHH:MM:SS) 전체 글 조회시 비워둡니다",
                "default": "",
                "schema": {
                  "type": "string"
                }
              },
              {
                "name": "search",
                "in": "query",
                "description": "이 값이 포함된 게시물에 대한 검색을 실행합니다. 전체 글 조회시 비워둡니다",
                "default": "",
                "schema": {
                  "type": "string"
                }
              },
              {
                "name": "categories",
                "in": "query",
                "description": "카테고리 ID 값으로 이 카테고리에 속하는 게시물만 조회합니다. 전체 글 조회시 비워둡니다",
                "default": "",
                "schema": {
                  "type": "string"
                }
              },
              {
                "name": "post_id",
                "in": "path",
                "description": "조회하고자 하는 게시물의 고유 ID로 /wp-json/wp/v2/posts/{post_id}로 접속하면 해당 post_id 게시물만 조회됩니다. post_id 값이 없 게시물 전체를 조회하게 됩니다. 전체 글 조회시 비워둡니다",
                "required": true,
                "default": "",
                "schema": {
                  "type": "string"
                }
              }
            ]
          },
          "post": {
            "operationId": "createPost",
            "parameters": [
              {
                "name": "post_id",
                "in": "path",
                "required": true,
                "description": "수정하고자 하는 게시물의 ID (이 값이 있으면 해당 ID의 게시물에 대한 수정 모드, 이 값이 0이면 새글쓰기 모드입니다)",
                "schema": {
                  "type": "integer"
                }
              }
            ],
            "requestBody": {
              "required": "true",
              "content": {
                "application/json": {
                  "schema": {
                    "type": "object",
                    "properties": {
                      "categories": {
                        "type": "string",
                        "description": "getAllCategories를 통해 얻은 카테고리 ID (이 값은 필수가 아님. 없어도 등록됨)"
                      },
                      "title": {
                        "type": "string",
                        "description": "글 제목"
                      },
                      "content": {
                        "type": "string",
                        "description": "글 내용"
                      },
                      "status": {
                        "type": "string",
                        "description": "글 상태 ( publish (즉시 발행), pending (보류 또는 대기), future (date_gmt 시간에 예약 발행) )"
                      },
                      "date_gmt": {
                        "type": "string",
                        "description": "status가 future인 경우, 이 값의 시간에 예약 발행됨 (GMT 형식의 시간) "
                      }
                    }
                  }
                }
              }
            }
          }
        },
        "/wp-json/wp/v2/categories": {
          "get": {
            "operationId": "getAllCategories",
            "description": "모든 카테고리 조회 (이것을 통해 /wp-json/wp/v2/post의 categories에 들어갈 카테고리 ID 목록을 얻을 수 있습니다)",
            "parameters": [
              {
                "name": "search",
                "in": "query",
                "description": "이 값은 특정 키워드로, 카테고리를 검색하고자 할 때 사용합니다",
                "default": "100",
                "schema": {
                  "type": "string"
                }
              }
            ]
          }
        },
        "/wp-json/custom/append-post-content": {
          "post": {
            "operationId": "appendContents",
            "requestBody": {
              "required": "true",
              "content": {
                "application/json": {
                  "schema": {
                    "type": "object",
                    "properties": {
                      "post_id": {
                        "type": "string",
                        "description": "내용을 추가하고자 하는 게시물의 ID"
                      },
                      "content": {
                        "type": "string",
                        "description": "기존 내용에 추가하고자 하는 내용"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }

     

    php 추가 코드

    //아래 내용은 functions.php 마지막 부분에 추가
    // functions.php 내용 수정은 반드시 백업 후 진행해야 복원 가능합니다. 
    // functions.php 위치 ->  /wp-content/themes/사용중테마/funcions.php
    
    function append_post_content( $request ) {
        // post_id를 POST 요청 본문으로부터 받음
        $post_id = $request['post_id'];
        $append_content = $request['content'];
    
        // 게시물을 조회
        $post = get_post( $post_id );
    
        if ( !$post ) {
            return new WP_Error( 'not_found', 'Post not found', array( 'status' => 404 ) );
        }
    
        // 기존 내용에 추가
        $new_content = $post->post_content . $append_content;
    
        // 게시물 업데이트
        wp_update_post( array(
            'ID'           => $post_id,
            'post_content' => $new_content
        ) );
    
        return new WP_REST_Response( array( 'status' => 'success', 'post_id' => $post_id ), 200 );
    }
    
    
    
    // 커스텀 엔드포인트 등록  예: https://ai.goldsystem.org/wp-json/custom/append-post-content
    add_action( 'rest_api_init', function () {
        register_rest_route( 'custom', '/append-post-content', array(
            'methods' => 'POST',
            'callback' => 'append_post_content',
            'args' => array(
                'post_id' => array(
                    'required' => true,
                    'validate_callback' => function($param, $request, $key) {
                        return is_numeric( $param );
                    }
                ),
                'content' => array(
                    'required' => true,
                    'validate_callback' => function($param, $request, $key) {
                        return is_string( $param );
                    }
                ),
            ),
        ) );
    } );

    GPTs에서 프롬프트 작성

    글 생성시 html 변환하여 색깔을 변경하고 스타일을 예쁘게 바꿔 줄래?

    ~~ 글을 올려줘. 보류 상태로 올려줘

    좋은 내용 발견하면 즉시 올리기가 가능해집니다.

     

    API 확장

    포스트 및 미디어 관리 API 확장

    워드프레스와 GPTs의 연동을 위해서는 포스트 및 미디어 관리 기능을 확장할 필요가 있습니다. 이를 위해 functions.php 파일에 다음과 같은 함수를 추가하여, 게시물 내용에 자동으로 추가 내용을 삽입하는 기능을 구현할 수 있습니다.

    function append_post_content($request) {
        // post_id를 POST 요청 본문으로부터 받음
        $post_id = $request['post_id'];
        $append_content = $request['content'];
    
        // 게시물을 조회
        $post = get_post($post_id);
    
        if (!$post) {
            return new WP_Error('not_found', 'Post not found', array('status' => 404));
        }
    
        // 기존 내용에 추가
        $new_content = $post->post_content . $append_content;
    
        // 게시물 업데이트
        wp_update_post(array(
            'ID' => $post_id,
            'post_content' => $new_content
        ));
    
        return new WP_REST_Response(array('status' => 'success', 'post_id' => $post_id), 200);
    }
    
    add_action('rest_api_init', function () {
        register_rest_route('custom', '/append-post-content', array(
            'methods' => 'POST',
            'callback' => 'append_post_content',
            'args' => array(
                'post_id' => array(
                    'required' => true,
                    'validate_callback' => function($param, $request, $key) {
                        return is_numeric($param);
                    }
                ),
                'content' => array(
                    'required' => true,
                    'validate_callback' => function($param, $request, $key) {
                        return is_string($param);
                    }
                ),
            ),
        ));
    });

     

    이 함수는 특정 포스트 ID에 대한 요청을 받아, 해당 포스트의 내용에 새로운 내용을 추가하고 결과를 반환합니다.

     

    이 기능은 GPTs와 같은 AI 기반 콘텐츠 생성 도구와 연동하여 자동으로 콘텐츠를 업데이트하거나 확장하는 데 유용하게 사용될 수 있습니다.

     

    다음에는

    Google Docs를 생성했을 때 Google Sheets에 문서 정보 가져와 목록화 하는 방법에 대해서 알아보려 합니다.

    Pretty Button 알아보러 가기

     

Designed by Tistory.