Blog / Build with AI /

Writing a WordPress plugin with AI: the minimum viable plugin

A WordPress plugin sounds like serious software. The smallest one is a single PHP file with about ten lines.

That is the point of a minimum viable plugin: the smallest thing that works, does one job, and teaches you how plugins are put together. Once you’ve seen a tiny one run, bigger ones stop looking mysterious.

This is the beginner’s follow-up to building a WordPress plugin with AI: the workflow that works, starting from nothing.

The finished thing: a plugin that adds a shortcode, [last_updated], which shows when a post was last modified. Useful on its own, and a safe first build.

Rule zero: don’t test on a live site

Plugins run PHP on your server. A small mistake can show a white screen to every visitor. So:

  • Use a local WordPress install or a staging copy, never the live site.
  • Take a backup if there is anything you’d miss.
  • Turn on debugging while you work by setting WP_DEBUG to true in wp-config.php on the test site.

WordPress snippets: where to put AI-written code safely explains why a plugin is often a better home for custom code than your theme.

What a plugin is

A plugin is a PHP file, or a folder of them, in wp-content/plugins/. WordPress reads a comment block at the top to learn the plugin’s name, then loads the code when the plugin is active. That’s all.

Think of WordPress as a house with labelled hooks on the walls. A plugin is something you hang on a hook. In our case, the hook is “when a shortcode is used in a post, run this function”.

Step 1: describe the plugin

Write a minimal WordPress plugin as a single PHP file. It should register a shortcode called [last_updated] that outputs the last modified date of the current post, escaped for safe output. Include the standard plugin header, a check that stops the file from being run directly, and a short comment above each part. No settings page, no database changes, no external libraries.

The constraints keep it small. “No settings page” alone saves you from an afternoon of extra code.

Step 2: the code you should expect

You’ll get something close to this:

<?php
/**
 * Plugin Name: Last Updated Shortcode
 * Description: Adds [last_updated] to show when a post was last modified.
 * Version: 0.1.0
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

add_shortcode( 'last_updated', function () {
    return esc_html( get_the_modified_date() );
} );

Read it line by line:

  • The header comment makes it appear in the Plugins screen.
  • The ABSPATH check stops anyone loading the file directly outside WordPress.
  • add_shortcode says: when [last_updated] appears, run this function.
  • get_the_modified_date() returns the last modified date of the current post, in the format set in your site’s settings.
  • esc_html() makes the output safe to print. Escaping output is a habit worth building from day one.

Step 3: install and test

  1. Create a file named last-updated-shortcode.php.
  2. Put it in a folder named last-updated-shortcode inside wp-content/plugins/.
  3. In the dashboard, go to Plugins and activate it.
  4. Edit a post, add a Shortcode block containing [last_updated], and view the post.

You should see a date. Edit the post, save it and check that the date changes.

If the site shows an error, deactivate the plugin by renaming its folder through your file manager or FTP. WordPress then disables it automatically, which is why a test site is so useful.

Step 4: test the awkward cases

  • Use the shortcode on a page as well as a post.
  • Use it in a widget area or a sidebar. It may behave differently outside the main content, since there’s no “current post” in some places.
  • Change the site’s date format in Settings and see that the output follows it.
  • Try it with no content, and on a draft preview.

Finding that the shortcode misbehaves in a sidebar is useful. It teaches the idea of context: many WordPress functions depend on which post is being processed.

Step 5: make one improvement at a time

Ideas, in order of difficulty:

  1. Add an optional format: [last_updated format="j F Y"]. This introduces shortcode attributes.
  2. Add a label: “Updated on”.
  3. Show nothing if the post was never edited since publishing.
  4. Wrap the date in a <time> element with a machine-readable date.

Each is a separate prompt and a separate test. Iterating with AI: small changes beat big rewrites is the discipline to keep.

What AI gets wrong in WordPress code

These are the problems I’d watch for:

  • Missing escaping. Output must be escaped; input must be validated or sanitised.
  • Invented functions. WordPress has a large, specific set of functions. If you don’t recognise one, search the official developer documentation before you use it. AI hallucinations in code shows how.
  • Old patterns. Direct database queries where a built-in function exists, or deprecated functions.
  • Generic names. Functions called init_plugin will clash with other plugins. Real plugins prefix function names, such as ekxx_last_updated.
  • Too much at once. Admin pages, saved options and AJAX all in one go. That’s a recipe for security holes.
  • Security shortcuts. Anything that saves data from a form needs a nonce and a capability check. If your plugin does that and the AI hasn’t mentioned them, ask.

Copying code from AI safely has a general checklist.

Before it goes on a real site

Then install it, check the front end and keep an eye on the error log for a day or so.

Why start this small

A first plugin should teach the shape: header, hook, function, output. Once that’s clear, adding an options page or a custom block is just more of the same, with more care about security. People who jump straight to a “complete SEO plugin” tend to learn mostly how to debug code they don’t understand.

If you manage WordPress sites, even a tiny plugin like this is practical. It replaces a snippet buried in a theme, and it survives when the theme changes.

Key takeaways

  • A WordPress plugin can be one PHP file with a header and a function.
  • Use a local or staging site, never live, while you learn.
  • Give the AI tight constraints: one job, no settings, escaped output.
  • Test in different contexts and change one thing at a time.
  • Check for escaping, invented functions and unprefixed names before relying on it.

What’s one small thing you do on your WordPress site by hand that a ten-line plugin could do for you?