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 use wp_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 );
Related
With Wordpress, I'm trying to create a function which autologins users, but upon visiting certain pages also performs an autologout if the initial login was obtained via autologin.
I've written the following code but I guess my boolean check for the autologin boolean does not work.
/* AUTO LOGIN / LOGOUT */
function autologinout() {
global $wp;
// Autologin
if( isset($_GET['username']) ) {
$user = get_user_by('login', $_GET['username']);
// Redirect URL //
if ( !is_wp_error( $user ) ) {
if ( in_array( 'customer', (array) $user->roles ) ) {
wp_clear_auth_cookie();
wp_set_current_user ( $user->ID );
wp_set_auth_cookie ( $user->ID );
wp_redirect( '/lovelists/toon-lovelist/' );
$autologin = true;
exit();
}
}
}
// Autologout
$path = $_SERVER['REQUEST_URI'];
if ( is_user_logged_in() && $autologin = true && ( $path == '/lovelists/maak-lovelist/' || $path == '/lovelists/login/' ) ) {
wp_clear_auth_cookie(); // so you don't get the cache error
wp_logout(); // this will logout user
$autologin = false;
}
}
add_action( 'init', 'autologinout' );
Any idea what I'm doing wrong?
I've posted the code below. Whenever I logged in, the $username is not getting executing thus leaving blank.
function my_login_redirect( $redirect_to, $request, $user ) {
global $current_user;
$current_member = wp_get_current_user();
$username = $current_member->user_login;
$url = home_url( "/connections/$username/profile/edit/group/1/" );
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 esc_url( $url );
}
} else {
return $redirect_to;
}
}
add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );
The global $currentuser is unavailable during this filter request as stated here
But the $user is available during the request from function parameters,
so simply these should work
function my_login_redirect( $redirect_to, $request, $user ) {
$username = $user->user_login;
$url = home_url( "/connections/$username/profile/edit/group/1/" );
if ( isset( $user->roles ) && is_array( $user->roles ) ) {
if ( in_array( 'administrator', $user->roles ) ) {
return $redirect_to;
} else {
return esc_url( $url );
}
} else {
return $redirect_to;
}
}
add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );
I'm trying to send the user to my custom page below, however the $username is leaving blank in the URL when the user logins
/connections/username/profile/edit/group/1/
The below code is working perfectly fine, except that $username is not executing properly
function my_login_redirect( $redirect_to, $request, $user ) {
global $current_user;
get_currentuserinfo();
$username = $current_user->user_login;
$url = home_url( "/connections/$username/profile/edit/group/1/" );
//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 esc_url( $url );
}
} else {
return $redirect_to;
}
}
add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );
after wordpress 4.5 get_currentuserinfo() deprecated so you can use code
function my_login_redirect( $redirect_to, $request, $user ) {
global $current_user;
$current_user = wp_get_current_user();
$username = $current_user->user_login;
$url = home_url( "/connections/$username/profile/edit/group/1/" );
//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 esc_url( $url );
}
} else {
return $redirect_to;
}
}
add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );
I've added a query string to my Wordpress installation using the following code in my functions.php file:
function add_query_vars($aVars) {
$aVars[] = "username";
return $aVars;
}
add_filter('query_vars', 'add_query_vars');
function add_rewrite_rules($aRules) {
$aNewRules = array(
'([^/]+)/manage' => 'index.php?pagename=manage&username=$matches[1]',
'([^/]+)/settings' => 'index.php?pagename=settings&username=$matches[1]',
);
$aRules = $aNewRules + $aRules;
return $aRules;
}
add_filter('rewrite_rules_array', 'add_rewrite_rules');
However, I don't want to load the page template if the username query string is empty, rather I'd like to redirect to the homepage. For instance:
website.com/myusername/manage
... would load a page template
website.com/manage
... would redirect home.
Can I do this using Wordpress' rewrite engine or should I take another approach?
Well, I would use this code:
function redirect_to_homepage( $query ) {
//page manage and settings does NOT exits
if ( $query->is_main_query() && ( $query->query_vars['name'] == 'manage' || $query->query_vars['name'] == 'settings' ) ){
if( ( !array_key_exists( 'username', $query->query_vars ) || ( array_key_exists( 'username', $query->query_vars ) && trim($query->query_vars['username']) == '' ) ) ) {
wp_redirect( get_bloginfo('url') );
exit;
}
}
//page manage and settings does exits
if ( $query->is_main_query() && ( $query->is_page('manage') || $query->is_page('settings') ) ){
if( ( !array_key_exists( 'username', $query->query_vars ) || ( array_key_exists( 'username', $query->query_vars ) && trim($query->query_vars['username']) == '' ) ) ) {
wp_redirect( get_bloginfo('url') );
exit;
}
}
}
add_action( 'parse_query', 'redirect_to_homepage' );
In my theme, there's custom page for the login. Login function at functions.php is like this
function log_in($username, $password) {
$user = parse_user($username);
$username = $username;
$password = $password;
if(isEmptyString($username)) return new WP_Error('username', 'required');
if(isEmptyString($password)) return new WP_Error('password', "required");
if(!wp_check_password( $password, $user->user_pass ) ) return new WP_Error('wrong_password', "wrong");
wp_set_auth_cookie($user->ID, $remember);
wp_login($username, $password);
redirect_profile();
}
function parse_user($info = null, $return = 'object') {
if ( is_null( $info ) ) {
global $current_user;
if ( empty( $current_user->ID ) ) return null;
$info = get_userdata( $current_user->ID );
}
elseif ( empty( $info ) ) {
return null;
}
if( $return == 'ID' ) {
if ( is_object( $info ) ) return $info->ID;
if ( is_numeric( $info ) ) return $info;
}
elseif( $return == 'object' ) {
if ( is_object( $info ) && $info->ID) return $info;
if ( is_object( $info )) return get_userdata( $info->ID );
if ( is_numeric( $info ) ) return get_userdata( $info );
if ( is_string( $info ) ) return get_userdatabylogin( $info );
}
else {
return null;
}
}
I want to add remember me checkbox for user to logged in all the time until they logout. How can i add this ? Please kindly help me out. Thank you.
"remember me" buttons are generally just a simple tweak to the cookie settings internally. Instead of a session cookie that gets deleted when the browser is exitted, a "remember me" login cookie gets some future expiration point (a day, a month, a year, etc...) so it'll persist after the browser's closed.
In pseudo-code, you'd have:
if (form_value('remember_me') == 'yes) {
set_long_term_cookie();
} else {
set_session_cookie();
}
"Add a login form on your WordPress Theme" (including remember me functionality):
http://www.wprecipes.com/add-a-login-form-on-your-wordpress-theme
Also: http://www.problogdesign.com/how-to/how-to-create-a-wordpress-login-form-overlay/
etc...