I used login_redirect hook for redirect to custom url. while i use $redirect_to. It is now working. But while i echo $redirect_to it shows correct url.
I have set the redirect_to parameter in login form also.
function my_login_redirect( $redirect_to, $request, $user ) {
echo $redirect_to;
//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();
return $redirect_to;
die();
}
} else {
return $redirect_to;
}
}
add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );
well i found the solution. i used wp_redirect() inside my_login_redirect() i when the user is not administrator.
Related
Dear all,
I want to redirect Subscribers and Wordpress Admin to another url.
I have tried to do it with the following code but it does not work for me and I would like to improve it, or someone suggests a better coding.
I appreciate your efforts.
function custom_login_redirect($redirect_to, $request, $user) {
global $user;
if ( isset( $user->roles ) && is_array( $user->roles ) ) {
if ( in_array( 'subscriber', $user->roles ) ) {
return home_url("https://destodo.com/mi-escritorio/");
}
}
return $redirect_to;
}
add_filter( 'login_redirect', 'custom_login_redirect', 10, 3 );
This should do the trick. We filters the login redirect URL and redirect any users (including admin) to your redirect url.
<?php
add_filter( 'login_redirect', function ( $redirect_to, $requested_redirect_to, $user ) {
if ( $user && is_object( $user ) && is_a( $user, 'WP_User' ) ) {
$redirect_to = 'https://destodo.com/mi-escritorio/';
};
return $redirect_to;
}, 10, 3 );
You can try this:
It is working to redirect Subscribers and WordPress Admin to another URL
function login_redirect_based_on_roles($user_login, $user)
{
if (in_array('subscriber', $user->roles) || in_array('administrator', $user->roles)) {
exit(wp_redirect('https://destodo.com/mi-escritorio/'));
}
}
add_action('wp_login', 'login_redirect_based_on_roles', 10, 2);
I think I have achieved it with the following modification to the codes that you sent, where the subscriber accesses a page and the administrator to the desktop.
function login_redirect_based_on_roles($user_login, $user)
{
if (in_array('subscriber', $user->roles) ) {
exit(wp_redirect('https://pymecontable.com/mi-escritorio/'));
}
}
add_action('wp_login', 'login_redirect_based_on_roles', 10, 2);
Hi I use this code to redirect users on login based on the user role.
I want to modify it to use redirect based on the user id.
How could i do this?
Thanks
function my_login_redirect( $url, $request, $user ){
if( $user && is_object( $user ) && is_a( $user, 'WP_User' ) ) {
if( $user->has_cap( 'administrator' ) ) {
$url = home_url('mypage.html');
} else {
$url = home_url('/index.php');
}
}
return $url;}add_filter('login_redirect', 'my_login_redirect', 10, 3 );
Your $user object already contains the ID.
Your code would then look like this if you want to check for user with id 79:
function my_login_redirect( $url, $request, $user ){
if ( $user && is_object( $user ) && is_a( $user, 'WP_User' ) ) {
if ( $user->ID == 79 ) {
$url = home_url( '/mypage.html' );
} else {
$url = home_url( '/index.php' );
}
}
return $url;
}
add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );
Put the above in your functions.php. Tested and works.
I'm trying to disable admin login from front end of my wordPress site but my backend login also gets disable both login shows admin cannot login here
<?php
add_filter( 'authenticate', 'wp_admin_prevent_authentication', 30, 3 );
function wp_admin_prevent_authentication( $user, $username, $password ) {
if ( $user instanceof WP_User && is_page( 'my-account' ) ) {
if ( array_intersect( (array) $user->roles, [ 'administrator' ] ) ) {
return new WP_Error( 'admin-error', 'Admins cannot login from here.' );
};
};
return $user;
};
Not sure you can use is_page() inside authenticate but you can get page name using $_SERVER['REQUEST_URI']. check the below code.
function wp_admin_prevent_authentication( $user, $username, $password ) {
$url = explode( '/', rtrim( $_SERVER['REQUEST_URI'], '/') );
// If in My account dashboard page
if( $user instanceof WP_User && end( $url ) == 'my-account' ){
if ( array_intersect( (array) $user->roles, [ 'administrator' ] ) ) {
return new WP_Error( 'admin-error', 'Admins cannot login from here.' );
}
}
return $user;
}
add_filter( 'authenticate', 'wp_admin_prevent_authentication', 30, 3 );
Tested and works.
You can try with the below code, maybe it will work for you.
function wpum_admin_prevent_authentication( $user, $username, $password ) {
if ( $user instanceof WP_User && is_page( wpum_get_core_page_id( 'login' ) ) ) {
if ( array_intersect( (array) $user->roles, [ 'administrator' ] ) ) {
return new WP_Error( 'admin-error', 'Admins cannot login from here.' );
}
}
return $user;
}
add_filter( 'authenticate', 'wpum_admin_prevent_authentication', 30, 3 );
I want to redirect users after the login to specific page called 'activity'. I have tried the Peter’s Login Redirect plugin but it just worked to the subscriber users although I have added all the roles in the plugin setting.
I have tried to add function to function.php in my theme but I can not find the right one.
I hope this will help you for your specific page redirect, you want to do this:
function login_redirect( $redirect_to, $request, $user ){
return home_url('your-page.php');
}
add_filter( 'login_redirect', 'login_redirect', 10, 3 );
As you used this code...
function my_loginredirect( $redirect_to, $request, $user ) {
if ( isset( $user->roles ) && is_array( $user->roles ) ) {
if( in_array('administrator', $user->roles)) {
return admin_url();
}
else {
return site_url('avtivity');
}
}
add_filter('login_redirect', 'my_loginredirect', 10, 3);
After successful login user redirect to home url but i want to redirect it to admin dashboard after successful login in wordpress.
Note:I use multisite network. Any idea?
This can be used for wordpress multisite. It will direct user roles and redirect to specific page.
function login_redirect_page( $redirect_to, $request, $user ) {
global $user;
if ( isset( $user->roles ) && is_array( $user->roles ) ) {
if ( in_array( 'administrator', $user->roles ) ) {
return get_site_url().'/wp-admin/';
} else {
return home_url();
}
} else {
return $redirect_to;
}
}
add_filter( 'login_redirect', 'login_redirect_page', 10, 3 );
You can get the plugins on wordpress.org also.
function login_redirect_page( $redirect_to, $request, $user ) {
global $user;
if ( isset( $user->roles ) && is_array( $user->roles ) ) {
if ( in_array( 'administrator', $user->roles ) ) {
return get_site_url().'/wp-admin/';
}
if ( in_array( 'subscriber', $user->roles ) ) {
return get_site_url().'/wp-admin/';
}else {
return home_url();
}
} else {
return $redirect_to;
}
}
add_filter( 'login_redirect', 'login_redirect_page', 10, 3 );