Stop Deleting WordPress Categories

Hi Guys ,

Hope that you had a great weekend :slight_smile:

I want to add ‘Two Categories’ from admin panel ,

Which no-body , including Admin itself can not delete those category .

If admin tries to delete / bulk delete that categories , no need to show any error , just ‘skip them’ by code and nothing happens ..

Actually there may be 100 s of category , and admin can bulk select and delete that categories , but I want to skip specific two categories (ex- id=> 61 and 62) from being deleting , however other 98 will be deleted ..

Thanks

  • Raitis
    • Site Builder, Child of Zeus

    Hi Jude ,

    Thanks for your reply ,

    Admin is actually deleting categories from ‘Admin Panel’ –

    /wp-admin/edit-tags.php?taxonomy=goals-category&post_type=goal&paged=1

    By selecting all categories and deleting by bulk actions ,

    WordPress default functionality is doing its work pretty good , I just want to hook that functionality (Bulk Deleting) to avoid two ids (61,62)

    Thanks

  • Jude
    • DEV MAN

    Hey Raitis

    There is no way to do this out of the box. Maybe you can use the screen options tab on the top right and limit the number on the screen to say 25, and when you come across those categories just manually unselect them.

    This is of course just a workaround.

    Jude

  • Raitis
    • Site Builder, Child of Zeus

    Hi Jude ,

    Thanks for your reply ,

    Actually , these two categories will have many posts , and it will become too much worse if it is deleted accidentally / my mistake . So please provide any best solution for it so that we can sleep in night well without tension :slight_smile: . Please check with @Ashok or other developers too if someone else too can help us in finding its best solution .

    Thanks

  • Ash
    • Code Norris

    Hey @Raitis

    To be honest there is no way to skip silently a or multiple categories. If you wanted to skip one category, there is an easy way to do it. Just make that category the default category from Settings > Writing.

    As you have two categories to make undeletable, you must show a error message if someone wants to delete those as bulk delete operation. For single delete it will still show an auto generated error message. Here is the code you can use in that case:

    add_action( 'pre_delete_term', 'pre_delete_term_cb', 10, 2 );
    function pre_delete_term_cb( $arr_ids, $taxonomy )
    {
    $undeletable = array( 61, 62 );
    if( in_array( $arr_ids, $undeletable ) )
    wp_die( "You can't delete category A and B. Please deselect those and try again." );
    }

    You can add those codes in your functions.php in the theme, if you think your theme won’t be changed. Otherwise mu-plugins is the best solution. To use mu-plugins, go to /wp-content/ and find the folder with name ‘mu-plugins’. If there is no folder in that name, then create a folder, name it ‘mu-plugins’, create a file inside that, give any name you like and paste the code in there. You don’t need to activate that plugin. Mu-plugins means must use plugins, so it will be activated automatically always. If you use mu-plugins then add a php start tag at the beginning of the code.

    Hope it helps :slight_smile: Please feel free to ask more question if you have any.

    Cheers

    Ash

  • Raitis
    • Site Builder, Child of Zeus

    Hi @Ashok,

    Thank you very much for this awesome solution .

    It is working pretty good , and I also learn about a new thing ‘ MU-PLUGINS’ ;

    This is the result after adding this code –

    – When I am deleting any 61 or 62 term individually , it is showing this error –

    “An unidentified error has occurred”

    It is ok ,

    – When I am deleting multiple categories

    It is showing error which I write in die();

    – I agree that silently skipping would be great , but it is also working much better than expacted .

    Thanks again @Ashok & @Jude for your help in this

  • Ash
    • Code Norris

    Hello @Raitis

    I hope you are well today.

    – When I am deleting multiple categories

    It is showing error which I write in die();

    Yes, this is how it is supposed to do. In fact, there is no hook that we can use to skip category IDs, I have checked WordPress source code yesterday. So, we could not skip silently and we needed to force the function to be stopped. There is currently no other way I am afraid.

    Cheers

    Ash

  • Raitis
    • Site Builder, Child of Zeus

    Hi @Ashok ,

    Thanks for your awesome help in this , It is working very nicely . I used same for other post_types taxonomy also , as all categories of every posts types uses same database table , I just pushed more category ids in that array , and thats cool to see its working perfectly :slight_smile:

    Thanks