How to Enable Tags in WordPress Pages

Craig Buckler
Share

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: https://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.