Luutaa Technologies - Developer Blog

Prevent user from deleting categories in wordpress

WordPress is like a heaven for the programmer and for designer both. With the introduction of wordpress, blogging moves to next level . All of us know that wordpress is highly customizable and in any manner but there are several things which becomes pain for the beginner developer or wordpress programmer.

One of the reason for the pain is, “Categories”. Categories helps us in grouping the posts so it is easy for manage and beneficial for reader and for programmer also. But sometimes it happens that programmer creates a category which is treated as top level category , which we dont want to delete anyhow , so as to maintain the smooth functionality of our blog / site,  as we can  create custom files for categories on the basis of category slug / ID and so on.

So overcome the fear of category deletion , we can write some piece of code in our theme’s functions.php file , which will helps us to overcome the fear of category deletion .

The piece of code for preventing the category deletion is provided below :

<?php
// pass IDs of categories to block as arra
$cat_array = array(4, 3, 5); // pass the id's of category which you want to prevent from deletion
return blockCategoriesDeletionPlugin::bootstrap( $cat_array ); 

class blockCategoriesDeletionPlugin {
	/**
	* @var blockCategoriesDeletionPlugin
	*/
	static $instance;
	private $categoryIDs = array();
	public static function bootstrap(array $categoryIDs) {
		if (null===self::$instance) {
			self::$instance = new self($categoryIDs);
		} else {
			throw new BadFunctionCallException(sprintf('Plugin %s already instantiated', __CLASS__));
		}
		return self::$instance;
	}

	private function isCategoryDeleteRequest() {
		$notAnCategoryDeleteRequest =
		empty($_REQUEST['taxonomy'])
		|| empty($_REQUEST['action'])
		|| $_REQUEST['taxonomy'] !== 'category'
		|| !( $_REQUEST['action'] === 'delete' || $_REQUEST['action'] === 'delete-tag');

		$isCategoryDeleteRequest = !$notAnCategoryDeleteRequest;

		return $isCategoryDeleteRequest;
	}

	public function __construct(array $categoryIDs) {
		$this->categoryIDs = $categoryIDs;
		if ($this->isCategoryDeleteRequest()) {
			add_filter('check_admin_referer', array($this, 'check_referrer'), 10, 2);
			add_filter('check_ajax_referer', array($this, 'check_referrer'), 10, 2);
		}
	}

	private function blockCategoryID($categoryID) {
		return in_array($categoryID, $this->categoryIDs);
	}

	/**
	* @-wp-hook check_admin_referer
	* @-wp-hook check_ajax_referer
	*/

	public function check_referrer($action, $result) {

		if (!$this->isCategoryDeleteRequest()) {
			return;
		}

		$prefix = 'delete-tag_';
		if (strpos($action, $prefix) !== 0)
			return;

		$actionID = substr($action, strlen($prefix));
		$categoryID = max(0, (int) $actionID);

		if ($this->blockCategoryID($categoryID)) {
			wp_die(__('This category is blocked for deletion.'));
		}
	}
}
?>

As you can see , you just have to pass the array of category id’s in the first line and then no one is able to delete the defined category from wp-admin .

Leave a Reply:

Your email address will not be published. Required fields are marked *