{"id":122048,"date":"2013-09-26T11:30:13","date_gmt":"2013-09-26T15:30:13","guid":{"rendered":"http:\/\/wpmu.org\/?p=122048"},"modified":"2018-08-21T16:52:30","modified_gmt":"2018-08-21T16:52:30","slug":"tame-tags-happy-search-engines-users","status":"publish","type":"post","link":"https:\/\/wpmudev.com\/blog\/tame-tags-happy-search-engines-users\/","title":{"rendered":"Tame the Tags and Keep Search Engines and Users Happy"},"content":{"rendered":"<p>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.<\/p>\n<p>In WordPress tags, unlike categories, are an &#8220;all or nothing&#8221; 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.<\/p>\n<p>In this article, we&#8217;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&#8217;s SEO goodness.<\/p>\n<figure id=\"attachment_122068\" class=\"wp-caption aligncenter\" data-caption=\"true\"><a rel=\"lightbox[122048]\" class=\"blog-thumbnail\" href=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2013\/09\/chaos.jpg\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"size-ratio-large wp-image-122068\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2013\/09\/chaos-700x241.jpg\" alt=\"The word chaos graffitied onto a wall\" width=\"700\" height=\"241\" \/><\/a><figcaption class=\"wp-caption-text\">Take control of your tagging!<\/figcaption><\/figure>\n<p>We&#8217;re not going to delve into the world of SEO and tags \u2013 you can learn more about this <a title=\"Categories and Tags: Which One to Use and When to Use It\" href=\"https:\/\/wpmudev.com\/blog\/categories-and-tags-which-one-to-use-and-when-to-use-it\/\" target=\"_blank\" rel=\"noopener\">here<\/a> and <a title=\"Categories and Tags: SEO Best Practices For Sorting Your WordPress Content\" href=\"https:\/\/wpmudev.com\/blog\/categories-and-tags-seo-best-practices-for-sorting-your-wordpress-content\/\" target=\"_blank\" rel=\"noopener\">here<\/a>. 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.<\/p>\n<p>Tags are WordPress&#8217; 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 <a title=\"Look at the small selection of tags used in the WordPress Shwocase\" href=\"http:\/\/wordpress.org\/showcase\/\" rel=\"noopener\" target=\"_blank\">Showcase<\/a> on WordPress.org with the user-managed <a title=\"Example of entry with too many and too similar tags\" href=\"http:\/\/wordpress.org\/plugins\/theme-check\/\" rel=\"noopener\" target=\"_blank\">Plugins Repository<\/a> to see the difference that a little control can bring.<\/p>\n<p>When managing tags we have three main goals:<\/p>\n<ol>\n<li><strong>Consistency<\/strong>: a single term to cover a topic or subject. For example, SEO vs SEM vs Search Engine Optimisation \u2013 the last term also breaks the best practice of using a single word.<\/li>\n<li><strong>Well-populated tag pages<\/strong>: the more posts, the more authority the tag page will have, not just with search engines but also with site visitors<\/li>\n<li><strong>Posts not over-tagged<\/strong>: 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<\/li>\n<\/ol>\n<p>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.<\/p>\n<p>To do that, we need to:<\/p>\n<ol>\n<li>Manipulate the post edit screen Tags meta box to remove the facility to add new tags and to make the full list\u00a0of tags immediately visible<\/li>\n<li>Add a check before saving a post to ensure that the number of assigned tags satisfies both a minimum and maximum requirement<\/li>\n<\/ol>\n<h2>Step 1 \u2013 Manipulating the post edit Tags meta box<\/h2>\n<p>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&#8217;s not just a matter of controlling the capabilities to control the adding of a new tag.<\/p>\n<p>It also does not show the tag cloud by default \u2013 it only appears when the user clicks on <em>Choose from the most used tags<\/em> and when it is displayed it only contains those tags that have already been assigned to at least one post.<\/p>\n<p>Rather than replace the built-in meta box with a custom version, we can insert javascript using the <em>admin_footer-post[-new].php<\/em> actions\u00a0to get the results we want:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nfunction customise_post_tag_metabox() {\r\n&lt;!-- hide the add function and display tag cloud automatically --&gt;\r\n&lt;script&gt;\r\n(function ($) {\r\n\/\/ hide the add tag elements\r\n$(function () {\r\nvar new_tag = $(&quot;#new-tag-post_tag&quot;),\r\nparent = new_tag.length ? new_tag.parents(&#039;p&#039;) : false,\r\nhowto = parent ? parent.parents(&quot;#post_tag&quot;).find(&quot;.howto&quot;) : false ;\r\nif (!parent) return false;\r\nparent.hide();\r\nif (howto) howto.hide();\r\n$(&quot;#link-post_tag&quot;).hide();\r\n});\r\n\/\/ show the tag cloud by triggering the click event on the link\r\n$(window).load(function () {\r\n$(&quot;#link-post_tag&quot;).trigger(&quot;click&quot;);\r\n});\r\n})(jQuery);\r\n&lt;\/script&gt;\r\n&lt;?php\r\n}\r\nadd_action(&#039;admin_footer-post.php&#039;, &#039;customise_post_tag_metabox&#039;);\r\nadd_action(&#039;admin_footer-post-new.php&#039;, &#039;customise_post_tag_metabox&#039;);\r\n?&gt;\r\n<\/pre>\n<p>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 <em>Choose from the most used tags<\/em> link to show the tag cloud.<\/p>\n<p>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 <em>get_terms<\/em> function:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nfunction get_all_tags($args){\r\n\/\/ only show all tags when in the admin interface\r\nif ( is_admin() ) {\r\n$args&#x5B;&#039;hide_empty&#039;] = false;\r\n$args&#x5B;&#039;number&#039;] = &#039;&#039;;\r\n}\r\nreturn $args;\r\n}\r\nadd_filter( &#039;get_terms_args&#039;, &#039;get_all_tags&#039; );\r\n?&gt;\r\n<\/pre>\n<p>You&#8217;ll notice that as well as overriding the <em>hide_empty<\/em> argument, the number argument is also set to override WordPress&#8217; default limit of 45.<\/p>\n<p>For a quick test, add this code to your <em>functions.php<\/em> 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.<\/p>\n<h2>Step 2 \u2013 Adding a pre-save tag check to the post edit screen<\/h2>\n<p>Surprisingly, there doesn&#8217;t appear to be a plugin in the WordPress Repository to handle our pre-save check for tags. Fortunately, there is the <a title=\"Visit the Required Categories plugin\" href=\"http:\/\/www.warpconduit.net\/wordpress-plugins\/require-post-category\/\" rel=\"noopener\" target=\"_blank\">Required Categories plugin<\/a>, so we&#8217;ll stand on the shoulders of <a title=\"Visit Josh Hartman's site\" href=\"http:\/\/www.warpconduit.net\/\" rel=\"noopener\" target=\"_blank\">Josh Hartman<\/a> and modify that code to provide our tag check.<\/p>\n<p>Again, more javascript is added via the <em>admin_footer-post[-new].php<\/em> 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&#8217;s outside of the acceptable range then the save action is cancelled.<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nfunction post_tag_check(){\r\nglobal $post_type;\r\nif($post_type==&#039;post&#039;){\r\necho &quot;&lt;script&gt;\r\njQuery(function($){\r\nvar min_tags = 2, max_tags = 5;\r\n$(&#039;#publish, #save-post&#039;).click(function(e){\r\nvar assigned_tags = $(&#039;.tagchecklist span&#039;).length;\r\nif(assigned_tags &lt; min_tags){\r\nalert(&#039;You assigned &#039; + assigned_tags + &#039; tags. Please assign at least &#039; + min_tags + &#039; tags before publishing this post.&#039;);\r\ne.stopImmediatePropagation();\r\nreturn false;\r\n}else if(assigned_tags &gt; max_tags){\r\nalert(&#039;You assigned &#039; + assigned_tags + &#039; tags. Please assign a maximum of &#039; + max_tags + &#039; tags before publishing this post.&#039;);\r\ne.stopImmediatePropagation();\r\nreturn false;\r\n}else{\r\nreturn true;\r\n}\r\n});\r\nvar publish_click_events = $(&#039;#publish&#039;).data(&#039;events&#039;)&#x5B;&#039;click&#039;];\r\nif(publish_click_events){\r\nif(publish_click_events.length&gt;1){\r\npublish_click_events.unshift(publish_click_events.pop());\r\n}\r\n}\r\nvar save_click_events = $(&#039;#save-post&#039;).data(&#039;events&#039;)&#x5B;&#039;click&#039;];\r\nif(save_click_events){\r\nif(save_click_events.length&gt;1){\r\nsave_click_events.unshift(save_click_events.pop());\r\n}\r\n}\r\n});\r\n&lt;\/script&gt;&quot;;\r\n}\r\n}\r\nadd_action(&#039;admin_footer-post.php&#039;, &#039;post_tag_check&#039;);\r\nadd_action(&#039;admin_footer-post-new.php&#039;, &#039;post_tag_check&#039;);\r\n<\/pre>\n<p>Change the <em>min_tags<\/em> and <em>max_tags<\/em> variables to suit your own circumstances.<\/p>\n<p>This code is best suited to a plugin and so it&#8217;s been bundled up and is <a href=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2013\/09\/tag_taming_plugin.zip\" target=\"_blank\">available for download<\/a>. You&#8217;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.<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<p><strong>If you currently use tags in your posts, how do you keep them under control? Tell us in the comments below.<\/strong><\/p>\n<p>Photo credit: <a title=\"Visit David Boyle's Flickr site\" href=\"http:\/\/www.flickr.com\/photos\/beglen\/\" rel=\"noopener\" target=\"_blank\">David Boyle<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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.<\/p>\n<p>In this article, we&#8217;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&#8217;s SEO goodness.<\/p>\n","protected":false},"author":262394,"featured_media":171883,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"blog_reading_time":"","wds_primary_category":0,"wds_primary_tutorials_categories":0,"footnotes":""},"categories":[263],"tags":[35,45],"tutorials_categories":[],"class_list":["post-122048","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-seo","tag-tags"],"_links":{"self":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/122048","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/users\/262394"}],"replies":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/comments?post=122048"}],"version-history":[{"count":2,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/122048\/revisions"}],"predecessor-version":[{"id":173908,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/122048\/revisions\/173908"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media\/171883"}],"wp:attachment":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media?parent=122048"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/categories?post=122048"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tags?post=122048"},{"taxonomy":"tutorials_categories","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tutorials_categories?post=122048"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}