向WooCommerce客户用户角色显示WordPress后端


Showing WordPress backend to WooCommerce customer user role

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

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

这仅适用于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文件或任何插件文件中。

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

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