For those who want to connect WordPress to GPTs:

This guide simplifies the process, covering JWT Authentication setup, GPT actions, schema… and key configurations.

1- Install the JWT Authentication Plugin

  • Download and install from: JWT Authentication for WP REST API
  • Access your WordPress site’s root directory using FTP.
  • Open and edit the .htaccess file to enable PHP HTTP Authorization Header. Add the following lines:
RewriteEngine on
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]

Save the .htaccess file. (Note: Ensure your hosting allows .htaccess file modifications.)

2-Configure the Secret Key

  • Open and edit wp-config.php.
  • Add a new constant called JWT_AUTH_SECRET_KEY:

define('JWT_AUTH_SECRET_KEY', 'your-top-secret-key');

Replace 'your-top-secret-key' with a unique secret key, you can use a string from here.

3-Configure CORS Support
In wp-config.php, add a new constant called JWT_AUTH_CORS_ENABLE:

define('JWT_AUTH_CORS_ENABLE', true);

Save wp-config.php .

4-Testing with Postman or HTTPie

Submit a POST request to the following endpoint (no authentication, only JSON body):

https://yourwordpress.com/wp-json/jwt-auth/v1/token

JSON body:

{
  "username": "your-username",
  "password": "your-password"
}

Copy and save your token.

Now, submit a POST request to this endpoint:
wp-json/wp/v2/posts/

Header:
Authorization: Bearer {token}

good!

now, Create a new action in your GPT configuration, and use the API schema below ( don’t forget to edit your server URL. )

{
  "openapi": "3.1.0",
  "info": {
    "title": "WordPress API",
    "description": "API for creating and editing posts in WordPress",
    "version": "1.0.0"
  },
  "servers": [
    {
      "url": "https://yourwordpress.com/wp-json"
    }
  ],
  "paths": {
    "/wp/v2/posts": {
      "post": {
        "summary": "Create a new post",
        "operationId": "createPost",
        "tags": ["Posts"],
        "requestBody": {
          "description": "Post data",
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/Post"
              }
            }
          },
          "required": true
        },
        "responses": {
          "201": {
            "description": "Post created",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Post"
                }
              }
            }
          }
        }
      }
    },
    "/wp/v2/posts/{id}": {
      "put": {
        "summary": "Edit an existing post",
        "operationId": "editPost",
        "tags": ["Posts"],
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "integer"
            },
            "description": "The ID of the post to edit"
          }
        ],
        "requestBody": {
          "description": "Updated post data",
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/Post"
              }
            }
          },
          "required": true
        },
        "responses": {
          "200": {
            "description": "Post updated",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Post"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "Post": {
        "type": "object",
        "properties": {
          "title": {
            "type": "string"
          },
          "content": {
            "type": "string"
          },
          "status": {
            "type": "string"
          }
        }
      }
    }
  }
}

Change the Authentication type to Bearer Token and paste your saved token.

ScreenShot_20231121021834

Now ,Test the integration by sending a request through chat or click here

that’s it!
for any problems or feedback, don’t hesitate to share.

7 Likes

3 Likes

Great write-up. I’m sure many will find this useful. Thanks for sharing with us.

ETA: Probably not too much different to set-up on an NGINX box… other than the .htaccess

2 Likes
  • GPT’s JSON request has a token limit of 1000 tokens.

I think I need something like this for Joomla 4 because I cannot get my module to connect to the api or assistant ID

This seems like a way to access WP API from GPTs, but I’m curious how difficult it would be to go other way around - create WP Plugin for custom GPT? There are plugins for generic GPT, but what about custom (public or not) GPTs?

if you want to create WP plugin so you don’t need to GPT’s
just use OpenAI API
GPT’s is more easier way

but what about custom (public or not) GPTs?

i didn’t understand

There are WP plugins to embed ChatGPT functionality into your web site, like Chatbot ChatGPT for WordPress – WordPress plugin | WordPress.org but if you need to access custom GPTs you’re out of luck.

This was completely new to me so I didn’t understand and hit a couple of hurdles;

  1. I was using 2FA on the wp username / password I was using so realised I needed to create a different account just for this. I used the Author role.

  2. I’ve never posted this way through an API or postman so I didn’t understand that in the 2nd post variation I needed to edit the body to include the content of the post. I ended up using this:
    {
    “title”: “hello”,
    “content”: “for test”,
    “status”: “publish”
    }

Hi, this guide is for wordpress[doc]com or wordpress[doc]org? Because i have a site based on wordpress org and i would like to embed my GPT in my site…do you know how? Thank you

Welcome to the forum.

If you read the thread above, you’ll see there’s mention of several WP Plug-ins… which I’m pretty sure you can use on your own domain or via Wordpress.org

I can’t find the instructions of " open custom gpt" on githup to embed a custom gpt on a wordpress site…can you explain step by step to do this? Thank you

It’s not a simple task as you can’t specifically embed a Custom GPT. They’re only available to ChatGPT Plus members. However, with a bit of grunt work, you can mostly replicate a Custom GPT (from the ground-up) using the Assistants API.

I m a gpt plus member…but to see this on github its needs to sign up? Because as guest maybe i can’t find any instructions to embed a custom gpt on your own site

You might want to try WP GPT Poster - Post SEO Content To Your WP. I am unable to embed link (yet) but you can try to search it in the GPT Store. You will need to install the plugin (Just ask the GPT where to download). I have made the steps very simple (I hope) and wish that it will be useful for everyone.

I managed to publish and edit. Thanks for the tutorial.

I’m not a programmer. Any tips on how to LIST posts?

PROMPT example: ‘List 10 posts published in 2023. List ‘title’ and ‘ID’

I tried including the code below but it’s not working.

paths:
  /wp/v2/posts:
    get:
      summary: List all posts
      operationId: listPosts
      tags:
        - Posts
      parameters:
        - name: page
          in: query
          description: Page number for paginating through results
          required: false
          schema:
            type: integer
        - name: per_page
          in: query
          description: Number of posts to return per page
          required: false
          schema:
            type: integer
        - name: search
          in: query
          description: Keyword to search in posts
          required: false
          schema:
            type: string
      responses:
        '200':
          description: A list of posts
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Post'
        default:
          description: Unexpected error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'