How to Enable Tags in WordPress Pages

Contributing Editor

Tags have been supported in WordPress since version 2.3 was released in 2007. When used well, they can be a more effective form of navigation than categories or menus. But why has it never been possible to tag WordPress pages?

Fortunately, WordPress provides the tools to help us enable tags in pages and any other type of post. Beneath the surface, WordPress treats pages, posts and other content in much the same way; a page is just a custom post type. Therefore, enabling tags is simply a matter of saying “hey WordPress, I’d like to use tags on my pages and don’t forget to include them in the tag cloud!”

Let’s convert that to code you can insert into your theme’s functions.php file (wp-content/themes/<themename>/functions.php):

// add tag support to pages
function tags_support_all() {
	register_taxonomy_for_object_type('post_tag', 'page');
}

// ensure all tags are included in queries
function tags_support_query($wp_query) {
	if ($wp_query->get('tag')) $wp_query->set('post_type', 'any');
}

// tag hooks
add_action('init', 'tags_support_all');
add_action('pre_get_posts', 'tags_support_query');

Simple. If you have further custom post types which require tags, you’ll need to add register_taxonomy_for_object_type calls for each — the second argument is the type name.

Those running several WordPress sites or a network may find it easier to convert the code to a plugin so it can be enabled and disabled accordingly. In essence, that’s a matter of adding the code above to a suitably-named plugin file, i.e. wp-content/plugins/enable-tags.php, and placing comments at the top:

<?php
/*
Plugin Name: Enable Tags in WordPress Pages
Plugin URI: http://www.sitepoint.com/
Description: Enables tags in all content
Version: 1.0
Author: Craig Buckler
Author URI: http://twitter.com/craigbuckler
License: Free to use and adapt
*/

// add tag support to pages
// ... rest of code ...

I hope you find it useful. Please use and adapt the code as you like in your own projects — a link back to this article is appreciated.

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

  • http://niteodesign.com Blake Petersen

    Love it, thanks Craig!

    • http://www.optimalworks.net/ Craig Buckler

      You’re welcome, Blake.

  • Debasis Sabat

    i found a Cool plugin which allow to do taging in WP pages same like post. Refer http://wordpress.org/extend/plugins/page-tagger/

  • http://www.wpmagi.com Dave

    Nice trick there Craig, thanks a lot!