2

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!

4

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.

2

@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'
0

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;
    }
}
-2

LAST RESORT THAT WORKS You can temporarily disable redirection from the wp-login.php file and then delete all newly installed or updated plugins.

  1. Disable redirection by opening the file wp-login.php

2.Scroll down to the line where you have the code "do_action( "login_form_{$action}" );" Mine was around line 461

  1. Comment out that line of code to disable redirection

  2. Save the file. You will now be able to login using http://www.example.com/wp-login.php

  3. Disable or delete all recently installed plugins especially plugins that manage access control functionalities

  4. Clear your cookies and cache

  • 1
    It is really bad practice to change wordpress core files – Anton Lukin Dec 19 '17 at 21:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.