I am working on my wordpress website and i am little stuck into one situation.... i want to redirect the users to a specific page after successful login but i dont know how to use the wordpress redirect hook here below is my wordpress hook that i put in function file... code that i found online:-
/**
* 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 );
currently i am using this code in anchor tag to redirect to sign in page and after sign in to specific page, the sign in is going fine but redirect is not working
<?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/
hey i edited the code as per my need.. you commented, but still it dosent 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();
} 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 );
$urllinkin =
remove this from your code.
Further more here is the code for login redirect (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 );
For redirection, you can usewp_redirect
default function too.
Here is your updated code,
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 );
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.