UPDATE: For anyone following this thread, the plugin solution does work, but seems to have a problem on my multisite format. I have now tested on 3 individual installs and on 3 multisites. On the multisites, the red “undeletable” marker appears next to the categories in the array, but they can get deleted. This may well be down to a problem with my particular install, as it works properly with newly created subsites.
Original Post:
Working on a multisite & would like a way to protect a couple of default categories from being deleted by site owners.
I’m already using the “members” plugin to define admin roles, but the only option there is “manage categories”, and if I disable that capability they can’t delete categories, but they can’t create new ones either.
Any way to allow new categories but not to delete a few special ones??
Extending @Roman’s answer.
The following was developed and tested in a Multisite environment – local and live WP installs.
Looking at the source of
wp_delete_term
, there are some hooks that are triggered when the function is called.I’m not sure if this is the best way of doing this, but it works.
So, when trying to delete the “Undeletable” category (using the Quick Edit menu), it produces:
As the deletion happens via Ajax, we are breaking its execution and WP dumps that error message (modifying that text deserves a separate Question).
If we use the “Bulk actions“, that
wp_die()
is printed in the screen.But that’s not all. Before the actual deletion (action hook
delete_term_taxonomy
), the child categories of the one we are blocking are “unparented”, line #1772:There’s another hook where this action happens and we can insert an early break to prevent the “unparenting”:
Use this inside a Must Use Plugin and it will be automatically active in the network.
The snapshot above has a custom ID column in the category listing. That’s done with the following code (an here with an extra column marking the “undeletable” categories with a big red bullet):
Full working plugin: https://gist.github.com/4110831
Maybee you can hook to wp_delete_term function with action “delete_term” (see info at: http://adambrown.info/p/wp_hooks/hook/delete_term?version=3.4&file=wp-includes/taxonomy.php) and if such particular term is deleted, you can insert it once again…
this is not so clean solution and users would look bit surprised that term remains still after they clicked “delete link” but it can do the job, actually i think it is only solution…