This post was written as part of the How to Write a WordPress Plugin series.
WordPress filters are the functions that your plugin can hook into with regards to modifying text. This modified text is usually formatted for either inserting into a database or displaying the output to the end user.
WordPress filters allow to you modify virtually any kind of text displayed and are extremely powerful. WordPress filters allow you to modify posts, feeds, how authors are displayed in comments, and much, much more.
To demonstrate the usefulness of WordPress filters, we will continue working with the existing code in the Devlounge Plugin Series code from the WordPress Plugin Actions post.
Adding A Content Filter
One of the cool filters you can hook into is one called ‘the_content‘. This particular filter is run for post content being displayed to the browser. We’re going to just add a line of text that will be displayed at the end of the content.
The format for adding a filter from the WordPress Plugin API is: add_filter('hook_name', 'your_filter', [priority], [accepted_args]);
We just need to add in a function to the DevloungePluginSeries class. Let’s call it addContent.
[php]
function addContent($content = ”) {
$content .= “
Devlounge Was Here
“;
return $content;
}
[/php]
In the above code, the following things are happening.
- The above function will accept one variable named content.
- If no variable is passed, a default value is set.
- The content variable has our line of text added to it.
- The text is then returned.
After the function is added to the class, the next step is to hook into the ‘the_content‘ filter and call the function.
[php]
//Actions and Filters
if (isset($dl_pluginSeries)) {
//Actions
add_action(‘wp_head’, array(&$dl_pluginSeries, ‘addHeaderCode’), 1);
//Filters
add_filter(‘the_content’, array(&$dl_pluginSeries, ‘addContent’));
}
[/php]
As you can see on line 6, a filter with the name ‘the_content‘ is added and our function ‘addContent‘ is called.
If the plugin were activated and a post was viewed, the text “Devlounge Was Here” would show up towards the end of the post content.
Adding An Author Filter
Another example of a filter I will show is manipulating the display of comment authors. I’m simply going to make all authors uppercase.
We just need to add in a function to the DevloungePluginSeries class. Let’s call it authorUpperCase.
[php]
function authorUpperCase($author = ”) {
return strtoupper($author);
}
[/php]
In the above code, the following things are happening.
- The above function will accept one variable named author.
- If no variable is passed, a default value is set.
- The author string is returned as uppercase.
After the function is added to the class, the next step is to hook into the ‘get_comment_author‘ filter and call the function.
[php]
//Actions and Filters
if (isset($dl_pluginSeries)) {
//Actions
add_action(‘wp_head’, array(&$dl_pluginSeries, ‘addHeaderCode’), 1);
//Filters
add_filter(‘the_content’, array(&$dl_pluginSeries, ‘addContent’));
add_filter(‘get_comment_author’, array(&$dl_pluginSeries, ‘authorUpperCase’));
}
[/php]
As you can see on line 7, a filter with the name ‘get_comment_author‘ is added and our function ‘authorUpperCase‘ is called.
If the plugin were activated and a post with comments was viewed, the comment authors would all be upper case.
Applying Filters
One of the more powerful things you can do with filters is to call then dynamically. There’s no need to add a filter to be run every time. You can run a filter whenever you choose from within your code.
The format for the apply_filters function is: apply_filter('filter name', 'your text');
You will see an example of apply_filters in use later in this series.
Conclusion
Hopefully you have a good understanding of the capability of WordPress Plugin Filters. You can download the code for the Devlounge Plugin Series filter portion.
Download the Code Used In This Post
For some further reading, please check out these links: