The plugin used on the homepage of this blog to display featured articles is called Related Posts with Thumbs. It will display the last X posts published in a given category. For flexibility, I created a categoruy called “Editorial” for the posts which should be displayed with the plugin. The problem was : I didn’t want this category to appear anywhere else. That is, this category shoulnd’t appear in the category list (in the left sidebar) or on the Archives Page. It should also be excluded from the post metadata… As I worked on this, I decided to also hide the “Uncategorized” category.

In order to accomplish this, I studied WordPress hooks. WordPress hooks are small functions customizable in one’s WordPress theme which will be called by WordPress core code. The reason behind hooks is to enable some custom code to be called in a given theme, plugin or widget, without modifying the WordPress core code which will be erased every time we upgrade our blog to the newest version of WordPress.

Hiding a category from the post metadata:

The post category at the end of a post are displayed using the following method : the_category(). This method is defined in wp-includes/category-template.php. This method calls get_the_category_list(), itself defined in the same file. This function gets the category list by calling get_the_category() also defined in this category-template.php file. Now, the important part is that this method get_the_category calls a custom filter named get_the_categories.

1
2
3
4
function get_the_category($id = false) {
....
return apply_filters( 'get_the_categories', $categories );
}

Let’s enable the filter get_the_categories and use it to skip the “Editorial” category. We will do that by editing (or creating) the functions.php file of the blog’s theme file.

1
2
3
4
5
6
7
8
9
10
11
12
// We enable the filter
add_filter('get_the_categories', 'hideCategories');
// The filter method (it hides the Editorial category, but you can change that to suit you rneeds
function hideCategories($categories) {
if (is_admin()) return $categories;
$resCategories = array();
foreach ($categories as $category) {
if ($category->name == "Editorial") continue;
$resCategories[] = $category;
}
return $resCategories;
}

Notice that we will not skip the category if we are logged in as super admin (that is to let the category appear in the administration panel)

Hiding a category from the category widget :

The same solution applies to the category widget. After identifying the method used by the category widget which is Walker_CategoryDropdown in the same wp-includes/category-templates.php file, it is clear it calls the list_cats filter somehwere down the road (in start_el). The only problem was, if the category was filtered out in the apply_filter method, it would display an empty line. Therefore, I submitted the following patch to the wordpress team.

In functions.php (in our blog’s theme folder)

1
2
3
4
5
6
7
8
9
// Apply the filter
add_filter('list_cats', 'hideCat');
// The filter code
function hideCat($catName) {
if (is_admin()) return $catName;
if ($catName == "Editorial") return '';
if ($catName == "Uncategorized") return '';
return $catName;
}

If you wish to use this code, download and apply the patch (tested on WordPress 3.1) I hope the patch will make into the later versions by the WordPress dev team.

 

I hope this example of use of hooks and filters will enable you to create your own custom code for WordPress. Happy coding 😉

 

References:

Below are great articles about WordPress development in general and WordPress hooks in particular

Filed under: Computing, Me myself I, PHP, wordpress

Tagged with: , , , , , , , ,

Liked this page?

Tweet it

Subscribe to the RSS feed or sign up for the newsletter now.

Posts related to Using WordPress Hooks by Example – hiding a category

Name (required)

Email (required)

Website

Leave a Reply

  • About Me


    photo of Samantha Halfon Samantha Halfon
    Software Engineer
    blueKiwi software
    Paris, France
    I enjoy playing with my computer(s), listening to Bob Dylan (and related artists) and watching movies (especially if they were directed by Martin Scorsese or John Cassavetes). Sometimes, I play a little guitar... If not doing any of the above, I am either riding a small red bike around Paris, or, making videos. About my videomaking please check out World Wide Angle and its blog.