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:

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.

Craig is a freelance UK web consultant who built his first page for IE2.0 in 1995. Since that time he's been advocating standards, accessibility, and best-practice HTML5 techniques. He's written more than 1,000 articles for SitePoint and you can find him @craigbuckler

Get your free chapter of Level Up Your Web Apps with Go

Get a free chapter of Level Up Your Web Apps with Go, plus updates and exclusive offers from SitePoint.

  • 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!

Related books & courses
Introduction to Wordpress

Available on SitePoint Premium. Check it out now!