Skip to content

Instantly share code, notes, and snippets.

@brasofilo
Created November 19, 2012 14:08
Show Gist options
  • Save brasofilo/4110831 to your computer and use it in GitHub Desktop.
Save brasofilo/4110831 to your computer and use it in GitHub Desktop.
WordPress plugin : Prevent Categories Deletion
<?php
/*
Plugin Name: Prevent Category Deletion
Plugin URI: http://wordpress.stackexchange.com/q/70758/12615
Description: Prevent deletion of categories. Modify the $undeletable array to suit your setup. Use Category SLUGS.
Author: brasofilo
Version: 1.0
Author URI: http://wordpress.stackexchange.com/users/12615/brasofilo
*/
add_action( 'delete_term_taxonomy', 'wpse_70758_del_tax', 10, 1 );
add_action( 'edit_term_taxonomies', 'wpse_70758_del_child_tax', 10, 1 );
add_filter( 'manage_edit-category_columns', 'wpse_70758_cat_edit_columns' );
add_filter( 'manage_category_custom_column', 'wpse_70758_cat_custom_columns', 10, 3 );
$undeletable = array(
'undeletable'
, 'uncategorized'
, 'other-cat'
);
function wpse_70758_del_tax( $tt_id )
{
global $undeletable;
$term = get_term_by( 'id', $tt_id, 'category' );
if( in_array( $term->slug, $undeletable ) )
wp_die( 'cant delete' );
}
function wpse_70758_del_child_tax( $arr_ids )
{
global $undeletable;
foreach( $arr_ids as $id )
{
$term = get_term_by( 'id', $id, 'category' );
$parent = get_term_by( 'id', $term->parent, 'category' );
if( in_array( $parent->slug, $undeletable ) )
wp_die( 'cant delete' );
}
}
function wpse_70758_cat_edit_columns( $columns )
{
$columns['tt_id'] = 'ID';
$columns['undeletable'] = 'Undeletable';
return $columns;
}
function wpse_70758_cat_custom_columns( $value, $name, $tt_id )
{
if( 'tt_id' == $name )
echo $tt_id;
global $undeletable;
$term = get_term_by( 'id', $tt_id, 'category' );
if( 'undeletable' == $name && in_array( $term->slug, $undeletable ) )
echo '<span style="color:#f00;font-size:5em;line-height:.5em">&#149;</span>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment