redirect - Users are redirected to homepage instead of wp-admin

admin2025-06-03  3

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!

Share Improve this question asked Apr 30, 2016 at 18:27 Hello LiliHello Lili 1811 gold badge1 silver badge7 bronze badges 1
  • You can also use this plugin. wordpress/plugins/role-based-redirect – Yasar Commented Jul 25, 2018 at 4:46
Add a comment  | 

4 Answers 4

Reset to default 5

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.

  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/wp-login.php

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

  4. Clear your cookies and cache

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748963293a315200.html

最新回复(0)