[英]WooCommerce - Redirect Logged In Users Based on their Role
This is a query based around WooCommerce Membership and Subscriptions.这是基于 WooCommerce 会员资格和订阅的查询。
I must add that I'm also trying to decide if it's good UX to do what I'm doing.我必须补充一点,我也在努力确定做我正在做的事情是否是好的用户体验。
There are many solutions to redirecting users after they log in but I have a situation where I want to redirect a user with the role of 'subscriber' when they click on specific links to pages that describe and allow you to become a member.用户登录后重定向的解决方案有很多,但我有一种情况,当用户单击特定链接到描述并允许您成为会员的页面时,我想重定向具有“订阅者”角色的用户。 So although I don't want to hide 'join now' etc I just want those to redirect to the my-account page.
因此,虽然我不想隐藏“立即加入”等,但我只想将它们重定向到我的帐户页面。
Again there are various roles and redirect plugins but none seem to help in this specific scenario.同样有各种角色和重定向插件,但在这种特定情况下似乎没有任何帮助。 So the source of the code I've used is here: SOURCE and I'm looking to do something like this:
所以我使用的代码的来源在这里: SOURCE ,我正在做这样的事情:
function eks_redirect_users_by_role() {
global $post;
$current_user = wp_get_current_user();
$role_name = $current_user->roles[0];
if ( 'subscriber' === $role_name && $post->ID == 47145) {
wp_redirect( '/my-account' );
}
}
add_action( 'admin_init', 'eks_redirect_users_by_role' );
So if the user role is a subscriber and they try and visit the page idea it's redirected.因此,如果用户角色是订阅者并且他们尝试访问页面创意,则它会被重定向。
At the current time, it does fall back to a product page which says 'you already have a membership' but it's multiple steps to arrive.目前,它确实会退回到一个产品页面,上面写着“您已经拥有会员资格”,但要经过多个步骤才能到达。
This might be more useful and proper way to achieve your request.这可能是实现您的请求的更有用和更正确的方法。
function redirection_based_user_role(){
$current_user = wp_get_current_user();
$role_name = $current_user->roles[0];
$postid = get_the_ID();
if ( 'subscriber' === $role_name && $postid == 47145 ) {
wp_redirect( home_url( '/my-account' ), 301 );
exit;
}
}
add_action( 'template_redirect', 'redirection_based_user_role' );
Hope this works.希望这有效。
I was able to achieve want I wanted to the following way:我能够通过以下方式实现我想要的:
function eks_redirect_user() {
$current_user = wp_get_current_user();
$role_name = $current_user->roles[0];
$postid = get_the_ID();
if ( 'subscriber' === $role_name && $postid == 47145 ) { ?>
<script>
function redirectPage() {
window.location = "/my-account/";
}
redirectPage();
</script>
<?php
exit;
}
}
add_action('wp_footer', 'eks_redirect_user' );
The issue is it's a fairly untidy solution as the redirect feels odd.问题是这是一个相当不整洁的解决方案,因为重定向感觉很奇怪。 I'm not sure if using wp_redirect would work better.
我不确定使用 wp_redirect 是否会更好。 I decided to do was just disable the button on the page with the main membership information rather than redirecting every call to action to the account page which seems like a more elegant solution.
我决定做的只是禁用包含主要会员信息的页面上的按钮,而不是将每个号召性用语重定向到帐户页面,这似乎是一个更优雅的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.