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' );
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 yourfunctions.php
file.wp_insert_term()
– allows you to register a termwp_delete_term()
– allows you to delete a termget_terms()
– allows you to retrieve all terms from a given taxonomyThe 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.).If you’re adding terms into the taxonomy independantly and you want to hide the UI, why not simply use two of
register_taxonomy
‘s supported arguments.Set the capabilities to some nonexistent capabilities and you’ll essentially prevent users from being able to modify, create or delete them. If you need to be able to assign them to posts the traditional way(ie. via the post editor), simply use a real capability for the
assign_terms
value.Example:
Set
show_ui
to false and you’ll prevent any menu items being shown for the taxonomy.Hope that helps…