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.
Share on facebook
Share on twitter
Share on email
Share on facebook
Share on twitter
Share on email

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.

Table of Contents
Our Sponsors
Our sponsors

One Response

Leave a Reply

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

Stay up-to-date with the Mayor
Sign up to receive one weekly email about our latest reviews, tutorials, giveaways and more.

Black Friday Deals

Amazing offers on plugins,
themes, hosting & more!

+ 4 incredible giveaways ending in

Days
Hours
Minutes
Seconds