如何在分类上设置单独的能力?或者如何重新注册现有的分类法?

时间:2013-07-29 作者:sker

默认分类法category。默认情况下,只有edit_posts 功能可以在后期创建/编辑期间分配类别。

我的角色能力非常有限。我想让此角色中的用户能够在自定义帖子类型创建/编辑期间分配类别,但我无法为他们提供edit_posts 他们应该不能编辑分类法,只能分配它。

我该怎么做?背景\'assign_terms\' => \'read\' 是一个选项,但如何设置该值而不必重新注册分类法?

或者,我如何授予我的低级角色分配分类的权限?

3 个回复
最合适的回答,由SO网友:gmazzap 整理而成

This should works

add_action( \'init\', \'register_category_again\', 1 );

function register_category_again() {
  $user = wp_get_current_user();
  if ( $user->roles[0] != \'your_custom_role\' ) return;
  global $wp_taxonomies;
  unset( $wp_taxonomies[\'category\'] );
  global $wp_rewrite;
  $rewrite = array(
    \'hierarchical\' => true,
    \'slug\' => get_option(\'category_base\') ? get_option(\'category_base\') : \'category\',
    \'with_front\' => ! get_option(\'category_base\') || $wp_rewrite->using_index_permalinks(),
    \'ep_mask\' => EP_CATEGORIES,
  );
  register_taxonomy( \'category\', \'post\', array(
    \'hierarchical\' => true,
    \'query_var\' => \'category_name\',
    \'rewrite\' => $rewrite,
    \'public\' => true,
    \'capabilities\' => array(
      \'manage_terms\'=> \'manage_categories\',
      \'edit_terms\'=> \'manage_categories\',
      \'delete_terms\'=> \'manage_categories\',
      \'assign_terms\' => \'read\'
    ),
    \'show_ui\' => true,
    \'show_admin_column\' => true,
    \'_builtin\' => true,
  ) );
}
SO网友:2ndkauboy

我刚刚发现这个问题,虽然它可能有用,但我对解决方案并不满意。必须有更好的方法来做到这一点,而不必再次注册分类法。还有一个更好的解决方案,我现在正在我的CPT插件中使用。

public function wpse_108219_set_taxonomy_caps( $taxonomy, $object_type, $args ) {
    global $wp_taxonomies;

    if ( \'category\' == $taxonomy && \'cpt_item\' == $object_type ) {
        $wp_taxonomies[ $taxonomy ]->cap->assign_terms = \'edit_cpt_items\';
    }

}

add_filter( \'registered_taxonomy\', \'wpse_108219_set_taxonomy_caps\' ), 10, 3 );
在本例中,我设置assign_terms 自定义岗位类型的能力cpt_item 到自定义功能edit_cpt_items 它允许任何具有此功能的用户将类别分配给CPT。

我希望这种更清洁的解决方案也适用于您。

SO网友:James

您还可以在注册分类法之前筛选core的类别参数:

<?php
/*
 * Set the capabilities for the category taxonomy before it\'s registered.
 *
 * @param array  $args        Array of arguments for registering a taxonomy.
 * @param array  $object_type Array of names of object types for the taxonomy.
 * @param string $taxonomy    Taxonomy key.
 */
function wpse_108219_register_taxonomy_args( $args, $taxonomy, $object_type ) {

    if ( \'category\' === $taxonomy ) {

        $args[\'capabilities\'] = array(
            \'manage_terms\' => \'manage_categories\',
            \'edit_terms\'   => \'manage_categories\',
            \'delete_terms\' => \'manage_categories\',
            \'assign_terms\' => \'read\',
        );

    }

    return $args;
}

add_filter( \'register_taxonomy_args\', \'wpse_108219_register_taxonomy_args\', 10, 3 );

结束

相关推荐

Looping taxonomy in taxonomy?

所以我有一个CPT的参考文献(就像一个音乐的字典格式)。其中有两种分类法:medium_reference 和reference_letter.这个reference_letter 列出字母A-Z,而medium_reference 按引用类型(人员、术语、公司)列出。我想知道是否有可能遍历每个字母,然后在该循环中遍历各个术语。类似这样的情况(无论如何都不可能):$reference_letters = get_the_terms($post->ID,\'reference_letter\');&#