Blog / Build with AI /

Asking AI for WordPress code that follows the coding standards

Ask an AI for a bit of WordPress code and you’ll usually get something that runs. That’s the low bar. The real questions are whether it’s safe, whether it will survive the next update, and whether it would pass review if you ever wanted to put it on WordPress.org.

The assistant knows the WordPress way of doing things. It also knows a lot of old tutorials, and those mix good habits with bad ones. Your prompt decides which it leans on.

The finished thing

A short prompt template and a review checklist, so any snippet or plugin you get from an assistant uses WordPress’s own functions, escapes what it prints, checks who is allowed to do what, and follows the project’s style.

Why the standards exist

WordPress has published coding standards for PHP, JavaScript, CSS and HTML, and a set of security practices that go with them. They aren’t decoration. They exist because thousands of plugins by thousands of authors have to live on the same site.

For you, they mean three practical things:

  • Safety. Most real-world plugin vulnerabilities come from unescaped output, unchecked input or missing permission checks.
  • Compatibility. Using WordPress’s own functions (for database access, HTTP requests, file handling) keeps your code working across hosts and versions.
  • Readability. Code in a consistent style is easier to review, including by you in six months.

The prompt template

Start from this and adjust:

I’m writing a WordPress plugin. Follow the WordPress PHP Coding Standards. Prefix all functions, classes, options and hooks with myplugin_. Use WordPress APIs rather than raw PHP where one exists (wpdb with prepare for queries, wp_remote_get for HTTP, the Settings API for options). Sanitise all input and escape all output with the right function for the context. Check capabilities with current_user_can() before any admin action, and verify a nonce on every form or AJAX request. Add a plugin header and a text domain for translation. Don’t use deprecated functions. Explain anything you’re unsure about.

The last sentence matters. Assistants will confidently guess; asking them to flag uncertainty gives you a list of things to check.

Then describe the feature in its own paragraph. Keep the “how to write it” and the “what to build” separate, so the rules apply to everything.

The five things to check in the result

You can review a lot without being a PHP expert. Look for these.

1. Escaping on output

Anything printed into a page should pass through an escaping function that matches where it lands: esc_html() for text, esc_attr() for attribute values, esc_url() for links, wp_kses_post() where limited HTML is allowed. If you see echo $_GET['something'] or a variable printed bare, that’s a flaw.

2. Sanitising on input

Values arriving from forms, URLs or APIs should be cleaned before being saved or used: sanitize_text_field(), absint(), sanitize_email() and friends. Different data needs different functions, and “it’s only an admin form” isn’t an excuse.

3. Nonces and capabilities

Two separate questions: did this request come from my own form (a nonce), and is this person allowed to do this (a capability check). A missing nonce lets other sites trick a logged-in admin into submitting actions; a missing capability check lets people who shouldn’t change things do exactly that. Both are explained with examples in making a WordPress plugin secure: nonces, sanitising and escaping.

4. Database queries

If the code builds SQL by joining strings with user values, stop. Queries with variables should use $wpdb->prepare(). Better still, ask whether the built-in functions (WP_Query, get_option, update_post_meta) can do the job so there’s no custom SQL at all.

5. Prefixes and clean-up

Functions without a unique prefix can clash with other plugins and cause fatal errors. And if the plugin stores options, ask what happens on uninstall. A good assistant will add an uninstall.php when asked.

Use the tooling, not just your eyes

You don’t have to rely on a visual review. WordPress has an official ruleset for PHP_CodeSniffer (the WordPress Coding Standards), which flags many of the problems above automatically. Plugin Check, the tool WordPress.org uses, goes further on security and plugin requirements.

A neat loop:

  1. Ask the assistant for the code.
  2. Run the checker on it.
  3. Paste the checker’s errors back into the assistant: “Fix these warnings without changing behaviour.”
  4. Run the checker again.

Machines are good at style rules and bad at intent, so you’re still the one deciding whether the feature does what you want. If a tool and the assistant disagree, trust the tool for rules and your own judgement for behaviour.

What assistants get wrong

  • Old habits. Direct mysql_ style thinking, extract(), short echo tags, or loading jQuery from a CDN rather than using the registered script.
  • Inline everything. Scripts and styles printed straight into the page instead of enqueued with wp_enqueue_script() and wp_enqueue_style().
  • Hard-coded paths and URLs. Use plugin_dir_path(), plugins_url() and home_url().
  • Invented functions. If a function name looks plausible but you can’t find it in the WordPress developer reference, it may not exist. Look it up before you trust it.
  • Over-engineering. Three classes and an autoloader for a task that needed twenty lines. Ask for the simplest version first.

Test on a copy

Whatever the checker says, run new code on a staging site or a local install, with debugging turned on so notices and warnings show. Never paste untested generated code into a live site’s functions file. One typo can take the front end down.

Key takeaways

  • Put the standards in the prompt: prefixes, escaping, sanitising, nonces, capabilities.
  • Review for five things: output, input, permissions, queries, prefixes.
  • Let automated checkers do the style work, then feed their output back to the assistant.
  • Check unfamiliar function names against the official reference.
  • Test on a copy, with debugging on.

FAQ

Do I need to follow the coding standards for a private plugin?

Not legally. But the security parts protect your own site, and the style parts make the code easier to maintain. Only a plugin for WordPress.org has to pass review.

Will the assistant remember my rules next time?

Not reliably. Keep your template somewhere you can paste it, and start each new request with it.

Next time you get a snippet, run it through the five checks before you run it on a real site. Which one do you think you’ll find missing most often?