我想知道是否有办法防止在分类法中添加新类别,本质上是“锁定”分类法。
我正在通过编程注册一个分类法,并通过函数用术语填充它。php和希望它,所以你不能再添加到它。
实现的愿景
这是我的解决方案的最终结果,它工作得很好!感谢所有帮助过我的人。在你面前到处投票!
// 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\' );
SO网友:t31os
如果要独立地将术语添加到分类法中,并且要隐藏UI,为什么不简单地使用register_taxonomy
\'s支持的参数。
capabilities
(数组)(可选)此分类法的功能数组。
默认值:无
“manage\\u terms”-“manage\\u categories”-“edit\\u terms”-“manage\\u categories”-“delete\\u terms”-“manage\\u categories”-“assign\\u terms”-“edit\\u posts”
show_ui
(布尔)(可选)是否生成用于管理此分类的默认UI。
默认值:如果未设置,则默认为公共参数的值
将这些功能设置为一些不存在的功能,您将从本质上阻止用户修改、创建或删除它们。如果您需要能够以传统方式(即通过帖子编辑器)将它们分配给帖子,只需使用assign_terms
价值
Example:
$args = array(
....
\'capabilities\' => array(
\'manage_terms\' => \'foobar\',
\'edit_terms\' => \'foobar\',
\'delete_terms\' => \'foobar\',
\'assign_terms\' => \'foobar\' // <-- change this one to a desired cap if you need to be able to assign them(you could use manage_options for admins only)
),
);
设置
show_ui
如果设置为false,则将阻止显示分类法的任何菜单项。
希望这有助于。。。