I was wondering if there was a way to prevent the addition of new categories into a taxonomy, essentially ‘locking’ the taxonomy.

I’m programatically registering a taxonomy and filling it with terms via functions.php and would like it so you cannot add anymore into it.

The Vision, REALIZED…

Heres how my solution ended up looking, and it works great! THANKS to everyone who helped. Upvotes all over the place up in your face!

// some stuff happens before this...
    $labels = array(
        'name' => _x( 'Attendees', 'taxonomy general name' ),
        'singular_name' => _x( 'Attendee', 'taxonomy singular name' ),
        'search_items' =>  __( 'Search Attendees' ),
        'all_items' => __( 'All Attendees' ),
        'edit_item' => __( 'Edit Attendee' ), 
        'update_item' => __( 'Update Attendee' ),
        'add_new_item' => __( 'Add New Attendee' ),
        'new_item_name' => __( 'New Attendee Name' ),
        'menu_name' => __( 'Attendees' )
    );
    $rewrite = array(
        'slug' => 'attendee'
    );
    $capabilities = array(
        'manage_terms' => 'nobody',
        'edit_terms' => 'nobody',
        'delete_terms' => 'nobody',
        'assign_terms' => 'nobody'
    );
    $args = array(
        'hierarchical' => true,
        'labels' => $labels,
        'show_ui' => true,
        'query_var' => 'attendee',
        'rewrite' => $rewrite,
        'capabilities' => $capabilities
    );
    register_taxonomy('attendees', 'meetings', $args);
}
add_action( 'init', 'todo_create_taxonomies', 1);
function todo_register_attendees() {
    $users = get_users();
    foreach ( $users as $user ) {
        wp_insert_term( $user->display_name, 'attendees', array('description'=> $user->display_name, 'slug' => $user->user_nicename) );
        $lockdown[] = $user->user_nicename;
    }
    $terms = get_terms('attendees', array('get' => 'all') );
    foreach ($terms as $term) {
        if ( !in_array($term->slug, $lockdown) ) {
            wp_delete_term( $term->term_id, 'attendees' );
            $message = new WP_Error('force_terms', __('Only Registered Users can be Attendees, ' . $term->name . ' has been deleted.'));
            if ( is_wp_error($message) ) { ?>
                <div id="aphid-error-<?php echo $message->get_error_code(); ?>" class="error aphid-error"> 
                    <p><strong><?php echo $message->get_error_message(); ?></strong></p>
                </div>
            <?php }
        }
    }
}
add_action( 'admin_notices', 'todo_register_attendees' );

2 s
2

Categories, Tags & Taxonomies

First i want to make this one clear: Everything is a Taxonomy. Tags is a non-hierarchical, Categories a hierarchical and both are built-in taxonomies. Same goes for eg. post formats (aside, chat, etc.). It’s the same concept as with post-types (post, page, attachment, nav_menu_item, etc. are all just built in post types).

Everything inside one of those Taxonomies is a Term. For eg. (inside post formats) “aside”, “quote”, “audio”.

Codex Links

  • register_taxonomy() – allows you to register a taxonomy from ex. inside your functions.php file.
  • wp_insert_term() – allows you to register a term
  • wp_delete_term() – allows you to delete a term
  • get_terms() – allows you to retrieve all terms from a given taxonomy

The Concept

The following is for your functions.php file. This triggers on every page request. You could improve it – using the Transients API – to trigger on given timestamps (eg. twice daily, hourly, etc.).

function wpse14350_cleanup_taxonomy_terms()
{
    // Define your terms inside this array
    $predefined_terms = array(
         'term A'
        ,'term B'
        ,'term C'
    );
    // Name your taxonomy here
    $predefined_taxonomy = 'category';

    $all_terms_inside_tax = get_terms( 
         $predefined_taxonomy
        ,array(
             'hide_empty'   => false
            ,'taxonomy'     => $predefined_taxonomy
        ) 
    );

    foreach ( $all_terms_inside_tax as $term )
    {
        if ( ! in_array( $term->name, $predefined_terms ) )
            wp_delete_term( $term->term_id, $predefined_taxonomy );
    }
}
add_action( 'init', 'wpse14350_cleanup_taxonomy_terms' );

Leave a Reply

Your email address will not be published. Required fields are marked *