【问题标题】:how to add custom role function on wordpress?如何在wordpress上添加自定义角色功能?
【发布时间】:2017-06-21 10:09:27
【问题描述】:

对不起,我想做官方网站。这是最重要的角色:

  • 首先(作者),用户可以发布他们的内容(我知道 wordpress 已经有这个功能)。作者不能公开发布内容。

  • 第二个(验证者,我感谢那个名字),用户可以看到第一个角色(作者)的内容并添加评论。如果内容足够好,内容可以传递给第三个角色。

  • 第三。 (发布),用户只能看到第一个角色的内容。添加发布它

如何在 wordpress 上添加自定义功能角色?

ps:对不起我的英语不好。

【问题讨论】:

标签: javascript php wordpress


【解决方案1】:

如果您想让自己的角色变得容易,最好先创建一个插件。检查 github 上的一些插件样板,比如这个 https://github.com/DevinVinson/WordPress-Plugin-Boilerplate

然后创建编辑你的主文件,首先定义行,然后创建一个挂钩,在安装插件时激活该行。从那里您将能够使用您的自定义角色

$newrole = add_role(
     'basic_contributor',
      __( 'Basic Contributor' ),
      array(
      'read'         => true,  
      'edit_posts'   => true,
      'delete_posts' => false, 
      'publish_post'=> true
    )
);

function add_roles_on_plugin_activation() {
       add_role( 'custom_role', 'Custom Subscriber', array( 'read' => true, 
       'level_0' => true ) );
   }

register_activation_hook( __FILE__, 'add_roles_on_plugin_activation' );

如果这可能有点复杂,那么从那里安装来自 wordpress 存储库的自定义用户行插件,如果我正在为我使用的不同功能或权限定制已经开发的插件,你将能够在某些情况下为我使用

if(current_user_can("basic-contributor") && is_user_logged_in(){

    //do this
    //show this
}

【讨论】:

    【解决方案2】:

    您可以通过插件https://wordpress.org/plugins/user-role-editor/ 创建和更改自定义用户角色的功能

    如果您对 wordpress 有深入了解,您可以创建自定义用户角色并为特定用户角色设置功能。

    $result = add_role(
    'basic_contributor',
    __( 'Basic Contributor' ),
    array(
        'read'         => true,  // true allows this capability
        'edit_posts'   => true,
        'delete_posts' => false, // Use false to explicitly deny
    )
    );
    if ( null !== $result ) {
        echo 'Yay! New role created!';
    }
    else {
        echo 'Oh... the basic_contributor role already exists.';
    }
    

    https://codex.wordpress.org/Function_Reference/add_role

    https://codex.wordpress.org/Roles_and_Capabilities

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-10
      • 2021-02-17
      • 2021-05-07
      • 1970-01-01
      • 2014-09-05
      • 1970-01-01
      • 2012-01-14
      • 2016-03-07
      相关资源
      最近更新 更多