Using wordpress filters

Using wordpress filters

Online by JSC0d3
June 19, 2017 | | | 432 recognitions

What is a Filter in WordPress? How to Use Filters in WordPressFilter hooks are one of the the more powerful WordPress features that help to provide a huge amount of flexibility when it comes to modifying WordPress, including custom themes and plugins.

There could easily be hundreds of different unique tutorials on how to use filters in themes and plugins, simply because of the huge array of things you can do with them. In this quick tutorial, I’m just going to give you a simple example that demonstrates what a filter hook does.

There are four primary core functions that you will often use when working with filters:

  1. add_filter() – used for adding a new custom filter
  2. remove_filter() – used for removing an already registered filter
  3. apply_filters() – runs the provided data through the specified filter
  4. has_filter() – checks whether a specific filter has been registered

You can think of a filter as a method for modifying data and you can try to see the function with better names:

apply_filters(
	$filter_name,     // used for add_filter( $filter_name, 'callback' );
	$value_to_change, // the only variable whose value you can change
	$context_1,       // context
	$context_2        // more context
);
  1. $filter_name – The name of the filter (same as before).
  2. $value_to_change – The name of our callback function (same as before).
  3. $context_1 – Basically, this third parameter is the priority in which this filter gets applied. In our case, this is our only filter being added, and so it doesn’t matter. We’ll just keep this as 10 because that’s the default value WordPress has there, anyway. IF there were other filters overriding ours, we could increase that priority number to be larger.
  4. $context_2 – Then this fourth parameter is the the number of parameters to be passed into our callback function. By default, WordPress has this as 1 and so we need to change it to 3 in this scenario. That will accommodate the three parameters we’re passing into our callback — (1) Value being altered, (2) first helper variable, and (3) second helper variable.

What is a Filter in WordPress? How to Use Filters in WordPress

For example the following list of names is displayed on one of our WordPress pages with a function like this:

$names = array(
	'Joe',
	'James',
	'Mary',
	'John',
	'Sarah',
	'Tudor',
);

if ( ! function_exists( 'js_show_names' ) ) {
	function js_show_names( $names ) {
		$list = '<ul>';
		foreach ( $names as $name ) :
			$list .= '<li>' . $name . '</li>';
		endforeach;
		$list .= '</ul>';

		return $list;
	}
}
echo js_show_names();

Well what if we want to modify the list displayed via a plugin? First we need to update out display function to include a filter. We can do that like this:

$names = array(
	'Joe',
	'James',
	'Mary',
	'John',
	'Sarah',
	'Tudor',
);

if ( ! function_exists( 'js_show_names' ) ) {
	function js_show_names( $names ) {
		$list = '<ul>';
		if ( has_filter( 'js_add_names' ) ) {
			$names = apply_filters( 'js_add_names', $names );
		}
		foreach ( $names as $name ) :
			$list .= '<li>' . $name . '</li>';
		endforeach;
		$list .= '</ul>';

		return $list;
	}
}
echo js_show_names();

So what happens when we apply the filter to our array of names? Well, it could be anything. We could selectively remove names, we could change their names, we could add names, etc. In this example, I will simply add additional name to the list.

First, we setup our filter function like this:

if ( ! function_exists( 'js_add_extra_names' ) ) {
	function js_add_extra_names( $names ) {
		// the $names parameter is an array of all names from
		// the js_show_names() function
		return $names;
	}
}
add_filter( 'js_add_names', 'js_add_extra_names' );

The array of names created in our js_show_names() function is passed as a parameter. Since this is an array of all the names, we can do whatever we want with it. At the end of the function, we have to return the $names array, otherwise we will end up with a blank array.

So to add additional names to our list, we can do this:

if ( ! function_exists( 'js_add_extra_names' ) ) {
	function js_add_extra_names( $names ) {
		// the $names parameter is an array of all names from 
		// the js_show_names($names) function  
		$extra_names = array( 'Steven', 'Michaela', 'Michael' );
		// combine the two arrays 
		$names = array_merge( $extra_names, $names );

		return $names;
	}
}
add_filter( 'js_add_names', 'js_add_extra_names' );

So here’s what is really happening. In the js_display_names() function we setup the original array of names, then, if the filter exists, we pass that array of names through our filter function. This is done with the apply_filters() hook. The js_add_extra_names() function then takes the $names array and modifies it. Once the modification is complete, the array is returned back to the js_display_names() function, which then loops through each of the items in the array and displays them.

Why use hooks?

Now you know how they work, you can see that understanding hooks is absolutely necessary for anyone developing with WordPress. It’s also very useful even if you are not a developer but want to modify WordPress’s—or your theme’s— behavior.
With an understanding of hooks, you can:
  • Change almost anything in WordPress—even quite fundamental things—because a lot of WordPress’s core functions use actions and filters;
  • Make changes easily: once you’ve understood the concepts, you can make some incredibly complex changes very quickly;
  • Change a theme’s behavior at the source, rather than trying to retro-fit an inappropriate solution with HTML and CSS;
  • Make your own changes easy to understand and easier to debug, because your code is reduced to a minimum;
  • Enable and disable your changes easily because each piece of code is a small unit in your functions.php;
  • Make your changes relatively upgrade-proof because you no longer need to edit or copy WordPress or any themes and plugins core files;
  • Share your knowledge and swap code snippets with others.
Fully understanding hooks can take a few hours or (much) longer, depending on your skills, but it will save you days of time in the future.

References:

JSC0d3's Logo
About JSC0d3

JSC0d3 is an entrepreneur, online marketer, and an employee of an IT company. When not building websites, creating content, or helping customers improve their online business, spend time with their wife and two beautiful children. Although he still feels new in WordPress, he enjoys sharing what he has learned with all of you! If you want to get in touch with him, you can do this through this website.

On the same idea

Posted by | April 1, 2019

Images are vital components of every website Before you start questioning the importance of images, just try to imagine your favorite blog or website...

Posted by | March 5, 2019

I’ve been loosely following the noise and #wpdrama surrounding Gutenberg for as long as it has been around and honestly for the most part I’ve...

Posted by | February 24, 2019

To ensure that your site ranks highly in Search Engine Result Pages (SERPs), you’ll need to make it easy for search engine ‘bots’ to explore...

Previous PostBackNext Post

Leave here an impression