admin管理员组文章数量:1129094
I've created some custom taxonomies using register_taxonomy, but I want to prevent new terms from being added. I noticed that there is a 'capabilities' argument available in register_taxonomy, is that what I should be using and if so, how would I use it?
Here's some of my code, I'm using a plugin I created to add the taxonomies (hence the public static function part). Is there an easy way to prevent new terms being created for my taxonomy?
Thanks
Osu
public static function register_directory_styles_taxonomy()
{
$labels = array(
'name' => 'Music styles',
'singular_name' => 'Music style',
'search_items' => 'Search music styles',
'all_items' => 'All music styles',
'parent_item' => 'Parent music style',
'edit_item' => 'Edit music style',
'update_item' => 'Update music style',
'add_new_item' => 'Add new music style',
'new_item_name' => 'New music style',
'choose_from_most_used' => 'Choose from most used music styles'
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'rewrite' => false
// 'show_ui' => false
);
register_taxonomy( 'ibmstyles', 'ibmdirectory', $args );
}
I've created some custom taxonomies using register_taxonomy, but I want to prevent new terms from being added. I noticed that there is a 'capabilities' argument available in register_taxonomy, is that what I should be using and if so, how would I use it?
Here's some of my code, I'm using a plugin I created to add the taxonomies (hence the public static function part). Is there an easy way to prevent new terms being created for my taxonomy?
Thanks
Osu
public static function register_directory_styles_taxonomy()
{
$labels = array(
'name' => 'Music styles',
'singular_name' => 'Music style',
'search_items' => 'Search music styles',
'all_items' => 'All music styles',
'parent_item' => 'Parent music style',
'edit_item' => 'Edit music style',
'update_item' => 'Update music style',
'add_new_item' => 'Add new music style',
'new_item_name' => 'New music style',
'choose_from_most_used' => 'Choose from most used music styles'
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'rewrite' => false
// 'show_ui' => false
);
register_taxonomy( 'ibmstyles', 'ibmdirectory', $args );
}
Share
Improve this question
asked Sep 3, 2013 at 9:45
OsuOsu
1,4526 gold badges25 silver badges44 bronze badges
2
|
2 Answers
Reset to default 24You can block addition of new terms with a filter on pre_insert_term
. The source is helpful in working out what you can do.
add_action( 'pre_insert_term', function ( $term, $taxonomy )
{
return ( 'yourtax' === $taxonomy )
? new WP_Error( 'term_addition_blocked', __( 'You cannot add terms to this taxonomy' ) )
: $term;
}, 0, 2 );
It would seem the easiest way to do this has been answered here.
The solution was modifying the taxonomy's capabilities
array:
'capabilities' => array(
'manage_terms' => '',
'edit_terms' => '',
'delete_terms' => '',
'assign_terms' => 'edit_posts'
),
本文标签: How to prevent new terms being added to a custom taxonomy
版权声明:本文标题:How to prevent new terms being added to a custom taxonomy? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736697694a1948267.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
'show_ui' => false
? – gmazzap Commented Sep 3, 2013 at 11:03capabilities
. You can take a additional look at codex.wordpress.org/Function_Reference/register_post_type, because there is a little bit mor information as on the codex.wordpress.org/Function_Reference/register_taxonomy. Besides that, this: wordpress.stackexchange.com/questions/16588/… and this: wordpress.stackexchange.com/questions/21411/… might help you. At the latter especially the link in the answer. – Nicolai Grossherr Commented Sep 3, 2013 at 11:35