M
M
matros972018-02-05 15:02:52
WordPress
matros97, 2018-02-05 15:02:52

How to prohibit the removal of taxomy?

Hello, tell me how you can disable the removal of taxonomy, is it possible to do this

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
RigidStyle, 2018-06-28
@RigidStyle

add_action( 'delete_term_taxonomy', 'wpse_70758_del_tax', 10, 1 );
add_action( 'edit_term_taxonomies', 'wpse_70758_del_child_tax', 10, 1 );

$undeletable = array( 'категория_один', 'категория_два' );		// <<<<<<<<<< сюда вписывать категории, которые нельзя удалить (category slugs)

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( 'Required category, can not be 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( 'Required category, can not be delete.' );        
    }
}

Optionally adapt to the desired taxonomy.
True, this is a bit of a crutch. But it will not be possible to delete after such protection.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question