Using GPTScript to Build a WordPress Assistant

Managing the content from the WordPress dashboard is usually a breeze. But have you ever wanted to get help with managing this? The steps outlined below will walk through how to get set up.

For additional reference I've created this video guide below:

Let's set up a GPTScript that can add comments for you, publish content or anything else that's available through the WordPress API.

Setting up GPTScript

We can begin with setting up GPTScript on your computer. The version I used for this tutorial is version 0.8.

A short guide:

  1. First you install the CLI (Command line interface) by running this command:
MacOS / Linux: brew install gptscript-ai/tap/gptscript Windows: winget install gptscript-ai.gptscript
  1. Get your OpenAI API key by going here (https://platform.openai.com/api-keys)

  2. Add the API key to your environment by running this command:

MacOS / Linux: export OPENAI_API_KEY="your-api-key” Windows: $env:OPENAI_API_KEY = 'your-api-key'

And now you should be ready to jump to the next step. If you need more details for the installation, you can go to the official getting started guide for GPTScript by clicking here (https://docs.gptscript.ai/getting-started).

Prices

Before you start running the commands for running GPTScript, it might be smart to take a look at the price lists here: https://openai.com/pricing

Using OpenAI’s API is not free, but you do not pay for more than you actually use.

The script we are building in this tutorial will use a language model called gpt-4-o. This price can vary a little bit based on what you actually are sending in to the GPTscript, how long the texts are, etc.

Take a good look at the price lists to familiarize your self with them.

Setting up WordPress

Now that we have GPTScript running, we can jump to the next step which is setting up a WordPress site. I am not going to go through this in this article since this is covered many places.

For example, you can go through this step by step guide: https://www.digitalocean.com/community/tutorials/how-to-install-wordpress-with-docker-compose

When you're finished with this tutorial, you should have a site running with SSL (This is important for the API to be working).

To confirm that your API is working go to https://example.com/wp-json. Just replace example.com with your domain or IP-address.

Application password

To interact with the API, we need an application password. You can create one under "your profile" in the dashboard.

You can read the official guide from WordPress here: https://make.wordpress.org/core/2020/11/05/application-passwords-integration-guide/

Creating a GPTScript

Okay, now we should be ready to start building the GPTScript.

Somewhere on your computer, create a folder called "wp-assistant" and a file inside there called "wp-assistant.gpt".

Feel free to set the name to something else, this is just I wanted to use for this tutorial.

Open up the folder in your favorite editor, I like to use Visual Studio Code.

Add the following code to the wp-assistant.gpt" file (I will go through the code below the block):

tools: sys.exec description: An assistant for managing wordpress sites. chat: true The WordPress API is located here: https://wpgptscript.codewithstein.com/wp-json. The username is "wpgptscript", the password for the user is "RfvBhy321" and the application password is "6E18 esSW CD2e Gpud G5SQ 24id". Ask the user what you can help with. Execute the commands the user gives you using cURL.

This is actually the whole script. No more than 6 lines of easy to understand "script". Let's go through it.

  1. First, we set the tools that this script should use. In our case, we only need "sys.exec". This makes it possible for GPTScript to run cURL commands on your behalf.
  2. Next, we set up a description. This is just to make it easier to understand what this script does.
  3. chat: true, is a new feature in GPTScript. This converts the script into a chat bot. What this means is that you can actually "chat" with your wordpress site.
  4. Below the configuration, we give the script some information like where the API is located.
  5. Next, we need to give the script our username, our password and an application password.
  6. At the end, we just instruct the script what to do. The script will now ask the user what it want to do, and then execute it with cURL.

Creating new posts

The bot is now capable of doing a lot of cool things like creating posts, pages, answering comments, etc. Let's go over an example where we instruct it to create a new post.

Go to the command line and run the following command:

gptscript wp-assistant.gpt

You will be greeted with a window looking similar to this:

greeting.png

Try asking the following command:

Can you create a new post for me? Set the title to be "This is a test", the content can be "This is just a test, bla bla bla", and the status to "publish".

The bot might ask if it is okay to run cURL commands on your behalf. Just type "a" to confirm this.

You will see that the bot now will talk to your WordPress API, and after a few seconds you'll get a confirmation that the post has been created.

Check out your dashboard posts list:

post created.png

Let's try one more time where we'll use the bot to generate the content for us.

Try asking the following command:

Can you create one more post for me? The title can be "This is generated by GPTScript", set the status to "publish". Based on the title, can you generate a 100 word article and set that as the content?

If you now go to the dashboard again, you will see a little bit of what the bot is capable of.

post detail.png

Working with comments

Since we now can create posts, maybe we should try to do something with a comment. Run the following command to get all comments in your system:

Can you get all comments?

This will list out all of your comments with ID, timestamp and content. If you just have the single demo comment that comes with WordPress, you can interact with it like this:

Can you add an answer to the comment with ID = 1? Feel free to come up with a happy answer.

And in response, we get something like this (Brilliant):

comment.png

Creating pages

Not only can you create posts and comments, but you can also create pages and add them to the menu. Let's create a very basic about page by running this command:

can you create a page for me?

You will then be asked to provide a title, content and a status. You can give it something like this:

create page.png

Great, let's go to the browser to see the result.

about.png

And there you have it, feel free to ask the chat bot to generate content for you or add something more manually.

Inserting images from the web

Let's say that we want the chat bot to find an image for us on Google and insert it. First, we need to add one more tool to the script:

tools: sys.exec, sys.http.get description: An assistant for managing wordpress sites. chat: true The WordPress API is located here: https://wpgptscript.codewithstein.com/wp-json. The username is "wpgptscript", the password for the user is "wpgswrcript" and the application password is "TN2d 3GyB Hvtt S5Vn i57X We0M". Ask the user what you can help with. Execute the commands the user gives you using curl.

If you now save it and go back to the command line, you can ask it to find an image by running a command similar to this:

can you go to Google and find an image of a man? Get the url and insert it directly to the about page.

The result will be something like this:

result.png

If you want to take it up a notch, you can modify the script a little bit more so that it actually downloads the image from the web, uploads it to the wordpress media and inserts it on a page.

tools: sys.exec, sys.http.get, sys.write, sys.download description: An assistant for managing wordpress sites. chat: true The WordPress API is located here: https://wpgptscript.codewithstein.com/wp-json. The username is "wpgptscript", the password for the user is "wpgptscript" and the application password is "EN2d 3GyB Hv81 S5Vn o52X r10M". Ask the user what you can help with. Execute the commands the user gives you using curl.

Notice that the only difference is that we added "sys.write" and "sys.download" to make it possible to download and store files locally. The rest is automatically taken care of by the bot.

Summary

You should now have a chat bot that makes it possible to talk with your WordPress site. You can make this script more "generic", by instead of providing the url and credentials, you can make the chat bot ask you to provide this in the terminal.