我正在写我的wordpress网站,我有点陷入了一种情况……我想在成功登录后将用户重定向到特定的页面,但我不知道如何使用wordpress重定向钩子,下面是我放在函数文件中的wordpress钩子。我在网上找到的代码:
/**
* WordPress function for redirecting users on login based on user role*/
function wpdocs_my_login_redirect( $url, $request, $user ) {
$urllinkin =
if ( $user && is_object( $user ) && is_a( $user, 'WP_User' ) ) {
if ( $user->has_cap( 'administrator' ) ) {
$url = admin_url();
} elseif() {
$url = home_url( '/members-only/' );
}
}
return $url;
}
add_filter( 'login_redirect', 'wpdocs_my_login_redirect', 10, 3 );目前,我正在使用锚标签中的代码来重定向到登录页面,登录到特定页面后,登录进行得很好,但重定向不起作用
<?php echo wp_login_url(get_permalink()); ?>"> this code give the url :- http://192.168.1.50/jobifylocal/my-profile/?redirect_to=http://192.168.1.50/jobifylocal/job/clinical-psychologist/嘿,我根据我的需要编辑了代码..您发表了评论,但仍未重定向
function wpdocs_my_login_redirect( $url, $request, $user ) {
if ( $user && is_object( $user ) && is_a( $user, 'WP_User' ) ) {
if ( $user->has_cap( 'administrator' ) ) {
$url = admin_url();
} elseif ( $user->has_cap( 'candidate' ) ) {
$variable_two = $_GET['redirect_to'];
if(!empty($variable_two)){
$url = $variable_two;
}
// $url = home_url( '/members-only/' );
}
}
return wp_redirect($url);
}
add_filter( 'login_redirect', 'wpdocs_my_login_redirect', 10, 3 );发布于 2020-09-18 13:16:08
$urllinkin =将其从您的代码中删除。
此外,这里是登录重定向(Check official docs)的代码,
function my_login_redirect( $redirect_to, $request, $user ) {
//is there a user to check?
if ( isset( $user->roles ) && is_array( $user->roles ) ) {
//check for admins
if ( in_array( 'administrator', $user->roles ) ) {
// redirect them to the default place
return $redirect_to;
} else {
return home_url();
}
} else {
return $redirect_to;
}
}
add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );对于重定向,您也可以使用wp_redirect默认功能。
这是您更新的代码,
function wpdocs_my_login_redirect( $url, $request, $user ) {
if ( $user && is_object( $user ) && is_a( $user, 'WP_User' ) ) {
if ( $user->has_cap( 'administrator' ) ) {
$url = admin_url();
} else {
$url = home_url( '/members-only/' );
}
}
return $url;
}
add_filter( 'login_redirect', 'wpdocs_my_login_redirect', 10, 3 );https://stackoverflow.com/questions/63949635
复制相似问题