How to allow users to post only in some categories: wordpress

The following code will be useful if you want to allow users to post only in some categories.

function restrict_categories($categories) { 
// If we are in the new/edit post page and not an admin, then restrict the categories
$onPostPage = (strpos($_SERVER['PHP_SELF'], 'post.php') || strpos($_SERVER['PHP_SELF'], 'post-new.php'));
if (is_admin() && $onPostPage && !current_user_can('level_10')) {
$size = count($categories);
for ($i = 0; $i < $size; $i++) {			 			if ($categories[$i]->slug != 'user-created-articles' && $categories[$i]->slug != 'user-created-content' && $categories[$i]->slug != 'user-created-disagreements'&& $categories[$i]->slug != 'user-created-reviews')
return $categories;
add_filter('get_terms', 'restrict_categories');

In the above code it checks the category slug. Admin can post in any categories, but other users can post only in the categories, where slug is either “user-created-articles” or “user-created-articles” or “user-created-disagreements” or “user-created-reviews”

Leave a Reply