What AI Can Teach Web Developers About WordPress


By

Artificial intelligence (AI) is a tool for getting things done. The WordPress community is already using it to write and troubleshoot code. Meanwhile, the WordPress project is in the midst of making AI integrations easier for all.

That’s all great and exciting stuff. However, we may be missing the boat regarding other use cases. AI has the potential to do more than perform menial tasks. We can also pick its virtual brain to learn more about WordPress and related technologies.

Human knowledge still matters, and AI is only as effective as the person interacting with it. So, why not learn as much as we can? This new technology could be a goldmine of information, with the results improving our project outcomes and helping us develop better processes.

So, what can AI teach us about WordPress? Let’s find out!

Explain What a Function or Code Snippet Does

Code snippets aren’t always easy to understand, particularly when you’re unfamiliar with them. For example, you may inherit a website that includes a custom plugin or theme template. Likewise, you might not recall the purpose of something you wrote years ago.

In addition, WordPress includes hundreds of hooks, filters, and functions. Some have better documentation than others (and even that can be difficult to comprehend).

AI can help you identify what a code snippet does in short order. Paste it into your favorite Large Language Model (LLM), and you’ll receive a detailed overview of what’s happening. You can even ask follow-up questions to narrow down a specific line.


Here’s a simple example, using a snippet commonly found in theme templates and plugins. We’ll ask ChatGPT to provide some details.

"What does the following code snippet do?"
if ( ! defined( 'ABSPATH' ) ) {
  exit;
}

ChatGPT responded with a breakdown of the code’s functionality and operation.

This snippet is a standard security guard used in WordPress PHP files, especially in plugins and themes.

What it does
It prevents the PHP file from being accessed directly via the browser.

How it works
1. ABSPATH

  • ABSPATH is a constant defined by WordPress in wp-config.php during the normal WordPress bootstrap process.
  • It contains the absolute filesystem path to the WordPress installation.

2. defined( ‘ABSPATH’ ) check

  • The code checks whether WordPress has already been loaded.
  • If WordPress is running normally, ABSPATH will exist.

3. Direct access scenario
If someone tries to access the file directly in a browser, e.g.:
https://example.com/wp-content/plugins/my-plugin/my-file.php

WordPress has not been loaded, so:
defined(‘ABSPATH’) === false

4. exit;
Because the constant isn’t defined, the script immediately stops executing.

Why this matters
Without this check:
-Someone could directly run the PHP file.
-Internal functions might run outside the WordPress environment.
-It could expose sensitive data, database queries, or admin functionality.

ChatGPT’s response was even more robust than what’s included above. It also gave specifics on where our snippet is typically found and advice on security best practices.

This is all valuable information. We now know the purpose of our snippet and have a step-by-step guide to explain what it does.

Discover Different Approaches for Building a Feature

Our first attempt to build a feature isn’t always the best one. After all, the goal is to find the best answer, not the quickest.

That’s why code typically undergoes several rounds of revisions. It seems like there is always something we didn’t account for in the initial try. For instance, there could be a missing detail or a feature that includes unnecessary bloat. Both humans and AI models make these mistakes.

As such, it’s worth weighing your options. There may be an approach that’s easier to maintain or extend later. This also comes in handy when a client inevitably changes their mind.

We can use this line of thinking when instructing AI to generate code. Let’s consider building a related posts plugin.

You might start with a prompt like this:

"Build me a related posts plugin for WordPress."

The above may get the job done. However, AI may not offer you multiple approaches by default. Let’s add some nuance to the prompt:

"I want to build a related posts plugin for WordPress. Give me at least two approaches for achieving this, along with the pros and cons for each one."

AI’s answer may even lead you to think of other potential features or issues. It’s another example of how AI should work for you, not the other way around.

Research Improvements and Optimizations

Our code may work, but is it the best it can be? Is it secure and performant? Will it be compatible with the next version of WordPress? All are important considerations.

There’s something to be said for having a second set of eyes (or the virtual equivalent) review your work. That applies to proofing a new project or refactoring legacy code. You won’t have to look far to find room for improvement.

Why not ask AI for a few suggestions? Paste a code snippet or upload a file, then instruct the app to improve performance, efficiency, and security.

You may also want to share relevant documentation, such as the WordPress theme and plugin handbooks. That will ensure AI compares your code against best practices.


Let’s put that theory to the test by asking AI to review an old-school WordPress post query.

"Review this code snippet and tell me how to improve it. Could it be more secure or efficient?"

  <?php
  global $post;
  $args = array( 'numberposts' => 1, 'offset'=> 0,  'category' => 4 );
  $myposts = get_posts( $args );
  foreach( $myposts as $post ) :    setup_postdata($post); ?>
  <p><a target="_blank" href="<?php the_permalink();  ?>"><?php the_title(); ?></a></p>
  <?php endforeach; ?>

  <?php wp_reset_query(); ?>

Once again, ChatGPT gave us a detailed overview of what our code does and steps to improve it.

Yes — you can make this cleaner, safer, and a bit more correct (especially around globals and reset functions).

What your snippet currently does

  • Fetches 1 post from category ID 4
  • Loops it, outputs a link with title
  • Calls setup_postdata() so template tags like the_title() / the_permalink() work
  • Resets something at the end

Issues / improvements

1. Don’t rely on global $post in the loop variable

Right now you’re doing:
global $post;
foreach ( $myposts as $post ) :

That overwrites the global $post reference inside the loop (and can confuse readers). Use a different variable name (e.g. $featured_post) and then pass it to setup_postdata().

2. Use wp_reset_postdata() (not wp_reset_query())

  • get_posts() does not modify the main query ($wp_query)
  • wp_reset_query() resets the main query and is meant for query_posts() usage (which you should avoid)
  • After setup_postdata(), the right cleanup is wp_reset_postdata()

3. Escape output (security/hardening)

  • the_title() and the_permalink() generally output safe data, but in custom code it’s best practice to use the “get_” versions and escape yourself:
  • esc_url( get_permalink( … ) )
  • esc_html( get_the_title( … ) )

This is the main “more secure” improvement.

4. Prefer newer/clearer query args

numberposts works, but posts_per_page is the more standard arg.

Also consider:

  • ‘no_found_rows’ => true (micro-optimization; avoids pagination counts)
  • ‘ignore_sticky_posts’ => true (avoid sticky surprises)
  • ‘post_status’ => ‘publish’ (explicit)

5. If you only need one post, you don’t need a foreach

You can grab the first result and output it, reducing code and risk.

In addition, AI provided two revised snippets that include the above suggestions. It’s an easy way to modernize an old code snippet.

Using AI as a Learning Opportunity

AI apps are easy to use when working with code – almost too easy. It’s tempting to copy the results and ignore the app’s explanation of what it just did. However, there’s value in slowing down and reading those details.

In some ways, using AI can be akin to having a seasoned developer by your side. You can learn a lot by paying attention.

That’s particularly true for well-documented software like WordPress. The content management system (CMS) is full of niche functions and settings. Not to mention a whole ecosystem of themes and plugins.

You can use AI as a guide for understanding how everything works. It’s a way to dig deeper and experiment with building from scratch and reworking your existing code.

The bottom line is that, if you want to learn more about WordPress, AI offers a simple way to do it. Even better, it (probably) won’t get mad if you keep asking questions!



Top
This page may contain affiliate links. At no extra cost to you, we may earn a commission from any purchase via the links on our site. You can at any time.