AI

AI Generated WordPress Code Can Work and Still Be the Wrong Solution

AI can generate WordPress code that looks correct while completely missing the real problem. A practical example of why reviewing the surrounding system matters just as much as reviewing the generated code itself.

Table of Content

AI coding tools are very good at producing code that looks plausible.

That is useful when the problem is understood correctly. It becomes less useful when the code is being changed before anyone has established where the problem actually is.

A recent client situation was a good example.

The client had tried to add a small piece of custom functionality to a WordPress site using an AI coding assistant. The requirement was not particularly complex, and the generated solution looked reasonable. It also failed in a way that led the AI into repeatedly rewriting code that was never the real problem.

What the Client Was Trying to Build

The client had a WordPress page containing a directory of resources displayed through a Query Loop.

They wanted to add a simple live filter above the directory.

The requirement was straightforward:

  • visitors should be able to type into a search field;
  • filtering should begin after a few characters;
  • matching resources should remain visible while non-matching resources disappear;
  • the page should not reload;
  • clearing the search field should restore the complete list;
  • if nothing matches, a simple message should be displayed.

The archive was relatively small, so there was no reason to introduce a separate search service or make a new server request for every search.

The resources were already present in the page.

A small amount of JavaScript filtering the rendered content was enough.

What the AI Suggested

The AI produced a small JavaScript implementation.

The basic idea was sensible: find the search input, collect the rendered resource items, create a searchable representation of their text and listen for changes to the input.

A simplified version looked something like this:

const input = document.querySelector(
    '.resource-filter__input'
);

const items = Array.from(
    document.querySelectorAll('.resource-item')
);

const index = items.map((item) => ({
    element: item,
    text: item.textContent
        .toLowerCase()
        .trim()
}));

input.addEventListener('input', () => {
    const query = input.value
        .toLowerCase()
        .trim();

    index.forEach(({ element, text }) => {
        const matches =
            query === '' ||
            text.includes(query);

        element.hidden = !matches;
    });
});

For this particular requirement, there was nothing fundamentally wrong with that approach.

But on the actual site, typing into the field did nothing.

Submitting the field still triggered the site’s existing search behaviour and loaded a normal WordPress search results page.

The client returned to the AI assistant and asked it to fix the problem.

That was where things became more interesting.

The AI Started Fixing the JavaScript

The first response treated the issue as a form-submission problem.

The generated code was changed to intercept the form:

const form = document.querySelector(
    '.resource-filter'
);

form.addEventListener('submit', (event) => {
    event.preventDefault();
});

The behaviour did not change.

The next suggestion changed the selectors.

Instead of looking for the original custom input class, the code targeted the input inside the existing WordPress search form:

const form = document.querySelector(
    'form.resource-filter'
);

const input = form.querySelector(
    'input[type="search"]'
);

Still no change.

Another version changed which DOM element was hidden:

const row = item.closest('.resource-list__item');

row.hidden = !matches;

That was also a plausible improvement. Hiding an inner element instead of the complete Query Loop item can produce awkward empty spaces in a grid.

But the page still behaved exactly as it had before.

More JavaScript was being generated.

Nothing was actually being fixed.

What I Observed

When I looked at the problem, the individual JavaScript changes were not the first thing that stood out.

The unchanged behavior was.

Several materially different versions of the script had supposedly been installed, yet the browser behaved identically every time.

That is useful information.

If an input event listener is attached to a search field, typing should cause something to happen. The filtering might be wrong. The selector might target the wrong items. The visual result might be
broken.

But there should normally be some observable change.

Here there was none.

The existing WordPress search behaviour also continued to work normally.

That suggested a more basic question:

Is the JavaScript being executed on this page at all?

Before changing another selector or rewriting another event listener, I checked the page itself.

The expected search form was present in the DOM:

document.querySelector(
    'form.resource-filter'
);

It returned the form.

Then I checked whether the custom JavaScript file was actually present:

document.querySelector(
    'script[src*="resource-tools.js"]'
)?.src;

The result was:

undefined

The JavaScript file was not loaded on the page.

At that point, the behaviour made complete sense.

The Actual Problem Was in WordPress

The custom functionality had been placed in a small plugin.

Its assets were conditionally loaded so that the JavaScript and CSS were only included on pages where they were required.

That is normally a reasonable decision.

The condition, however, did not match the way this particular WordPress site was structured.

The plugin contained logic similar to this:

$is_entry   = is_singular( 'post' );
$is_listing = is_home();

if ( ! $is_entry && ! $is_listing ) {
    return;
}

The developer had assumed that the resource listing was the WordPress posts page.

It was not.

The client had built it as a normal WordPress Page containing a Query Loop.

For that request, is_home() returned false.

The enqueue function therefore stopped before the JavaScript was registered on the page.

The relevant condition needed to represent the actual page being used:

$is_entry   = is_singular( 'post' );
$is_listing = is_page( 'resources' );

if ( ! $is_entry && ! $is_listing ) {
    return;
}

Once the asset was loaded on the correct page, the live filtering code could actually run.

The search problem did not require another JavaScript rewrite.

It required one WordPress condition to be corrected.

Why the AI’s Answers Were Convincing

This is the part that makes AI-assisted development worth reviewing carefully.

Most of the generated fixes were technically plausible.

Preventing a form submission with:

event.preventDefault();

is perfectly normal.

Using:

input[type="search"]

to target a search input is perfectly normal.

Hiding the complete result container rather than one of its children can also be the correct implementation.

If those suggestions are reviewed individually, none necessarily looks alarming.

The problem is that they were answering the wrong question.

The failure was being treated as:

broken filtering logic

when the actual chain looked like this:

incorrect WordPress page condition
        ↓
JavaScript asset is not loaded
        ↓
search initialization never runs
        ↓
event listeners are never attached
        ↓
existing WordPress behaviour remains unchanged

Changing the event listener at the bottom of that chain cannot fix the condition at the top.

The More Important Review Was Not a Code Review

The useful part of the review was not finding a cleverer implementation.

It was reducing the number of assumptions.

First:

Does the expected HTML exist?

Yes.

Then:

Is the JavaScript responsible for the feature actually loaded?

No.

That immediately moved the investigation away from the filtering algorithm and toward WordPress asset loading.

Only after confirming that the script was present would it have made sense to investigate selectors, events and filtering behaviour.

This distinction matters because AI coding assistants tend to respond to the problem they are shown.

If the prompt says:

My JavaScript search doesn’t work.

The model has many plausible JavaScript changes available to it.

If nobody verifies that the JavaScript is running, the conversation can turn into a sequence of increasingly elaborate solutions to a problem that does not exist.

Conclusion

The lesson from this case is not that AI-generated code is inherently unreliable.

The original approach was reasonable. Several of the later suggestions were reasonable too.

The failure was diagnostic.

The AI kept changing implementation details without first validating a more fundamental assumption: whether the code being modified was present on the page at all.

That is an important distinction when reviewing AI-generated WordPress code.

A useful review should not stop at:

Is this code valid?

It should also ask:

Is this code running in the environment we think it is?

And then:

Does the implementation match the way WordPress is actually handling this request?

AI can generate the next version of a function in seconds.

Sometimes the better technical decision is not to generate another version at all.