So, I Built a WordPress Plugin With ChatGPT

Social media is ablaze with talk of ChatGPT. OpenAI’s tool has generated buzz by having all the answers – or many of them, anyway.

And it seems like people find a new way to leverage the power of artificial intelligence (AI) every day. Within my circles, friends have used it to help with search engine optimization (SEO), craft responses to emails, and ask questions regarding various subjects.

WordPress has also been a popular topic. ChatGPT can provide lots of useful information for users and developers. But that’s only scratching the surface. The tool can also write code, and a few adventurous folks have used it to build plugins.

That last use case elicits passionate responses. Some people are understandably worried that AI will replace human programmers. Others appear certain that it’s the biggest money-making opportunity of our lifetimes.

I tend to be leery of AI. While I see potential benefits, I also believe there will be negative consequences. Some we might not be able to predict. But it’s fascinating nonetheless.

So, I couldn’t help but experiment to see how effective ChatGPT is at writing code. I used it to build a relatively basic WordPress plugin.

Could it possibly be better than me? Admittedly, that’s not a very high bar. But I was interested in the results. Here’s a look at what happened.

The Mission: Build a Customized Dashboard Widget

I’m a big fan of customizing the WordPress back end. The goal is to make it easier for clients to navigate and perform common tasks.

Among the little niceties I implement is a dashboard widget plugin. It’s nothing fancy. But it provides links to items my clients will access most often. It also removes some little-used default widgets that come with WordPress. There are no settings to tweak – the links are hard-coded into the plugin’s PHP file.

I’ve long thought about adding a back-end settings panel. This would allow links to be added or removed at any time – and without the need to touch code.

This has been on my to-do list for some time. But I’ve found the WordPress Settings API to be confusing. After a few failed attempts, I put the project on an indefinite hiatus.

Then ChatGPT came along. Reading the stories of other developers using it to write code got me excited. I wasn’t sure what to expect or if it would even work. But at worst, I’d waste an hour. That’s far less than I’d previously spent trying to build this revamped plugin.

A Single Prompt Builds a Solid Foundation

From what I’ve read, it appears that a key to using ChatGPT effectively is in the prompt. This is the instruction you provide to the tool.

Much like a human, the tool processes the prompt and responds. The better your instruction, the better the result. Or so goes the logic.

My initial prompt:

Write a WordPress plugin that allows administrators to customize a dashboard widget.

To my surprise, ChatGPT took my idea and immediately went to work. I watched as it produced code on my screen, line by line. It started with the standard WordPress plugin header. Within a couple of minutes, it had written 74 lines of code.

First, I copied and pasted the code into a file. Then I tweaked the plugin header text to suit my needs.  From there, I loaded it onto a local WordPress installation. I activated the plugin and…it worked.

The plugin had an options page under the WordPress Settings menu. This simple page used the familiar WordPress UI. It consisted of a textarea field where I could add content and a button to save my work.

The AI proved to be efficient – and perhaps more so than me. I tend to overthink these types of projects. My initial thought was to create a fancy repeater field UI that let me add one link at a time. ChatGPT’s use of a textarea was simpler and easier.

I also want to mention security, as it’s a pretty big deal. I was happy to see that it included WordPress sanitization techniques. Still, it’s always worth reviewing the output to make sure it adheres to best practices.

A look at my “conversation” with ChatGPT. Video-only

Asking for a Few Changes

As I experimented with the plugin, I immediately saw a few areas that could be improved. The first was swapping the textarea field on the settings page with the WordPress Classic Editor.

The WYSIWYG field would make it easier to add links and format text. But I wasn’t sure how to make the change. Sure, I could look it up. But if AI is supposed to do everything for me, I might as well ask…

Can the input field become a WYSIWYG?

Not only did I receive a quick answer – I also received code and instructions for where to make the changes.

Just like that, the plugin was improved. ChatGPT and I were starting to make for an incredible tag team. I would have asked it out for a beer, but I didn’t want things to become too awkward.

Flush with confidence, I decided to go even bigger:

Can I also customize the widget title?

Maybe I was a bit too greedy. This is where my new Android friend and I failed to get on the same page.

The initial response gave me hope:

“Yes, you can customize the widget title by modifying the custom_dashboard_widget() function to accept a parameter for the widget title. Here’s an updated version of the plugin code that allows administrators to customize both the widget title and content:”

But the code produced was a bit out of sorts. Things didn’t appear to be in the same logical order as in previous versions. The output was also truncated – perhaps due to a busy server. Were we really going to end on such a sour note?

Not to worry. ChatGPT was prepared for such situations. It offers a “Regenerate response” feature that does exactly what it says.

While it took a few tries, the tool eventually rewrote the plugin to reflect the requested changes. The final version is available on GitHub and free to use/modify.

A demonstration of how the plugin works. Video-only

Here’s What I Learned About AI (and Myself)

The efficiency of ChatGPT was impressive – at least in my experience. So much seems to depend on who you ask. But the tool appears to work better with technical subjects that are well documented. Thus, writing a WordPress plugin seems right up its alley.

It also reminded me of how difficult it can be to start development projects from scratch. I’m not the most knowledgeable coder. And I often try to accomplish everything at once, rather than taking it one step at a time.

Perhaps the best thing ChatGPT provided, then, was a small foundation. Once I saw how the plugin worked, I started to think of ways to improve the code. Start small and build as you go. That’s probably how it’s supposed to work.

In a roundabout way, AI gave me both a plugin and a lesson in patience. All in less than an hour’s work.

Digging Into the WordPress Block Protocol Plugin
More AI coming to WordPress