How to Override a Function attached to a Filter

If you purchase through a link on our site, we may earn a commission. Learn more.

These days I am experimenting more and more with building extendible WordPress plugins. Today I'll share with you a quick tip on how to override a function that is attached to a filter hook.
Table of Contents
WP Engine High Performance Hosting
BionicWP Hosting

These days I am experimenting more and more with building extendible WordPress plugins.

Today I’ll share with you a quick tip on how to override a function that is attached to a filter hook.

Hooks are the way we build extensibility into plugins. So let’s consider an example.

In this example, I wrote a simple function that appends some text at the end of a post or page.

[php]
// Function to display some text at the end of a post

function hello_text( $content ) {
$default = get_hello_text();
return $content = $content . $default;
}

add_filter( ‘the_content’, ‘hello_text’);
[/php]

As you can see the function above gets its text from a function named get_hello_text(). It then appends it to the $content variable, containing the content of a post or page.

So lets take a look at that get_hello_text() function next.

[php]
// Function to define the text – default

function get_hello_text() {
$hello_text = ‘Hello Malta’;
return apply_filters( ‘custom_hello_text_filter’, $hello_text );
}
[/php]

The function above, get_hello_text(), defines a variable $hello_text with the words ‘Hello Malta’. Notice the very important next line however, which makes this function extendible.

We are using apply_filters, and thus creating a filter hook named custom_hello_text_filter.

Our output at this stage is, as you might guess, ‘Hello Malta‘.

At this point we can start extending our code. Take a look at what I did next

[php]
// Function to define the text – first extension

function custom_hello_text( $custom_hello_text ) {
$custom_hello_text = ‘Hello Europe’;
return $custom_hello_text;
}

add_filter( ‘custom_hello_text_filter’,’custom_hello_text’);
[/php]

What happened here? We used the add_filter function to attach the custom_hello_text() function to the filter named custom_hello_text_filter, thus this text will be used now, and the output will be ‘Hello Europe‘.

Let’s say I create another plugin and I want to extend things further, having another function which changes the text again, and we want to ignore any previous usage of the filter named custom_hello_text_filter.

Filters come with a priority parameter, the default of which is 10, so to override a function all we need to do is attach it to the filter hook with a higher priority, as follows:

[php]
// Function to define the text – second extension

function custom_hello_text_overridden( $custom_hello_text ) {
$custom_hello_text = ‘Hello World’;
return $custom_hello_text;
}

add_filter( ‘custom_hello_text_filter’,’custom_hello_text_overridden’,99);
[/php]

The higher priority of 99 will ensure that the final output will now be ‘Hello World‘.

Hope you found that useful, any improvement suggestions or questions are welcome.

If you enjoyed this post, make sure to subscribe to WP Mayor’s RSS feed.

Jean Galea

Jean Galea is an investor, entrepreneur, and blogger. He is the founder of WP Mayor, the plugins WP RSS Aggregator and Spotlight, as well as the Mastermind.fm podcast. His personal blog can be found at jeangalea.com.

Discover more from our archives ↓

Popular articles ↓

One Response

Share Your Thoughts

Your email address will not be published. Required fields are marked *

Claim Your Free Website Tip 👇

Leave your name, email and website URL below to receive one actionable improvement tip tailored for your website within the next 24 hours.

"They identified areas for improvement that we had not previously considered." - Elliot

By providing your information, you'll also be subscribing to our weekly newsletter packed with exclusive content and insights. You can unsubscribe at any time with just one click.