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.

6 Likes

2 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”
    }