[英]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
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.