I am trying to get WordPress to automatically redirect from somesite.com/wp-admin (if the user logged in successfully) to the blog that the user is on (somesubsite.somesite.com/wp-admin). I have written the following code to do so:
add_action( 'admin_page_access_denied', 'redirect_user_to_correct_blog' );
function redirect_user_to_correct_blog()
{
if ( ! is_user_logged_in() || is_network_admin() ){
return;
}
$blogs = get_blogs_of_user( get_current_user_id() );
//var_dump($blogs);
if ( wp_list_filter( $blogs, array( 'userblog_id' => get_current_blog_id() ) ) ){
return;
}
if(count($blogs)==1){
$blog=reset($blogs);
wp_redirect(get_admin_url($blog->userblog_id));
exit();
}
}
But this code does not appear to be executing (I placed logging code which has been removed). Does anyone have any idea what I’m doing wrong? I am running the latest version of all plugins/WordPress currently available.
Currently, when the user logs into somesite.com/wp-admin, I get: “You do not have sufficient permissions to access this admin page.” and they are sent to “somesite.com/wp-admin” and not “somesubsite.somesite.com/wp-admin”.
Thanks!
The issue was that I was using Admin Menu Editor, which overrides the default behavior for WordPress, and therefore, “admin_page_access_denied” was never called. I added a do_action(‘admin_page_access_denied’) to this plugin where it was denying access to a user. I hope this helps someone else not bang their head against a wall for hours. 🙂