$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>redirect - Users are redirected to homepage instead of wp-admin|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin8PV0评论

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

Post a comment

comment list (0)

  1. No comments so far