Tame the Tags and Keep Search Engines and Users Happy

Tame the Tags and Keep Search Engines and Users Happy

SEO best practice is to let search engines index your site using either categories or tags but NOT both. For most sites that means tags, so management of tags is critical.

In WordPress tags, unlike categories, are an “all or nothing” proposition: if a user can assign tags then they can add tags. This is not conducive to good tag management and leads to tag pages with too few posts, posts appearing on too many tag pages and, well, just too many tags.

In this article, we’ll go through some simple steps to add functionality to a WordPress site that will bring tag creation and tag assignment under control and maintain a WordPress site’s SEO goodness.

The word chaos graffitied onto a wall
Take control of your tagging!

We’re not going to delve into the world of SEO and tags – you can learn more about this here and here. Tags are important for both search engines and site visitors and are used extensively here at WPMU as well as WordPress heavyweight users such as Variety and WordPress themselves, particularly in the Showcase.

Tags are WordPress’ wild frontier. It is a free-for-all in regards to creation and assignment and this can lead to SEO issues as well as usability issues. Just compare the editor-managed Showcase on WordPress.org with the user-managed Plugins Repository to see the difference that a little control can bring.

When managing tags we have three main goals:

  1. Consistency: a single term to cover a topic or subject. For example, SEO vs SEM vs Search Engine Optimisation – the last term also breaks the best practice of using a single word.
  2. Well-populated tag pages: the more posts, the more authority the tag page will have, not just with search engines but also with site visitors
  3. Posts not over-tagged: if a page appears on too many tag pages (because it has been over-tagged) then it makes it much harder for search engines to work out its relevance

To meet these goals, we need to ensure that authors assign an acceptable number of tags from a controlled list and prevent them creating new tags.

To do that, we need to:

  1. Manipulate the post edit screen Tags meta box to remove the facility to add new tags and to make the full list of tags immediately visible
  2. Add a check before saving a post to ensure that the number of assigned tags satisfies both a minimum and maximum requirement

Step 1 – Manipulating the post edit Tags meta box

The Tags meta box uses a single capability (assign-terms) for the ability to both select tags for a post as well as create new tags, so it’s not just a matter of controlling the capabilities to control the adding of a new tag.

It also does not show the tag cloud by default – it only appears when the user clicks on Choose from the most used tags and when it is displayed it only contains those tags that have already been assigned to at least one post.

Rather than replace the built-in meta box with a custom version, we can insert javascript using the admin_footer-post[-new].php actions to get the results we want:

function customise_post_tag_metabox() {
<!-- hide the add function and display tag cloud automatically -->
<script>
(function ($) {
// hide the add tag elements
$(function () {
var new_tag = $("#new-tag-post_tag"),
parent = new_tag.length ? new_tag.parents('p') : false,
howto = parent ? parent.parents("#post_tag").find(".howto") : false ;
if (!parent) return false;
parent.hide();
if (howto) howto.hide();
$("#link-post_tag").hide();
});
// show the tag cloud by triggering the click event on the link
$(window).load(function () {
$("#link-post_tag").trigger("click");
});
})(jQuery);
</script>
<?php
}
add_action('admin_footer-post.php', 'customise_post_tag_metabox');
add_action('admin_footer-post-new.php', 'customise_post_tag_metabox');
?>

This javascript gets inserted into the footer of the post edit pages and executes when the page is loaded. It hides the textbox and the add button and then triggers the click event on the Choose from the most used tags link to show the tag cloud.

FREE EBOOK
Your step-by-step roadmap to a profitable web dev business. From landing more clients to scaling like crazy.

By downloading this ebook I consent to occasionally receive emails from WPMU DEV.
We keep your email 100% private and do not spam.

FREE EBOOK
Plan, build, and launch your next WP site without a hitch. Our checklist makes the process easy and repeatable.

By downloading this ebook I consent to occasionally receive emails from WPMU DEV.
We keep your email 100% private and do not spam.

This will make the tag cloud immediately available but will only show those tags that have already been assigned. We need the tag cloud to include those tags that have yet to be used. To get a full list we need to change the arguments passed to the get_terms function:

function get_all_tags($args){
// only show all tags when in the admin interface
if ( is_admin() ) {
$args['hide_empty'] = false;
$args['number'] = '';
}
return $args;
}
add_filter( 'get_terms_args', 'get_all_tags' );
?>

You’ll notice that as well as overriding the hide_empty argument, the number argument is also set to override WordPress’ default limit of 45.

For a quick test, add this code to your functions.php file, go back to a post edit screen and refresh. You should not see the add tags textbox and button and you should see a full tag cloud.

Step 2 – Adding a pre-save tag check to the post edit screen

Surprisingly, there doesn’t appear to be a plugin in the WordPress Repository to handle our pre-save check for tags. Fortunately, there is the Required Categories plugin, so we’ll stand on the shoulders of Josh Hartman and modify that code to provide our tag check.

Again, more javascript is added via the admin_footer-post[-new].php actions, this time to add a callback function when the Save Draft or Published button is clicked to check the number of tags assigned. If it’s outside of the acceptable range then the save action is cancelled.

function post_tag_check(){
global $post_type;
if($post_type=='post'){
echo "<script>
jQuery(function($){
var min_tags = 2, max_tags = 5;
$('#publish, #save-post').click(function(e){
var assigned_tags = $('.tagchecklist span').length;
if(assigned_tags < min_tags){
alert('You assigned ' + assigned_tags + ' tags. Please assign at least ' + min_tags + ' tags before publishing this post.');
e.stopImmediatePropagation();
return false;
}else if(assigned_tags > max_tags){
alert('You assigned ' + assigned_tags + ' tags. Please assign a maximum of ' + max_tags + ' tags before publishing this post.');
e.stopImmediatePropagation();
return false;
}else{
return true;
}
});
var publish_click_events = $('#publish').data('events')['click'];
if(publish_click_events){
if(publish_click_events.length>1){
publish_click_events.unshift(publish_click_events.pop());
}
}
var save_click_events = $('#save-post').data('events')['click'];
if(save_click_events){
if(save_click_events.length>1){
save_click_events.unshift(save_click_events.pop());
}
}
});
</script>";
}
}
add_action('admin_footer-post.php', 'post_tag_check');
add_action('admin_footer-post-new.php', 'post_tag_check');

Change the min_tags and max_tags variables to suit your own circumstances.

This code is best suited to a plugin and so it’s been bundled up and is available for download. You’ll probably want to tidy it up and perhaps add functionality to it, such as allowing the setting of the tag number range in the Settings area of the admin interface.

Tags can still be added through the Tag menu option under Posts. Access to this option is controlled by the manage_categories capability, which is off by default for contributors and authors, giving you complete control over tag creation.

Tags are a powerful and important component in search engine optimization but that power makes it all too easy to do more harm than good. Taking control of your tags by restricting their creation, requiring their use and controlling their assignment will keep your site on the path to SEO goodness.

If you currently use tags in your posts, how do you keep them under control? Tell us in the comments below.

Photo credit: David Boyle.

Tags: