Popular Filter Hooks

More than 1,000 different hooks are available in WordPress, which is a bit overwhelming at first. Luckily, a handful of them are used much more often than the rest. This section explores some of the more commonly used hooks in WordPress.

Some of the more common Filter hooks are:

> the_content: Applied to the content of the post or page before displaying.

> the_content_rss: Applied to the content of post or page for RSS inclusion.

> the_title: Applied to the post or page title before displaying.

> comment_text: Applied to the comment text before displaying.

> wp_title: Applied to the page <title> before displaying.

> get_categories: Applied to the category list generated by get_categories.

> the_permalink: Applied to the permalink URL.

Let's look at some of the more popular Filter hooks in WordPress, starting with a more practical example than our profanity filter using the_content Filter hook. This hook allows you to alter the content for posts and pages prior to it being displayed in the browser. By using this hook you can add your custom content either before, in the middle, or after the content:

<?php function SubscribeFooter($content) { if(is_single()) {

$content.= '<h3>' .__('Enjoyed this article?', 'gmp-plugin')

<a href="http://example.com/feed">RSS feed</a>!', 'gmp-plugin'). '</p>'; }

return $content;

add_filter ('the_content', 'SubscribeFooter'); ?>

In this example you are adding your subscribe text to the bottom of your content. Notice you are also using the is_single conditional tag to verify that your subscribe text is added only on a single post page. The $content variable stores all of the post or page content, so by appending your subscribe text you are adding it to the bottom of your content. This is the ideal way to add content to the bottom of all posts because you aren't actually modifying the post. In the future if you decide to change this message you can change it in one place, rather than updating every post in your web site.

Another powerful Filter hook is the_title. This hook is used for changing the post or page title prior to being displayed. Here's an example using this Filter:

<?php function custom_title($title) {

$title .= ' - ' .__('By Example.com', 'gmp-plugin');

return $title;

This example adds '' - By Example.com'' to all of your post and page titles. Remember, this doesn't actually modify the title in the database, but instead modifies the display of the title to the end user.

The default_content Filter hook is useful for setting the default content for your post and pages. This is helpful if you have a set format for all of your posts and can save valuable writing time:

<?php function my_default_content($content) {

$content = __('For more great content please subscribe to my RSS feed',

'gmp-plugin'); return $content;

add_filter('default_content', 'my_default_content'); ?>

Filter hooks are exceptionally powerful for inserting your own processing into a variety of points in the Loop processing of each post. Realizing the full power of the WordPress plugin system means also using action hooks to fire your own code in response to events within the WordPress core.

Was this article helpful?

0 0
Blogging For Maximum Profit

Blogging For Maximum Profit

Working Hard, Working Smart or Both; Without The Right Plan, You're Working For Less Than You Could Be! Do You Have A Plan For Making Some Serious Cash With Your Blog NOW? Then download Blogging For Maximum Profits.

Get My Free Ebook

Post a comment