【问题标题】:WordPress Create Custom CapabilityWordPress 创建自定义功能
【发布时间】:2013-01-15 01:54:01
【问题描述】:

我正在开发一个购物车插件,并计划为客户创建一个新的用户角色。

我的问题是如何创建自定义功能,以便我可以将这些自定义功能分配给新用户角色,this answer 提供了一种创建新功能的方法,但这只是原始功能的新名称。

谁能解释如何创建一个全新的功能来控制一些自定义功能?

【问题讨论】:

  • @maiorano84 谢谢,我已经搜索了一段时间,找不到满足我确切要求的东西。
  • “让我为你谷歌一下”的讽刺意味是谷歌把我带到了这里
  • 嗨。这个你搞清楚了吗?我也是一样的泡菜。我想要仅在 WooCommerce 中编辑订单状态的功能。

标签: wordpress user-roles capability


【解决方案1】:

您可以通过插件创建自定义角色和功能。自定义代码提供了两个选项,或者您可以使用现有插件。

对于自定义代码: https://wordpress.stackexchange.com/questions/35165/how-do-i-create-a-custom-role-capability

使用现有插件: User Roles and Capabilities

【讨论】:

    【解决方案2】:

    您首先应该了解,Wordpress 用户角色就像一组功能一样简单。话虽如此,既然你说你正在创建一个插件,我想你对编码并不陌生,因此不害怕编写你的解决方案而不是为此使用另一个插件。

    此代码应帮助您创建新用户角色并向其添加自定义功能。

    <?php
    
    // create a new user role
    
    function wpeagles_example_role()
    {
        add_role(
            'example_role',
            'example Role',
            [
                // list of capabilities for this role
                'read'         => true,
                'edit_posts'   => true,
                'upload_files' => true,
            ]
        );
    }
    
    // add the example_role
    add_action('init', 'wpeagles_example_role');
    

    要向此用户角色添加自定义功能,请使用以下代码:

    //adding custom capability
    <?php
    function wpeagles_example_role_caps()
    {
        // gets the example_role role object
        $role = get_role('example_role');
    
        // add a custom capability 
        // you can remove the 'edit_others-post' and add something else (your     own custom capability) which you can use in your code login along with the current_user_can( $capability ) hook.
        $role->add_cap('edit_others_posts', true);
    }
    
    // add example_role capabilities, priority must be after the initial role     definition
    add_action('init', 'wpeagles_example_role_caps', 11);
    

    进一步参考:https://developer.wordpress.org/plugins/users/roles-and-capabilities/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-11
      相关资源
      最近更新 更多