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

Sponsored Ad

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

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

Sponsored Ad

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

All suggestions are anonymous.

More from our blog...

One Response

Post a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Stay updated with WP Mayor's newsletter showcase every week

Stay on top of every new WordPress innovation and latest launches. Receive all our fresh product reviews and expert guides directly in your inbox.

Hosting Survey 2024

Are you happy with your hosting provider or are you over-paying for too little? Have your say below!

"*" indicates required fields

What's the main reason you picked this host?*
How happy are you with your host?*

OPTIONAL: If you'd like to receive the results of this survey, please enter your details below.