SkillsCategory

Using OpenAI to create a custom post type 

16 min read
Bud Kraus
Image credit: GoDaddy Studio

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.  

Disclaimer: When using AI tools, avoid entering sensitive information and always review output for accuracy. Additionally, this content is for demonstration purposes only; it does not represent any affiliation, endorsement, or sponsorship with ChatGPT or Open AI. All trademark rights belong to their respective owners. Third-party trademarks are used here for demonstrative and educational purposes only; use does not represent affiliation.

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: 

  1. 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. 
  2. 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:  

<?php 
/* 
Plugin Name: Custom Events Plugin 
Description: Adds a custom post type for events. 
*/ 

// Register Custom Post Type 
function custom_events_post_type() { 
    $labels = array( 
        'name'                  => 'Events', 
        'singular_name'         => 'Event', 
        'menu_name'             => 'Events', 
        'all_items'             => 'All Events', 
        'add_new'               => 'Add New', 
        'add_new_item'          => 'Add New Event', 
        'edit_item'             => 'Edit Event', 
        'new_item'              => 'New Event', 
        'view_item'             => 'View Event', 
        'search_items'          => 'Search Events', 
        'not_found'             => 'No events found', 
        'not_found_in_trash'    => 'No events found in Trash', 
        'parent_item_colon'     => 'Parent Event:', 
        'menu_icon'             => 'dashicons-calendar', // You can choose an icon from Dashicons or upload your own 
    );   

    $args = array( 
        'labels'            => $labels, 
        'public'            => true, 
        'has_archive'       => true, 
        'publicly_queryable'=> true, 
        'rewrite'           => array('slug' => 'events'), 
        'supports'          => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields'), 
    );   

    register_post_type('event', $args); 
} 

add_action('init', 'custom_events_post_type'); 

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: 

<?php 
/* 
Plugin Name: Custom Events Plugin 
Description: Adds a custom post type for events with "in-person" and "online" taxonomies. 
*/   

// Register Custom Post Type 
function custom_events_post_type() { 
    $labels = array( 
        // (same labels as before) 
    );   

    $args = array( 
        // (same args as before) 
    ); 
  
    register_post_type('event', $args); 
} 
add_action('init', 'custom_events_post_type');   

// Register Custom Taxonomies 
  
function custom_event_taxonomies() { 
    // Taxonomy for "in-person" 
    $labels_in_person = array( 
        'name'              => 'In-Person', 
        'singular_name'     => 'In-Person', 
        'search_items'      => 'Search In-Person', 
        'all_items'         => 'All In-Person', 
        'parent_item'       => 'Parent In-Person', 
        'parent_item_colon' => 'Parent In-Person:', 
        'edit_item'         => 'Edit In-Person', 
        'update_item'       => 'Update In-Person', 
        'add_new_item'      => 'Add New In-Person', 
        'new_item_name'     => 'New In-Person Name', 
        'menu_name'         => 'In-Person', 
    );   

    $args_in_person = array( 
        'hierarchical'      => true, // Behaves like categories 
        'labels'            => $labels_in_person, 
        'show_ui'           => true, 
        'show_admin_column' => true, 
        'query_var'         => true, 
        'rewrite'           => array('slug' => 'in-person'), 
    ); 
 
    register_taxonomy('event_in_person', 'event', $args_in_person);   

    // Taxonomy for "online" 
    $labels_online = array( 
        'name'              => 'Online', 
        'singular_name'     => 'Online', 
        'search_items'      => 'Search Online', 
        'all_items'         => 'All Online', 
        'parent_item'       => 'Parent Online', 
        'parent_item_colon' => 'Parent Online:', 
        'edit_item'         => 'Edit Online', 
        'update_item'       => 'Update Online', 
        'add_new_item'      => 'Add New Online', 
        'new_item_name'     => 'New Online Name', 
        'menu_name'         => 'Online', 
    ); 
  
    $args_online = array( 
        'hierarchical'      => true, // Behaves like categories 
        'labels'            => $labels_online, 
        'show_ui'           => true, 
        'show_admin_column' => true, 
        'query_var'         => true, 
        'rewrite'           => array('slug' => 'online'), 
    );   

    register_taxonomy('event_online', 'event', $args_online); 
  
    register_taxonomy('event_in_person', 'event', $args_in_person);   

    // Taxonomy for "hybrid" 
    $labels_hybrid = array( 
        'name'              => 'Hybrid', 
        'singular_name'     => 'Hybrid', 
        'search_items'      => 'Search Hybrid', 
        'all_items'         => 'All Hybrid', 
        'parent_item'       => 'Parent Hybrid', 
        'parent_item_colon' => 'Parent Hybrid:', 
        'edit_item'         => 'Edit Hybrid', 
        'update_item'       => 'Update Hybrid', 
        'add_new_item'      => 'Add New Hybrid', 
        'new_item_name'     => 'New Hybrid Name', 
        'menu_name'         => 'Hybrid', 

    );   

    $args_hybrid = array( 
        'hierarchical'      => true, // Behaves like categories 
        'labels'            => $labels_hybrid, 
        'show_ui'           => true, 
        'show_admin_column' => true, 
        'query_var'         => true, 
        'rewrite'           => array('slug' => 'hybrid'), 
    );   

    register_taxonomy('event_hybrid', 'event', $args_hybrid); 
} 
  
add_action('init', 'custom_event_taxonomies'); 

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: 

<?php 
/* 
Plugin Name: Custom Events Plugin 
Description: Adds a custom post type for events. 
*/   

// Register Custom Post Type 
function event_custom_post_type() { 
    $labels = array( 
        'name' => 'Events', 
        'singular_name' => 'Event', 
        'menu_name' => 'Events', 
        'all_items' => 'All Events', 
        'add_new' => 'Add New', 
        'add_new_item' => 'Add New Event', 
        'edit_item' => 'Edit Event', 
        'new_item' => 'New Event', 
        'view_item' => 'View Event', 
        'search_items' => 'Search Events', 
        'not_found' => 'No events found', 
        'not_found_in_trash' => 'No events found in Trash', 
        'parent_item_colon' => 'Parent Event:', 
        'menu_icon' => 'dashicons-calendar', // You can choose an icon from Dashicons or upload your own 
    ); 
  
    $args = array( 
        'labels' => $labels, 
        'public' => true, 
        'has_archive' => true, 
        'publicly_queryable' => true, 
        'rewrite' => array('slug' => 'events'), 
        'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields'), 
    ); 
  
    register_post_type('event', $args); 
} 
add_action('init', 'event_custom_post_type'); 
  
// Register Custom Taxonomy 
function event_custom_taxonomy() { 
    $labels = array( 
        'name' => 'Types', 
        'singular_name' => 'Type', 
        'search_items' => 'Search Types', 
        'all_items' => 'All Types', 
        'parent_item' => 'Parent Type', 
        'parent_item_colon' => 'Parent Type:', 
        'edit_item' => 'Edit Type', 
        'update_item' => 'Update Type', 
        'add_new_item' => 'Add New Type', 
        'new_item_name' => 'New Type Name', 
        'menu_name' => 'Type', 
    );   

    $args = array( 
        'hierarchical' => true, // Behaves like categories 
        'labels' => $labels, 
        'show_ui' => true, 
        'show_admin_column' => true, 
        'query_var' => true, 
        'rewrite' => array('slug' => 'event-type'), 
    ); 
  
    register_taxonomy('event_type', 'event', $args); 
} 
add_action('init', 'event_custom_taxonomy'); 

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: 

   // Add terms to the Type taxonomy 
    wp_insert_term('In-Person', 'event_type'); 
    wp_insert_term('Live', 'event_type'); 
    wp_insert_term('Hybrid', 'event_type'); 
} 
add_action('init', 'event_custom_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: 

<?php 
/* 
Plugin Name: Custom Events Plugin 
Description: Adds a custom post type for events. 
*/ 
  
// Register Custom Post Type 
function event_custom_post_type() { 
    $labels = array( 
        'name' => 'Events', 
        'singular_name' => 'Event', 
        'menu_name' => 'Events', 
        'all_items' => 'All Events', 
        'add_new' => 'Add New', 
        'add_new_item' => 'Add New Event', 
        'edit_item' => 'Edit Event', 
        'new_item' => 'New Event', 
        'view_item' => 'View Event', 
        'search_items' => 'Search Events', 
        'not_found' => 'No events found', 
        'not_found_in_trash' => 'No events found in Trash', 
        'parent_item_colon' => 'Parent Event:', 
        'menu_icon' => 'dashicons-calendar', // You can choose an icon from Dashicons or upload your own 
    );   

    $args = array( 
        'labels' => $labels, 
        'public' => true, 
        'has_archive' => true, 
        'publicly_queryable' => true, 
        'rewrite' => array('slug' => 'events'), 
        'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'custom-fields'), 
    ); 
  
    register_post_type('event', $args); 
} 
add_action('init', 'event_custom_post_type');   

// Register Custom Taxonomy 
function event_custom_taxonomy() { 
    $labels = array( 
        'name' => 'Types', 
        'singular_name' => 'Type', 
        'search_items' => 'Search Types', 
        'all_items' => 'All Types', 
        'parent_item' => 'Parent Type', 
        'parent_item_colon' => 'Parent Type:', 
        'edit_item' => 'Edit Type', 
        'update_item' => 'Update Type', 
        'add_new_item' => 'Add New Type', 
        'new_item_name' => 'New Type Name', 
        'menu_name' => 'Type', 
    );   

    $args = array( 
        'hierarchical' => true, // Behaves like categories 
        'labels' => $labels, 
        'show_ui' => true, 
        'show_admin_column' => true, 
        'query_var' => true, 
        'rewrite' => array('slug' => 'event-type'), 
    );   

    register_taxonomy('event_type', 'event', $args);   

    // Add terms to the Type taxonomy 
    wp_insert_term('In-Person', 'event_type'); 
    wp_insert_term('Live', 'event_type'); 
    wp_insert_term('Hybrid', 'event_type'); 
} 
add_action('init', 'event_custom_taxonomy'); 

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: 

 'show_in_rest'       => true, 

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. 

Github Copilot  
This is an extension to popular IDEs. 

Here is how it works. 

Llama2 

Meta AI's downloadable large language model 

Here is how it works. 

HuggingChat  

The online version of LLama2. Consider to be a competitor to ChatGPT. 

Microsoft Bing  

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