Can ChatGPT Write a Usable Custom WordPress Plugin?

At 212 Creative, we are always pushing the boundaries of what can be done. By now, everyone has heard of the AI program ChatGPT and the wonders it can do. It can write articles, create itineraries for your vacation, even write a speech for your product launch. That sounds great but we had to ask ourselves: “Can ChatGPT write a usable custom WordPress plugin?”

We were very curious, and just had to give it a try. Keep reading to find out what we discovered!

At 212 Creative, LC, we design and develop custom websites for clients in many varied fields: from municipalities and non-profit organizations to multinational corporations and local businesses. We were working on a custom website that required a custom post type with custom taxonomies and an additional custom field. We typically do this type of customization by writing a custom WordPress plugin. While this particular requirement is pretty simple and we’ve done many like this, we decided to put ChatGPT to the test!

Our ChatGPT WordPRess Plugin Experiment

We went to ChatGPT and typed in the query:

Write the code for a custom WordPress plugin that creates a custom post type called “Professional” with two taxonomies, “State” and “Expertise”, and a custom field for “Website URL”.

And here’s what ChatGPT returned:

<?php
/*
Plugin Name: Custom Professionals
Plugin URI: https://example.com/
Description: Creates a custom post type "Professional" with taxonomies "State" and "Expertise".
Version: 1.0
Author: Your Name
Author URI: https://example.com/
License: GPL2
*/

// Register the custom post type
function custom_professional_post_type() {
    $labels = array(
        'name'               => _x( 'Professionals', 'post type general name', 'textdomain' ),
        'singular_name'      => _x( 'Professional', 'post type singular name', 'textdomain' ),
        'menu_name'          => _x( 'Professionals', 'admin menu', 'textdomain' ),
        'name_admin_bar'     => _x( 'Professional', 'add new on admin bar', 'textdomain' ),
        'add_new'            => _x( 'Add New', 'professional', 'textdomain' ),
        'add_new_item'       => __( 'Add New Professional', 'textdomain' ),
        'new_item'           => __( 'New Professional', 'textdomain' ),
        'edit_item'          => __( 'Edit Professional', 'textdomain' ),
        'view_item'          => __( 'View Professional', 'textdomain' ),
        'all_items'          => __( 'All Professionals', 'textdomain' ),
        'search_items'       => __( 'Search Professionals', 'textdomain' ),
        'parent_item_colon'  => __( 'Parent Professionals:', 'textdomain' ),
        'not_found'          => __( 'No professionals found.', 'textdomain' ),
        'not_found_in_trash' => __( 'No professionals found in Trash.', 'textdomain' )
    );

    $args = array(
        'labels'             => $labels,
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array( 'slug' => 'professional' ),
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => false,
        'menu_position'      => null,
        'supports'           => array( 'title', 'editor', 'thumbnail' ),
        'taxonomies'         => array( 'state', 'expertise' ),
    );

    register_post_type( 'professional', $args );
}
add_action( 'init', 'custom_professional_post_type' );

// Register the custom taxonomies
function custom_professional_taxonomies() {
    // State Taxonomy
    $labels = array(
        'name'              => _x( 'States', 'taxonomy general name', 'textdomain' ),
        'singular_name'     => _x( 'State', 'taxonomy singular name', 'textdomain' ),
        'search_items'      => __( 'Search States', 'textdomain' ),
        'all_items'         => __( 'All States', 'textdomain' ),
        'parent_item'       => __( 'Parent State', 'textdomain' ),
        'parent_item_colon' => __( 'Parent State:', 'textdomain' ),
        'edit_item'         => __( 'Edit State', 'textdomain' ),
        'update_item'       => __( 'Update State', 'textdomain' ),
        'add_new_item'      => __( 'Add New State', 'textdomain' ),
        'new_item_name'     => __( 'New State Name', 'textdomain' ),
        'menu_name'         => __( 'State', 'textdomain' ),
    );

    $args = array(
        'hierarchical'      => false,
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'state' ),
    );

    register_taxonomy( 'state', 'professional', $args );

    // Expertise Taxonomy
    $labels = array(
        'name'              => _x( 'Expertise', 'taxonomy general name', 'textdomain' ),
        'singular_name'     => _x( 'Expertise', 'taxonomy singular name', 'textdomain' ),
        'search_items'      => __( 'Search Expertise', 'textdomain' ),
        'all_items'         => __( 'All Expertise', 'textdomain' ),
        'parent_item'       => __( 'Parent Expertise', 'textdomain' ),
        'parent_item_colon' => __( 'Parent Expertise:', 'textdomain' ),
        'edit_item'         => __( 'Edit Expertise', 'textdomain' ),
        'update_item'       => __( 'Update Expertise', 'textdomain' ),
        'add_new_item'      => __( 'Add New Expertise', 'textdomain' ),
        'new_item_name'     => __( 'New Expertise Name', 'textdomain' ),
        'menu_name'         => __( 'Expertise', 'textdomain' ),
    );

    $args = array(
        'hierarchical'      => false,
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'expertise' ),
    );

    register_taxonomy( 'expertise', 'professional', $args );
}
add_action( 'init', 'custom_professional_taxonomies' );

// Add custom field for Website URL
function custom_professional_add_meta_boxes() {
    add_meta_box(
        'custom_professional_website_url',
        __( 'Website URL', 'textdomain' ),
        'custom_professional_website_url_callback',
        'professional',
        'normal',
        'high'
    );
}
add_action( 'add_meta_boxes', 'custom_professional_add_meta_boxes' );

// Callback function for Website URL custom field
function custom_professional_website_url_callback( $post ) {
    $website_url = get_post_meta( $post->ID, '_custom_professional_website_url', true );
    ?>
    <input type="text" name="_custom_professional_website_url" value="<?php echo esc_attr( $website_url ); ?>" style="width:100%;">
    <?php
}

// Save Website URL custom field
function custom_professional_save_meta_data( $post_id ) {
    if ( ! isset( $_POST['_custom_professional_website_url'] ) ) {
        return;
    }

    $website_url = sanitize_text_field( $_POST['_custom_professional_website_url'] );
    update_post_meta( $post_id, '_custom_professional_website_url', $website_url );
}
add_action( 'save_post', 'custom_professional_save_meta_data' );

The following instruction were also given:

This code creates a custom post type called "Professional" with two taxonomies "State" and "Expertise", and adds a custom field for "Website URL" using WordPress' custom post type and taxonomy registration functions (register_post_type() and register_taxonomy()). It also adds a meta box for the "Website URL" custom field and saves its value when the post is saved using the add_meta_boxes and save_post actions respectively. Please note that you may need to customize the code further based on your specific requirements and desired functionality.

ChatGPT gave us the code, and even instructions on how and where to insert the code in the plugin.

Our first reaction was “That’s pretty incredible!”  Our second thought was “Yeah, but will it work in the actual plugin?”  

So, the answer to our first question “Can ChatGPT write a usable custom WordPres plugin?”  was an excited “Yes!”  Sure it can write code, but we didn’t stop there…

Our next two questions were:

  • Does the code work?
  • How difficult is it to implement into an existing WordPress site?

We put it to the test, here’s what we discovered:

Does the code Generated by ChatGPT work?

I have to say that the plugin worked and completely fulfilled the requirements I asked for. So my answer to the first question is: ChatGPT can write a usable WordPress plugin IF the person interacting with it knows how to properly define the requirements, AND knows what to look for in the generated code.

How difficult is it to implement This Plugin Generated by ChatGPT into an existing WordPress site?

Here I would have to say that the plugin was quite simple to implement for anyone who knows how to install WordPress plugins. That being said, the plugin did not exactly follow what we at 212 Creative believe is best practice. For example, it’s considered best practice to include an empty index.php file in the plugin directly to ensure that the directory cannot be accessed directly. While any developer knows this and will typically follow this practice, ChatGPT did not mention anything about that or produce the code for the index.php file. Now to be fair, I suppose if I would have specified that as one of the requirements for the plugin, ChatGPT would have generated the WordPress plugin code accordingly. Overall, this is a non-issue for me, so the answer to the question is: The custom WordPress plugin generated entirely by ChatGPT was simple to install and worked as expected. However, use caution and be sure to have a developer look over the plugin for potential security vulnerabilities before using a ChatGPT-generated plugin in a production environment.

In conclusion, the answer to our question “Can ChatGPT write a usable custom WordPress plugin?” would have to be a resounding…ALMOST. While the AI engine generated usable code for our plugin, it did require quite a bit of human interaction and prompting to get to a usable plugin. The plugin did work as required, but didn’t exactly follow best practice. We encourage you to try it on your own and double check everything carefully.

Coming Up…

Next we’ll see if ChatGPT can extend the functionality of our custom WordPress plugin with some additional and more complex requirements.

Are you ready to discuss your upcoming project?