After migrating my wordpress site users who are not Admins are redirected to the site's homepage after login. On the old site they were redirected on wp-admin. The Administrator is redirected to wp_admin as it should.
I want the users to be redirected to wp-admin
after login.
I changed siteurl from the database (wp_options
), also added this filter in my functions.php
:
function my_login_redirect( $redirect_to, $request, $user ) {
return admin_url();
}
add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );
Any help please? Thank you and Happy Easter!
After migrating my wordpress site users who are not Admins are redirected to the site's homepage after login. On the old site they were redirected on wp-admin. The Administrator is redirected to wp_admin as it should.
I want the users to be redirected to wp-admin
after login.
I changed siteurl from the database (wp_options
), also added this filter in my functions.php
:
function my_login_redirect( $redirect_to, $request, $user ) {
return admin_url();
}
add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );
Any help please? Thank you and Happy Easter!
Yeeey, I figured it out! Actually my theme had a redirect like this one in functions.php
:
// Block Access to /wp-admin for non admins.
function custom_blockusers_init() {
if ( is_user_logged_in() && is_admin() && !current_user_can( 'administrator' ) ) {
wp_redirect( home_url() );
exit;
}
}
add_action( 'init', 'custom_blockusers_init' ); // Hook into 'init'
All you have to do is add your own role capability, for example: !current_user_can( 'manage-reports' )
This helped me a lot.
@Hello Lili is right. But, we should check DOING_AJAX also!
// Block Access to /wp-admin for non admins.
function custom_blockusers_init() {
if ( is_user_logged_in() && is_admin() && !current_user_can( 'administrator' ) && (defined( 'DOING_AJAX' ) && !DOING_AJAX) ) ) {
wp_redirect( home_url() );
exit;
}
}
add_action( 'init', 'custom_blockusers_init' ); // Hook into 'init'
The below code is working like expected. This code restricts non admin users to access wp-admin or profile page.
add_action( 'admin_init', 'redirect_non_admin_users' );
/**
* Redirect non-admin users to home page
*
* This function is attached to the 'admin_init' action hook.
*/
function redirect_non_admin_users() {
if ( ! current_user_can( 'manage_options' ) && '/wp-admin/admin-ajax.php' != $_SERVER['PHP_SELF'] ) {
wp_redirect( home_url() );
exit;
}
}
LAST RESORT THAT WORKS You can temporarily disable redirection from the wp-login.php file and then delete all newly installed or updated plugins.
2.Scroll down to the line where you have the code "do_action( "login_form_{$action}" );" Mine was around line 461
Comment out that line of code to disable redirection
Save the file. You will now be able to login using http://www.example/wp-login.php
Disable or delete all recently installed plugins especially plugins that manage access control functionalities
Clear your cookies and cache