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.

9 Likes

4 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

3 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

1 Like

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

1 Like

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

1 Like

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

1 Like

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.

1 Like

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'

I’m following these steps exactly, and all works correctly through the final HTTPie testing step - submitting a POST request to my endpoint results in a successful post. However, when I try the next step of testing within GPT it always results in a client response error. Does the failure being at that point with all previous steps working correctly narrow down the possible issues?

Thanks for this guide! A couple of clarifications please:
1- Does this token remain static or does it need to change over time? Edit: looking at the plugin documentation it seems tokens go stale after 10 minutes, so how would we keep updating it in the Custom GPT?
2- Once this token is generated, is it safe to remove the plugin and revert the changes?
3- Does HTTP_AUTHORIZATION also cover HTTPS or is there an alternative, secure version?

1 Like