I'm using Wordpress with a User Role that should be redirected to /userrole
when they try to visit /
. Can anyone help me out? I tried a few things but it doesn't work.
This is what I have tried so far:
function redirect_users_by_role() {
$current_user = wp_get_current_user();
$role_name = $current_user->roles[0];
if('specuserrole' === $role_name) {
wp_redirect('website.com/userole'; ); }
}
add_action( '/'', 'redirect_users_by_role' );
It seems that you are hooking into a WordPress action of /
which doesn't exist. I'm not sure if you have read about what actions are available to you, but there is a full list here .
Based on this, it appears as though you want to hook into an action, check that we are on the front page and then redirect accordingly. With that in mind I would suggest the below:
function redirect_users_by_role() {
$current_user = wp_get_current_user();
$role_name = $current_user->roles[0];
if('specuserrole' === $role_name && is_page('home')) {
wp_redirect('website.com/userole');
exit();
}
}
add_action('init', 'redirect_users_by_role');
We are now hooking into the init
action as WordPress tells us that we have a fully authenticated user at this point. We then check that the current page is the home page using is_page('home')
and then do your redirection logic if needed.
It is not tested as I don't have a WordPress installation to hand so let me know if it works.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.