【问题标题】:Showing WordPress backend to WooCommerce customer user role向 WooCommerce 客户用户角色显示 WordPress 后端
【发布时间】:2017-02-17 07:10:40
【问题描述】:

也许有人知道如何链接注册 WooCommerce 和注册 wordpress?

例如用户应该有发布帖子等的能力。问题是标准管理面板在用户登录时隐藏。

【问题讨论】:

    标签: php wordpress woocommerce registration user-roles


    【解决方案1】:

    您可以在“设置”>“常规”>“新用户默认角色”下更改新用户的默认角色,或者,您可以使用此 sn-p 以编程方式设置它(这将覆盖 WP 设置中设置的选项):

    add_filter('pre_option_default_role', function($default_role){
      return 'subscriber'; //Change this to fit your needs
    });
    

    【讨论】:

    • 注意:订阅者无法发布帖子。
    【解决方案2】:

    这仅适用于 WooCommerce Customer 用户角色:

    这里你有必要的钩子函数来做你正在看的事情。这将允许客户用户角色访问 wordpress 后端并发布/编辑帖子(使用最后一个功能,您应该注意 customer 用户角色将能够添加/编辑/发布帖子,所以之前备份你的数据库)

    代码如下:

    add_filter('woocommerce_disable_admin_bar', '_wc_disable_admin_bar', 10, 1);
    add_filter('woocommerce_prevent_admin_access', '_wc_prevent_admin_access', 10, 1);
    function _wc_prevent_admin_access($prevent_admin_access) {
        $user_data = get_userdata( get_current_user_id() );
        $user_roles = $user_data->roles;
        $customer_role = get_role( 'customer' );
    
        // For "customer" WooCommerce user role only
        if (in_array('customer', $user_roles)) {
    
            // Warning! with this (This will be definitive, so make a database backup)
            // Adding 'add_post', 'publish_posts' and 'edit_post' capabilities to customer user role
            if ( !user_can( get_current_user_id(), 'publish_posts' ) ){
                $customer_role->add_cap( 'create_posts' );
                $customer_role->add_cap( 'publish_posts' );
                $customer_role->add_cap( 'edit_posts' );
            }
    
            // Giving access to wordpress backend
            return false;
        }
    }
    

    代码位于您的活动子主题(或主题)的 function.php 文件中或任何插件文件中。

    此代码已经过测试并且可以工作

    【讨论】:

      猜你喜欢
      • 2016-01-10
      • 2020-10-07
      • 2019-12-29
      • 1970-01-01
      • 2015-06-21
      • 2017-04-08
      • 2015-10-06
      • 1970-01-01
      • 2020-09-19
      相关资源
      最近更新 更多