Limit the number of categories for posts in WordPress

CHOOSE ONLY ONE CATEGORY WORDPRESS If you need to limit the number of categories used by the authors of your…

September 14, 2015

CHOOSE ONLY ONE CATEGORY WORDPRESS

If you need to limit the number of categories used by the authors of your site, there is a trick for you.

This piece of PHP code limits the number of categories choosen by the authors who are using WordPress to edit post articles in your web sites.

The code works only in the backoffice side of WordPress, to do this it starts with is_admin() check, then it hooks at admin_head action to add some inline javascript.
The script on the browser side (the javascript code is executed by the client browser, not by the server side) uses jQuery framework to count the checkboxes checked in the <div id="#category-all"> tag, and if the count is grater than the predefined max variable value, it stops with a a simple alert() message.

// limit categories wordpress ----------------
if (is_admin()) {
	add_action( 'admin_head', 'admin_inline_js' );
	function admin_inline_js(){
		echo "<script type='text/javascript'>\n";
		echo 'jQuery(document).ready(function($){
			$("#category-all input:checkbox").change(function () {
				var max = 1;
				var count = $("#category-all input:checked").length;
				if (count > max) {
					$(this).prop("checked", "");
					alert("You can choose " + max + " categor" + (max==1?"y":"ies") );
				}
			});
		});';
		echo "\n</script>";
	}
}

You can place this code at the bottom of your functions.php file in your WordPress theme.

Limit categories to 1 for SEO purposes

Limit the number of categories choosen by your authors could be useful to better handle and organize your site navigation, but limit the categories to 1 can be very useful also for Search Engine Optimization, especially if you have the category name in your permalink structure.

If you have, for example, two categories for a post and you have the category slug in your permalink structure your WordPress will creates 2 urls for your post (get_the_permalink() will return 1 url, but your post will be reached with both urls if you navigated throught caategory pages), which is a bad thing:

http://www.your-site.com/category-name-one/title-of-post/
http://www.your-site.com/category-name-two/title-of-post/

This is bad because for Google you’re site is duplicating contents, which is penalized by Google search engine.

Author

I'm a software engineer, an everyday web developer and a maker. I usually build sites with PHP, within or without WordPress. I build Internet of Things with Arduino and ESP8266. I'm the founder of Rockit.it and Dailybest.it and I'm actually the Chief Technical Officer of Better Days web agency.

Recommended

How to add rel=”nofollow” to links with preg_replace()

Adding rel="nofollow" to external link is a good SEO practice.

September 22, 2015

Modify wp_query for a specific category in category.php

Suppose you have a category called “events“, with a custom field with the date of the event, which is different…

April 8, 2016

Social buttons: the fastest way for WordPress, without plugins

NOTE: the code in this post is written for WordPress but you can easily translate it in any language. You’re here…

September 15, 2015

Optimize WordPress, a long list of tips

In the above image you can see your WordPress before reading this post, and after the optimizations you will make…

Remove archive pages in WordPress, how to

I think that in most blogs (that is not all) archive pages are redundant content and there’s no need to…

September 8, 2015

WordPress anyone can register, but no email confirmation

Many times it has happened to me that my WordPress installation properly send emails using wp_mail directly, specifing also the…

August 25, 2015