Feature
Post

Category
Code


WordPress Plugin Filters

How To Write a WordPress Plugin Series

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.

function addContent($content = '') {
			$content .= "<p>Devlounge Was Here</p>";
			return $content;
		}

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.

//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')); 
}

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.

function authorUpperCase($author = '') {
			return strtoupper($author);
		}

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.

//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'));
}

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:


  1. By test posted on October 1, 2008 at 7:57 am
    Want an avatar? Get a gravatar! • You can link to this comment

    test desc

  2. By test wer posted on October 1, 2008 at 7:58 am
    Want an avatar? Get a gravatar! • You can link to this comment

    dddddddddd

  3. By iPin.com posted on January 26, 2009 at 12:10 am
    Want an avatar? Get a gravatar! • You can link to this comment

    Interested in your plugin which can show codes in post. I am looking for it.

  4. By Hikari posted on July 1, 2009 at 1:43 pm
    Want an avatar? Get a gravatar! • You can link to this comment

    Hello, I’m looking for a filter that would let me know if the current page is the homeplage/archive/category and is printing just a teaser or if it is a proper post/page page and the whole post/page will be shown.

    Do you know how that could be done?

    I need it because hte plugin I’ll develop will only print data if it is the whole content, if it’s a teaser it will be ommited.

    tnx :)

  5. By Alexey Vesnin posted on August 4, 2009 at 9:44 pm
    Want an avatar? Get a gravatar! • You can link to this comment

    Doesn’t work for me :( http://digest-it.ru – check any post…. plugin activated and with that content replacing hook. Header hook works OK, content – nope

  6. By jack posted on October 13, 2009 at 12:12 pm
    Want an avatar? Get a gravatar! • You can link to this comment

    Thank, it’s a good explanation.
    But still, something is missing:

    Can you please explain the difference between add_filter and apply_filters?

    If you take the author example – add_filter(‘get_comment_author’, array(&$dl_pluginSeries, ‘authorUpperCase’));

    When would you use the “authorUpperCase” filer inside an “Apply_filter”?

  7. TrackbackPlugin für Wordpress Bastelanleitung - D-Sign WeblogWie programmier ich ein Wordpress PlugIn | Der Softwareentwickler BlogWordPress Plugin Filters Devlounge | Wood TV StandEssential Wordpress Plugin Development Resources, Tutorials and Guides : Speckyboy Design Magazine