How to Override a Function attached to a Filter

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

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
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.

Consider sharing this post so others can find it:

Stay in the know on the latest and greatest products and services in WordPress. We'll send you one email every week with new and trending tutorials and reviews.

Haven't found an answer to your question? Ask us about it.
Although we publish new posts every week, we might not be answering your questions. Recommend a topic and we'll do our best to cover it in one of our next posts.
From hosting plans to WooCommerce plugins and backup services, we have put together a collection of exclusive coupons and deals!
WP Mayor_Icon_white
WP Mayor Team

Get the latest reviews & tutorials, just like this one, curated in your inbox.