WordPress Filter Explained

Often confused bits of wordpress are hooks and filters. As the name goes filter are generally related to the content where as hooks are more related to the events. WordPress Filters Filters are used to modify text before adding it to databse or before its been output to the screen. WordPress filters takes two parameters. […]

Often confused bits of wordpress are hooks and filters. As the name goes filter are generally related to the content where as hooks are more related to the events.

Wordpress Filters
Filters are used to modify text before adding it to databse or before its been output to the screen.

Wordpress filters takes two parameters. The first one being the content we want to modify and second the callback function.

For example fif we add <hr> tag after the content we can do something like:

<?php
add_filter(“the_content”, function ($content)
{
return $content. "<hr />";
}
?>

Here above I am using the anonymous function which is supported in PHP 5.3.0 or above, as its for demo only. In case if your version is older than PHP 5.3.0, you will have to use
callback function. The alternate of above filter will be:

<?php
add_filter(“the_content”, "add_hr");
function add_hr($content)
{
return $content. "<hr />";
}
?>

Filters are the part of wordpress plugin development, and here is the complete list of avaialble list of wordpress filters.

About Author

Robin Thebe

Digital Strategist based in Sydney.

I am multi disciplined web developer based in Sydney focusing around WordPress web design, wordpress development, SEO, SEM and Email Marketing.