This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

📣 The Agency Advantage Summit is Here. Join 2,000+ digital pros for 2 days of AI-powered strategies. Register for Free →

WordPress Hooks Explained: How to Use Actions & Filters (With Practical Examples)

Updated on May 9, 2025

9 Min Read
WordPress hooks

In WordPress development, hooks are built-in functions that let you run custom code at specific spots during the page load or data processing without editing core files. That means you can change how things work or add new features while keeping your site safe.

Hooks come in two types: actions and filters. We’ll look at what each does as we walk through how WordPress hooks work, along with practical examples to help you get started using them.

What Are WordPress Hooks & How They Work?

At their core, WordPress hooks exist to trigger custom functions automatically. As I mentioned earlier, hooks are divided into two categories:

  • Action hooks let you add new elements — like displaying a custom message, loading a script, or adding a button.
  • Filter hooks are used to change things that already exist, such as modifying text or adjusting data before it’s shown

Let me show you how this works in practice. Here’s a basic action hook example:

// Add a promotional banner after blog posts

add_action('the_content', 'add_post_banner');

function add_post_banner($content) {

if (is_single()) {

$content .= '<div class="promo-banner">Special offer for readers: 20% off our premium plan!</div>';

}

return $content;

}

This code adds a special offer banner that automatically appears at the end of each blog post, giving you an easy way to promote deals or important messages to your readers.

WordPress Hooks—Actions vs. Filters

Here’s the key difference:

An action takes the data it receives, does something with it (like sending an email, loading a script, or adding a message), and that’s it. It doesn’t send anything back — it just runs and finishes.

A filter, on the other hand, takes the data, changes it in some way, and then hands it back. You can use filters to tweak content, modify settings, or adjust what gets shown on the screen.

How to Use WordPress Action & Filter Hooks?

Before we get started, there are a couple of things you’ll need. Since we’re working with code, you should be comfortable editing PHP files. You don’t need to be a developer, but knowing how to access your site files and make basic changes is important.

For this tutorial, you’ll need access to your WordPress theme’s functions.php file. You can do this either from your WordPress dashboard (go to Appearance > Theme File Editor) or by using an FTP tool like FileZilla if you prefer working directly with files on the server.

I’ll access my functions.php file through FTP using FileZilla.

Let’s get started…

Action Hooks Example:

Let’s say you want to display a short announcement on every page. Here’s a simple code I’ll use:

// Show a welcome bar across the top of the site

function show_welcome_bar() {

echo '<div class="welcome-bar" style="background: #eee; padding: 10px; text-align: center;">Welcome! Don’t miss our latest deals.</div>';

}

add_action('wp_body_open', 'show_welcome_bar');

To add this code to my functions.php file, I’ll open FileZilla and connect using my FTP credentials—host, username, password, and port. Since I’m using Cloudways, I’ll grab these credentials from the Master Credentials tab in my server settings.

Master Credentials

QuickConnect

Once logged in, I’ll go to public_html/wp-content/themes/your-active-theme-name/. I’m using Astra.

using Astra

Now I’ll edit the functions.php

.ViewEdit

Now, I’ll paste the code snippet example I shared earlier at the bottom of the file. Like so:

Dont miss our deals

Save the file and drag and drop the file into FileZilla. FileZilla will ask you if you wanna overwrite the existing file—click OK.

Overwrite

That’s it!

Now, here’s what the code we added will do:

show_welcome_bar() is a custom function that adds a message.

add_action(‘wp_body_open’, ‘show_welcome_bar’) tells WordPress to run your function right after the opening <body> tag.

This hook runs automatically every time a page loads.

And here is the result:

Joyce roberts

You can easily use these hooks if you are using hosting for WordPress from Cloudways.

Filter Hooks Example

Like I said earlier, filters are used to change something that already exists — like editing content, tweaking a message, or adjusting how a certain piece of data looks before it’s displayed.

Now let’s see how to use a filter hook through a practical example. Let’s say you’re running a blog with affiliate links, and you want to automatically add a short disclaimer at the end of every post.

To do this, I’ll add the code snippet mentioned below to my functions.php file.

// Add affiliate disclaimer to the end of posts

function add_affiliate_disclaimer($content) {

if (is_single()) {

$disclaimer = '<p><em>Some links in this post may be affiliate links. We may earn a commission if you click and buy something.</em></p>';

$content .= $disclaimer;

}

return $content;

}

add_filter('the_content', 'add_affiliate_disclaimer');

we may earn a commission

Here’s how this code works:

add_filter(‘the_content’, …) tells WordPress: “Hey, before you show a post, run it through this function first.”

Inside the function, we check if it’s a single post using is_single(). This way, the disclaimer doesn’t show up on category pages or the homepage.

If it is a post, we attach the disclaimer to the end of the content using .= — which just means “add this to the existing content.

Finally, the modified content is returned and shown to the reader.

So now, whenever someone opens a blog post, they’ll automatically see your affiliate note at the bottom.

Like so:

Affiliate post

That is it. This is how easily you can use WordPress hooks. Once you’re comfortable with hooks, you’ll find yourself making all sorts of custom changes without ever touching the WordPress core.

Before You Add Hooks, Know What Your Theme or Plugin Supports

If you tried the examples above and they worked—great! But there’s something critical to understand:

Hooks only work if they exist in your theme or plugin.

Let’s take wp_body_open as an example. That’s a WordPress core action hook, but for your code to run, your theme must actually call this hook somewhere in its templates—like inside the <body> tag.

Astra, the theme used in this tutorial, supports wp_body_open. But not every theme does. If your theme doesn’t include that hook, then your custom code won’t do anything.

The same goes for filters. The the_content filter (Filter Reference) is reliably present in WordPress core—it’s used when rendering post content. That’s why our affiliate disclaimer appeared successfully. But if you try to filter something else—say, a product title in WooCommerce—you’ll need to find the exact filter hook WooCommerce provides for that.

To find what hooks are supported in your theme or plugin, the simplest way is to:

Open your theme or plugin files and search for:

  • do_action(‘hook_name’) → this creates an action hook
  • apply_filters(‘hook_name’, $value) → this creates a filter hook

If a do_action() or apply_filters() isn’t written in the code, there’s nothing for your function to attach to.

Make WordPress hooks easier to work with on Cloudways

Whether you’re using actions or filters, Cloudways gives you a clean, fast setup to test and apply hooks without the hassle. Try it free for 3 days. No credit card required.

Understanding Hook Priority

Now that you know how to add hooks, here’s a little trick to control when your hook runs compared to others: priority.

Every hook you add can take a priority number. WordPress uses this to determine the order things happen.

Let’s look at an example:

add_action('wp_footer', 'first_thing', 5);

add_action('wp_footer', 'second_thing', 15);

In this case, first_thing runs before second_thing, because lower numbers run first.

The default priority is 10, but you can set it lower or higher to move things earlier or later in the load order.

Why does this matter?

Sometimes you’re working with plugins or themes that are also hooking into the same spot. Setting the priority helps you either run before or after their code — depending on what you need.

So if something’s not showing up exactly where you want, tweaking the priority can often fix it.

How to Remove Action and Filter Hooks in WordPress?

If you feel like removing an action or filter hook from your site, maybe it’s slowing things down, or is just not needed anymore, you can use the remove_action() and remove_filter() methods to achieve this.

These functions give you a clean way to disable a previously added hook, without deleting the original code.

This is especially useful when working with themes or plugins that aren’t yours. Editing or removing someone else’s code directly can break things fast and may trigger errors if done carelessly.

So instead of cutting lines from a plugin file, it’s usually safer to unhook them through your own theme’s functions.php.

For this blog, I’ll show you how to unhook the action and filter hooks we created earlier.

Example 1: How to Unhook the Welcome Bar (Action Hook)

Earlier, we added this code to display a welcome message across the top of every page:

function show_welcome_bar() {

echo '<div class="welcome-bar" style="background: #eee; padding: 10px; text-align: center;">Welcome! Don’t miss our latest deals.</div>';

}

add_action('wp_body_open', 'show_welcome_bar');

Now let’s say you want to turn this off — maybe you’re running a limited-time sale and don’t need the banner anymore.

To remove it, just add this line of code (below the original or in a separate file):

remove_action('wp_body_open', 'show_welcome_bar');

Below this code

WordPress will now skip that action and won’t display the welcome bar when loading pages.

Important: To successfully remove an action, the remove_action() call must run after the original add_action() has been registered. If needed, wrap the removal code inside a hook like this:

add_action('init', function() {

remove_action('wp_body_open', 'show_welcome_bar');

});

Example 2: How to Disable the Affiliate Disclaimer (Filter Hook)

Earlier, we also used a filter to automatically add an affiliate disclosure at the end of blog posts:

function add_affiliate_disclaimer($content) {

if (is_single()) {

$disclaimer = '<p><em>Some links in this post may be affiliate links. We may earn a commission if you click and buy something.</em></p>';

$content .= $disclaimer;

}

return $content;

}

add_filter('the_content', 'add_affiliate_disclaimer');

If you later decide you don’t want this to show anymore — maybe you’re updating your content strategy — you can unhook it with:

remove_filter('the_content', 'add_affiliate_disclaimer');

Again, make sure the filter is removed after it was added.

affiliate disclaimer

Wrapping it inside an init function ensures the timing works:

add_action('init', function() {

remove_filter('the_content', 'add_affiliate_disclaimer');

});

Example 3: How to Disable Multiple Hooks at Once

Want to turn off more than one hook all at once?

Here’s how to do this…

Suppose your functions.php file has these:

// Custom Action Hooks

function add_welcome_message() {

echo '<p>Welcome to our site!</p>';

}

add_action('wp_footer', 'add_welcome_message');

function track_user_time() {

// Logic to track time on page

}

add_action('wp_head', 'track_user_time');

function show_discount_banner() {

echo '<div class="banner">Limited-time offer!</div>';

}

add_action('woocommerce_before_main_content', 'show_discount_banner');




// Custom Filter Hooks

function modify_excerpt_more($more) {

return '... [continue]';

}

add_filter('excerpt_more', 'modify_excerpt_more');

function customize_title($title) {

return $title . ' | MySite';

}

add_filter('the_title', 'customize_title');

Instead of writing a remove_ line for each hook all over your file, you can group them in one function and remove them in one go.

Like so:

function remove_custom_hooks() {

// Remove Actions

remove_action('wp_footer', 'add_welcome_message');

remove_action('wp_head', 'track_user_time');

remove_action('woocommerce_before_main_content', 'show_discount_banner');

// Remove Filters

remove_filter('excerpt_more', 'modify_excerpt_more');

remove_filter('the_title', 'customize_title');

}

add_action('init', 'remove_custom_hooks');

Now you may have noticed I’ve used init. The reason why I used it is because it runs after WordPress has loaded but before any output happens — which ensures that any previously declared add_action() or add_filter() calls are registered and can now be safely removed.

Wrapping Up!

WordPress hooks give you control over how your site functions. You don’t need to be a full-time developer to use them—just some basic understanding of PHP is enough to get started.

Hooks let you modify how WordPress, themes, and plugins behave without editing their core files. That’s important because it keeps your changes safe during updates. Whether you’re adding content, adjusting layout, or triggering custom behavior, hooks are a clean way to do it.

Earlier in this post, we looked at action and filter hooks and saw how they’re used. We also discussed why it’s essential to know which hooks are already available in your theme or plugin—your code won’t work if the hook doesn’t exist.

Have questions? Let me know in the comments below.

Share your opinion in the comment section. COMMENT NOW

Share This Article

Abdul Rehman

Abdul is a tech-savvy, coffee-fueled, and creatively driven marketer who loves keeping up with the latest software updates and tech gadgets. He's also a skilled technical writer who can explain complex concepts simply for a broad audience. Abdul enjoys sharing his knowledge of the Cloud industry through user manuals, documentation, and blog posts.

×

Webinar: How to Get 100% Scores on Core Web Vitals

Join Joe Williams & Aleksandar Savkovic on 29th of March, 2021.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Want to Experience the Cloudways Platform in Its Full Glory?

Take a FREE guided tour of Cloudways and see for yourself how easily you can manage your server & apps on the leading cloud-hosting platform.

Start my tour