AI for Wordpress Development.

Just a decorative image for the page.

Intro

AI will make software developers unemployed. And I’ve written about this (already some time ago).

Here’s a real world example of AI usage that saved me many hours of work.

Creating a Wordpress Plugin Without Knowing How to Do It

As you might know I am developing a privacy-first web analytics platform. Privatracker. Cool tech. TimescaleDB. etc. Now - one of my clients is running many wordpress blogs and wanted to use Privatracker on her blogs. Privatracker is basically just a script that you have to include on your html page. Dead simple.

But integrating a script is not not that easy in Wordpress. Adding the snippet code is only one part. The other part is making sure not to break any templates and other html that gets put together by Wordpress. Updates of templates might override your changes.

The good news is that Wordpress has a plugin system that allows you to modify the base html in durable way. This plugin system allows you to add code to the footer html.

But this means I have to write a Wordpress Plugin. That takes time. I have to read the docs of Wordpress. Try out code. Trial and error. Collect all the pieces. Find out how to store some stuff and then add the script to each footer programmatically. Doable. But would take me some hours to get this production-ready.

There’s a better way. A faster way. ChatGPT! (no surprise here).

The Prompt

I’ve simply used gpt-4o with the following prompt:

I'd like to create a wordpress plugin that where I can set an id in the admin area of my wp site. 
The plugin then makes sure that each page has the following script in the footer 
(above the body tag):

<script defer 
        src=https://app.privatracker.com/visit.js
        data-site-id=74141c4-d103-49c3-b44e-c882e905de25>
</script>

How would I do that? What code is needed?

The Outcome

ChatGPT perfectly walked me through the creation process and created the full and working plugin code to make this work.

This took me exactly 2 minutes. From a few hours of work to 2 minutes. This is crazy.

<?php
/*
Plugin Name: Privatracker
Description: Allows to track Things on Each Page.
Version: 1.0
Author: Ra Bauer
*/

add_action('admin_menu', 'privatracker_add_admin_menu');
add_action('admin_init', 'privatracker_settings_init');

function privatracker_add_admin_menu() {
    add_options_page('Privatracker Script', 'Privatracker Script', 'manage_options', 'privatracker-script', 'privatracker_options_page');
}

function privatracker_settings_init() {
    register_setting('privatrackerPlugin', 'privatracker_settings');

    add_settings_section(
        'privatracker_privatrackerPlugin_section',
        __('Privatracker Script Settings', 'wordpress'),
        'privatracker_settings_section_callback',
        'privatrackerPlugin'
    );

    add_settings_field(
        'privatracker_text_field_0',
        __('Data Site ID', 'wordpress'),
        'privatracker_text_field_0_render',
        'privatrackerPlugin',
        'privatracker_privatrackerPlugin_section'
    );
}

function privatracker_text_field_0_render() {
    $options = get_option('privatracker_settings');
    ?>
    <input type='text' name='privatracker_settings[privatracker_text_field_0]' value='<?php echo $options['privatracker_text_field_0']; ?>'>
    <?php
}

function privatracker_settings_section_callback() {
    echo __('Enter your Data Site ID to be used in the script.', 'wordpress');
}

function privatracker_options_page() {
    ?>
    <form action='options.php' method='post'>
        <h2>Privatracker Script</h2>
        <?php
        settings_fields('privatrackerPlugin');
        do_settings_sections('privatrackerPlugin');
        submit_button();
        ?>
    </form>
    <?php
}

add_action('wp_footer', 'privatracker_add_script_to_footer');

function privatracker_add_script_to_footer() {
    $options = get_option('privatracker_settings');
    if (isset($options['privatracker_text_field_0']) && !empty($options['privatracker_text_field_0'])) {
        $data_site_id = esc_attr($options['privatracker_text_field_0']);
        echo "<script defer src='https://app.privatracker.com/visit.js' data-site-id='{$data_site_id}'></script>";
    }
}

Conclusion

ChatGPT saved me many hours of tedious work. I became Wordpress plugin developer within a second and without any prior knowledge to Wordpress.

We are living in crazy times. And this is only the beginning. Not using ChatGPT is no longer an option for anyone.

Related posts