Blog / Build with AI /

WordPress hooks explained for AI builders

When ChatGPT writes a WordPress snippet, it almost always starts with add_action( or add_filter(. If you don’t know what those are, the code is a magic spell: it works, or it doesn’t, and you can’t tell why.

Those two functions are hooks, and understanding them is the single most useful thing you can learn about WordPress code. They are how a plugin, a theme or your own snippet plugs into WordPress without editing a single core file.

The analogy: a train with stations

Picture WordPress loading a page as a train journey. It passes through a long list of stations: starting up, loading plugins, setting up the theme, building the header, fetching the posts, printing the footer, finishing.

At many of those stations, WordPress has put up a sign: “If anyone has anything to do here, now’s the time.” Those signs are hooks. Your code can say, “I’d like to do something at the footer station” and WordPress will call your function when the train gets there.

There are two kinds of sign.

Actions: “do something now”

An action is a moment where WordPress says, “this is happening, join in if you like.” Your function runs, and it doesn’t need to return anything.

Example: add a line of text to the footer of every page.

function mysite_footer_note() {
    echo '<p>Built with care.</p>';
}
add_action( 'wp_footer', 'mysite_footer_note' );

Read it as: when WordPress reaches the wp_footer moment, run mysite_footer_note. The function prints something. That’s an action.

Common actions you’ll see:

Hook When it runs
init WordPress has loaded; a good time to register custom post types
wp_enqueue_scripts The right place to add CSS and JavaScript files to the front end
wp_head Inside the <head> of the page
wp_footer Just before the closing </body>
save_post A post has been saved
admin_menu Time to add menu items to the admin area

Filters: “change this before you use it”

A filter is a moment where WordPress holds up a value and says, “I’m about to use this. Want to change it first?” Your function receives the value, may alter it, and must return it.

Example: add a note to the end of every post.

function mysite_add_note( $content ) {
    if ( is_single() && in_the_loop() && is_main_query() ) {
        $content .= '<p><em>Thanks for reading.</em></p>';
    }
    return $content;
}
add_filter( 'the_content', 'mysite_add_note' );

The filter the_content carries the text of a post. Your function takes it, adds a sentence, and passes it back. The if line limits it to single posts in the main loop, so it doesn’t appear in widgets, excerpts or other places that also call the_content.

The rule that causes the most grief: a filter function must always return the value, even when it didn’t change it. Forget the return, and the content disappears.

Common filters:

Hook What it carries
the_content The body of a post
the_title A post title
excerpt_length How many words in an automatic excerpt
body_class The CSS classes on the body tag
upload_mimes Which file types can be uploaded

Action or filter? A quick test

  • Does your code need to do something (print, save, send, register)? Probably an action.
  • Does it need to change a value on its way somewhere? A filter.

When in doubt, ask the AI: “Should this be an action or a filter, and which hook is correct?”

Priority and arguments

Two extras you’ll see:

add_filter( 'the_content', 'mysite_add_note', 20, 1 );
  • Priority (here 20) controls the order. The default is 10; lower numbers run earlier. If two functions hook the same place, priority decides who goes first. When your change seems to be overwritten by something else, try adjusting it.
  • Arguments (here 1) is how many pieces of information your function receives. Some hooks pass several values, and you must say how many you want.

Prompting AI for hooks

Hook mistakes are the most common reason generated WordPress code misbehaves. The model might pick a hook that fires at the wrong time, or forget to return a filter value. Helpful habits:

Ask which hook and why.

Write a WordPress snippet that adds a “Reading time” line above each post. Say which hook you chose, whether it’s an action or filter, and why that one rather than another. Make sure the function returns a value if it’s a filter, and only runs on single posts.

Ask it to explain the timing.

At what point in a page load does the hook init run compared with wp_enqueue_scripts? Explain simply.

Check the official reference. WordPress’s developer documentation lists hooks and their parameters. If the AI uses a hook you can’t find there, or one that sounds made up, be suspicious. Models do sometimes invent plausible names.

Prefix your functions. mysite_ stops your function names clashing with plugins. I mention it in WordPress shortcodes built with AI, and it matters just as much here.

Removing a hook

Plugins and themes use hooks too. You can unhook something with remove_action or remove_filter, using the same hook name, function name and priority it was added with. Timing matters: you must remove it after it’s been added, so removal often goes on a later hook such as init. It’s a useful trick, and it’s easy to get wrong, so test it on a staging site.

Making your own hooks

Developers can create their own hooks with do_action() and apply_filters(), which lets other code extend theirs. That’s how big plugins allow add-ons. You don’t need it yet, but it’s good to know why plugins publish “hook reference” pages.

Safe testing

Hooks change how the whole site behaves. A bad filter can blank every post; a bad action on init can take the site down. Work on a staging copy or a local site, keep a backup, and know how to deactivate the plugin file by renaming it in the file manager if the site breaks. I covered the wider approach in automating boring WordPress tasks with AI-written snippets.

Key takeaways

  • Hooks are signs along WordPress’s page-loading journey where your code can join in.
  • Actions do something; filters change a value and must return it.
  • Priority controls order; arguments control what your function receives.
  • Ask AI to name the hook and explain why, and check the name in WordPress’s documentation.
  • Prefix your functions and test on a copy.

Pick a snippet you’ve already pasted into a site. Can you tell whether it uses an action or a filter, and which moment it hooks into?