WordPress: role-based restriction of the ability to post (only specific category allowed)

I was looking for a way to allow a user to post only in a specific category and hide all the others. I found nothing on the internet. So here’s what I finally managed to achieve. This actually hides all the categories except the specified one, applies the specific category automatically when doing auto save or saving draft (to avoid having the post autosaved in default category).

Of course, put this in functions.php. Change the two global variables according to your needs.

A few notes:
The role created inherits its capabilities from the author role. You can either change that to an existing default WordPress role or manually add the capabilities you need.
If you ever need to reset all roles to the default state, you can install the plugin User Role Editor, then add this in config.php: define('URE_SHOW_ADMIN_ROLE', 1);. After having done so, go to Users > User Role Editor and click “Reset“. The get_terms portion is adapted from this article.

Lascia un commento

Il tuo indirizzo email non sarĂ  pubblicato. I campi obbligatori sono contrassegnati *