2

I am writing a plugin where I have custom taxonomy (category). I want to prevent

  • any user from
  • deleting some of
  • custom categories.

Is there any way how to do so.

Let's say that category with id1-id10 nobody (admin included) can delete.

3 Answers 3

4

If you want to prevent deleting a single or a list of category IDs within the admin, you can prevent so by blocking all requests that delete the category.

There is no hook in wordpress you can make use of to use easily, but there's always a work-around. In my example I use the check_admin_referer and check_ajax_referer hooks (note the typo in the hook name) combined with a check if the request is actually one to delete a category (delete something within the category taxnonomy).

Example Must-Use Plugin: Wordpress Block Category Deletion Example

On deletion of a blocked category, you will get either a You do not have permission to do that. message (Ajax) or a This category is blocked for deletion. message and you need to go back with your browser.

0
1

you could use

<?php $cats = wp_list_categories('echo'=>FALSE);?>

to find the categories, then search for the ones you want. Then you can use

<?php wp_set_object_terms( $object_id, $missing_cat, 'category', TRUE ); ?>

to place them in. You'll need a post that you don't mind having all the categories assigned to, a private post would work well here.

So the final code might look something like:

<?php 
function cats_protector(){
    $current_cats = wp_list_categories('echo'=>FALSE);
    $my_cats = array('list','of','required','categories');

    foreach ($my_cats as $cat){
        if(!in_array($cat, $current_cats){
            wp_set_object_terms( 15, $cat, 'category', TRUE );
        }
    }
}
add_action('init','cats_protector');
?>

This code is straight-from-the-brain, fresh and untested.

It's worth noting that this will not prevent deletion, it will just put them straight back so long as it is called on init.

To prevent deletion, you COULD look at hiding the option with CSS in the admin panel.

0

I do this with a workaround by using the following plugin for loading custom css in the admin panel: http://wordpress.org/extend/plugins/custom-admin-css/ And then hiding the delete option for the required posts/pages or categories.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.