添加、编辑具有自定义角色的特定CPT

时间:2022-02-11 作者:Ben

谁可以在角色和能力方面帮助我?我被困在这个问题上,我需要一些帮助。

我想要一个角色,只能添加,删除编辑等一个特定的CPT。

到目前为止我拥有的

add_action(\'init\', function(){  
    register_post_type( \'catalog\',
        array(
            \'capability_type\'   => \'manage_catalog\',
            \'labels\' => array(
                \'name\'          => \'Catalogus\',
                \'add_new\'       => \'Nieuw item\',
                \'add_new_item\'  => \'Nieuw item toevoegen\',
                \'new_item\'      => \'Nieuw item\',
            ),
            \'public\' => true,
            \'query_var\' => true,
            \'show_ui\' => true,
            \'show_in_rest\' => false,
            \'menu_icon\' => \'dashicons-store\',
            \'menu_position\' => 2,
            \'supports\' => array(
                \'title\',
                \'thumbnail\',
            ),
        )
    );

add_action(\'load-themes.php\', function(){
    global $pagenow;

    if(\'themes.php\' == $pagenow && isset($_GET[\'activated\'])){ 
        $role = get_role(\'subscriber\');
        $role->add_cap(\'manage_catalog\'); 
    }else{
        //Remove the capacity when theme is deactivate
        $role = get_role(\'subscriber\');
        $role->remove_cap(\'manage_catalog\');
    }
});
使用上面的代码,我无法在管理端的菜单中看到CPT目录。代码中缺少什么?

1 个回复
SO网友:Artemy Kaydash

没有名为加载主题的钩子。WordPress中的php。可以用来添加自定义功能的挂钩之一是在设置主题之后。

而且,没有必要使用\'themes.php\' == $pagenow && isset($_GET[\'activated\']) 那里的条件。你想通过这种方式实现什么?

请尝试改用此代码:

add_action( \'after_setup_theme\', function() {
  $role = get_role( \'subscriber\' );

  $singular = \'catalog\';
  $plural = \'catalogs\';

  $role->add_cap( "edit_{$singular}" ); 
  $role->add_cap( "edit_{$plural}" ); 
  $role->add_cap( "edit_others_{$plural}" ); 
  $role->add_cap( "publish_{$plural}" ); 
  $role->add_cap( "read_{$singular}" ); 
  $role->add_cap( "read_private_{$plural}" ); 
  $role->add_cap( "delete_{$singular}" ); 
  $role->add_cap( "delete_{$plural}" );
  $role->add_cap( "delete_private_{$plural}" );
  $role->add_cap( "delete_others_{$plural}" );
  $role->add_cap( "edit_published_{$plural}" );
  $role->add_cap( "edit_private_{$plural}" );
  $role->add_cap( "delete_published_{$plural}" );
} );

相关推荐

未定义的偏移量:1067行的>[...]/wp-includes/capabilities.php中的0

嘿,我在我的localhost设置中得到了这个错误消息,但只有在启用Genesis框架的情况下;WordPress二十一行。当我想创建一个新帖子时,就会发生这种情况。如果我刷新页面,错误会重复,但帖子本身会被创建,一切似乎都很好。有人知道这是什么原因吗?Notice: Undefined offset: 0 in /var/www/secret/htdocs/wp-includes/capabilities.php on line 1067 Notice: Undefined offset: 0