was successfully added to your cart.
CorePlugin Development

Simplifying WordPress Plugin Filters

By November 13, 2018 No Comments

WordPress Filters (Hooks)?

WordPress comes bundled with dozens of these filters and they are created using the apply_filters() function. This function takes in the following arguments.

  • $tag (required)
  • $value (required)
  • $var (optional)

The $tag variable is the name of the filter.
The $value is the variable or value that will be filtered.
The $var variable is optional but allows you to pass extra values into your function.

Plugins and themes often create these filters so that you can change elements without having to modify the source itself (making them extensible). These filters are often defined inside functions.

A WordPress filter is a hook that accepts a variable (or series of variables) and returns them back after they have been modified. These filters are often used so that you have a way to manipulate default information.

Image Source: https://www.wpexplorer.com/best-wordpress-plugins/

A Simple WordPress Filter

Plugins are great, they provide both developers and admins with a way to extend and re-use functionality. While they are a great tool they do still have both their strengths and weaknesses.

The get_the_excerpt filter is a filter that you can use inside the posts loop to access the excerpt.

This filter is defined as part of WordPress’s core and only defines the name of the filter and the function to call, it doesn’t define any optional arguments.

//get the except for a post, as defined in /wp-includes/post-template.php
echo apply_filters( 'the_excerpt', get_the_excerpt() );

You could attach your own function to the the_excerpt filter and then manipulate the excerpt before you return it (for example, you could wrap it inside a container, change its content or trim its length).

Creating your Own Filters

Let’s look at an example. If we are creating an array of names in a plugin we can make it filterable by using the apply_filters hook to tell WordPress that these values can be changed by any function that hooks onto them.

//make our name value array filterable
$name_values = apply_filters('filter_name_array_values',
            array('Joanna','Peter')
);

Creating a filter like this will let other developers (or yourself) change these values in a dynamic way. Generally, if you are inside a function or doing some sort of functionality it is good practice to offer filters to make your elements extensible.

Executing your Filters

When you want to execute your filter you will need to use the add_filter() function. This function takes the following values

$tag (required)
$function_to_add (required)
$priority (optional)
$accepted_args (optional but mandatory if we have multiple passed arguments)

The add_filter function works in a similar way to add_action. You define the name of the filter you want to call, along with the function that will be called and you’re good to go. Let’s look at how we can use this to filter the list of names we created in our previous example.

//Change the saved name values by filtering them
function add_new_names($names){
    $names[] = 'Simon';
    return $names;
}
add_filter('filter_name_array_values','add_new_names');

As you can see we are attaching our add_new_names function onto our filter_name_array_values filter and taking in a single variable of $names. When we defined our filter before with apply_filters we set the default values to be an array with the values of Joanna and Peter.

Our function here will take the existing values (which we have called $names here for simplicity) and added a new value onto the end of it called Simon.

At the very end we return the values. Returning the values is very very important, if you don’t you will break everything. The reason the values must be returned is because any additional function also attached to that filter will receive these values. If you don’t return them they will have nothing to use.

Using Filters

You will use the apply_filters function to create your filters inside your theme or plugin (supplying the values you wish to filter). You will then use the add_filter function to hook your specific function onto this filter, allowing you to manipulate the filtered variable and return it.

More Plugin Development Resources

If you’re looking for even more reading, tutorials or content, here are a few more resources the check out:

What Are WordPress Actions?
The Best WordPress Plugin Development Tutorials
Plugin Development – Web Design for Optimization
A New Beginning in WordPress Coding
Simple to follow wordpress plugin development tutorials
WordPress Plugins Explained

 

Content Credit: https://www.sitepoint.com/an-introduction-to-wordpress-plugin-development/

Matt-image

Matthew is a Marketing Professor at Fanshawe College who specializes in technical marketing but has dabbled in many graphic disciplines from animation to graphic design and photography to web development. Besides teaching thousands of students over his various online platforms, he has created a number of companies including Eagleye Photography Inc, Behind the Curtain and FLT- Free lightroom tutorials.com. Matthew holds a Masters of Technology Entrepreneurship and Innovation from McMaster University (Ontario, Canada), a Bachelor of Education degree and Bachelor of Arts degree with Honors.

Leave a Reply