With all the focus justifiably being on AI, and after hearing that ChatGPT (Chat Generative Pre-Trained Transformer) could generate WordPress code, it was time to give it a try. Not being a developer in the truest sense yet possessing strong HTML and CSS skills I decided to let ChatGPT spin up my own custom plugin.
With just enough knowledge of how WordPress uses PHP — with its action hooks, filters and functions — I set out to create a plugin for a custom post type that would have one category-like taxonomy that included three terms.
I used ChatGPT because it was what I was most familiar with having used it to help me create content for a demonstration website.
This article highlights key concerns and tips for using AI to create custom post types. This is not intended to be legal advice; rather, by being aware of these issues, we can use AI more effectively and responsibly. This content is for informational purposes only; it does not represent any affiliation, endorsement or sponsorship with ChatGPT, Open AI or any other third parties listed.
What did I want to build?
My goal was to create a custom post type for Eeents. I’d have it work like a blog post with a single taxonomy that would work like a category that I called “Types.” And while I could add terms after the plugin was installed and activated I decided to add “In-Person”, “Live” and “Hybrid” as my pre-defined terms.
Why not purchase a theme that has events as a built-in feature?
Surely, I could. There are plenty of themes built with event pages in mind. The problem with that would arise should I ever change the theme. The event's content would get lost with the new theme.
There are many free and premium plugins that will do what I want. Why go through the trouble?
I wanted to have the cleanest, simplest code possible to accomplish my goal. Relying upon code I don't understand or would be overkill was a non-starter.
I decided to do it myself with AI assistance.
What are the advantages of using AI?
Even before I began I deduced a couple reasons why AI might be a good reason to help me:
- This would speed up the process since my coding skills are not sufficient by themselves to create my custom plugin. It might take me hours — or days — to do what would take just a few minutes.
- ChatGPT would probably turn out to be not just a great assistant. As I was about to discover, it is a great teacher, because I could ask it questions about the code it would generate.
Starting the conversation with ChatGPT
It is called "Chat" for good reason. Understanding how to use its now famous prompt (text input box) is the key to successful outcomes. If you are new to using it try to have a conversation with it first by asking follow-up questions to your initial query.
Once you see how it works you can then try it with code and use follow-up questions (additional prompts) to iterate on the generated code.
My goal was to create a custom post type — Events — as mentioned, that included one taxonomy called "Types." The three types — known as terms in WordPress speak — would be "In-person", "Live" and "Hybrid."
As you'll see it was not smooth sailing but I did eventually find my safe harbor.
Step 1: Where I started
Me: Create a WordPress post type for Events.
ChatGPT: To create a custom post type for events in WordPress, you'll need to add some code
...and on it went giving clear instructions that I could add the code it was generating to the functions.php file, which it correctly advised against, or I could create a plugin with this code:
It went on to explain how to save the code to a unique file within an aptly named folder and then upload that to the /plugins folder. And, lastly, to activate the plugin.
The resulting code added this to my WP Admin:
However, the code did not provide the block editor for the Events pages. I decided to look into that later.
And what about my taxonomy with its three terms?
Step 2: Making my first change
Me: Now, add three taxonomies that behave like categories. They are "in-person", “live” and "online"
ChatGPT: Sure, I'll modify the custom plugin code to include three taxonomies, "in-person", “live” and "online," which will behave like categories for the "Events" custom post type:
I saw that ChatGPT was oblivious to my typos (which was a good thing).
Here is the revised code:
In this case, ChatGPT didn’t need to repeat the $labels and $args. It left comments for me to copy and paste into the revised code.
But I discovered I made a serious error when I saw this:
My custom taxonomy was to be “Live” with three terms. The three terms — In-Person, Online and Hybrid — were mistakenly added as three taxonomies.
At this point, I wasn’t sure what to do, so I decided to go a different way, knowing it probably would not solve my problem, but it would satisfy my curiosity.
Step 3: Asking for suggestions
Me: What are some suggestions that I can do to edit this code?
ChatGPT: Certainly, here are some suggestions to enhance and improve the code for creating the "Events" custom post type and the "In-Person", "Online" and "HybridHybrd" taxonomies:
I got a list of 11 suggestions. Some of them looked like things that were already generated in some form such as Code Organization, Comments and Documentation. It also mentioned sanitizing the code which I had not thought of.
Step 4: Time to get the code right
Me: Let's start over. Let's create a custom post type for a WordPress website. The name ofnameof the post type is "Event." It will behave like a blog post. And it will have one taxonomy that will work like a category which will be called "Type." This taxonomytaxonomuy will have 3 terms, "In-Person", "Live" and "Hybrid."
ChatGTP: Of course, let's create a custom post type called "Event" with a single taxonomy named "Type" containing the terms "In-Person," "Live," and "Hybrid." We'll organize the code in a plugin for better modularity and to avoid losing data when changing themes. Here's how you can do it:
This is what ChatGPT generated:
Interesting. Even though it gave me the impression that it would include the three terms, it did not. I got closer to my goal but was still not there.
Step 5: One more iteration to get it right
Me: Is it possible to add "terms" to the taxonomy in the above code?
ChatGPT: Yes, of course! You can add terms to the "Type" taxonomy in the above code. Here's how you can modify the code to add the terms "In-Person," "Live," and "Hybrid" to the "Type" taxonomy:
But here’s where things got a little sticky once I added that to my plugin.
The code threw an error finding an unexpected “}”. Going back to my HTML days, I figured there was a “}” to be removed. I resolved the issue while realizing…
Step 6: Ask ChatGPT to do code validation
Me: Please correct the error in this code:
(I pasted the plugin code into the prompt)
ChatGPT: It looks like you have an extra closing curly brace (}) after registering the taxonomy. Here's the corrected code:
Why this was not done in Step 4 was a reminder that AI which generates code is in its infancy and that a rough sea is to be expected.
At last, Types had its three terms.
Is that all there is?
ChatGPT comes packed with helper tools located in the upper right-hand corner which I had not seen at first glance.
When it came time to get more information about my conversation there were options to clarify, expand, explain, among others. Its ability to teach did not disappoint.
When I asked for clarification, ChatGPT correctly stated that the functions to create the Events post type and Types taxonomy were as follows:
...a custom function called event_custom_post_type to register a new custom post type named "Events." The function uses labels and arguments to specify how the "Events" post type should behave and appear in the WordPress admin.
As for the Types:
... another custom function called event_custom_taxonomy to register a custom taxonomy named "Types." The function specifies the taxonomy labels and arguments to define the behavior and appearance of the "Type" taxonomy.
As for the Terms:
...adds terms ('In-Person,' 'Live,' and 'Hybrid') to the "Type" taxonomy. Terms allow you to classify content within the taxonomy.
What about the block editor?
Since the generated code did not enable the Gutenberg Block Editor I had to figure that one out myself.
ChatGPT reported that I didn’t need to add any code as Gutenberg is the default editor and so long as I was using a current version of WordPress there was nothing to do.
This was incorrect as the code rendered the Classic Editor.
After a little old-fashioned Googling, I discovered this was required in the Events $args:
Again, ChatGPT needed my assistance.
A few tips when conversing with ChatGPT
As I went about creating my first generated WordPress plugin I learned a few things:
- Be specific and have a goal in mind.
- Talk to ChatGPT as you would a developer.
- Refine the code with additional prompts.
- Ask for clarification if needed
- Don’t worry about grammar and spelling
A powerful tool with shortcomings
As alluring as ChatGPT is to spin up a simple WordPress plugin it sure can’t do everything. Here are a few shortcomings I discovered.
- While I didn't try it, my understanding is that it can't (yet) create complex WordPress code.
- Expect the unexpected. I saw that during iterations.
- Be careful if you are not a developer like me. Is the code safe (sanitized), is it efficient, and does it meet WordPress coding standards? All this should be requested during your session.
- Once you have your plugin, it may become vulnerable over time due to changes in WordPress core. There's no way to check on that with certainty for now.
Remember ChatGPT is essentially assembling the code from what it has in its "memory." If it has learned less than optimal code that is what it will generate.
This is not a complete list. There are many other shortcomings if and when you use ChatGPT APIs.
Other open-source AIs
While ChatGPT is the most well-known text-generating AI and probably the easiest to use — it is not alone.
There are alternatives to explore but note that the term “open source Ai” is a misnomer as these tools are being developed by large companies and exactly how they work is a mystery. However, this is changing led by Meta AI with its release of a downloadable AI known as Llama2.
Here is how it works.
Meta AI's downloadable large language model
Here is how it works.
The online version of LLama2. Consider to be a competitor to ChatGPT.
Microsoft Bing has made some of its search engine indexing algorithm and various components open source. However, the search engine itself is not open source.
Resources to help you build your own post type