0

I am trying to create a redirect for my authors so that when they login (and access the admin area) they are directed to the posts page instead of the Wordpress dashboard. I found a function that works, but the function only works the first time the users access admin area. Each consecutive time they try and access the admin area, without logging out and back in, they are directed to the dashboard.

Here is my code

/* -------- Set default WP Admin page -------- */

function hide_the_dashboard()
{
    remove_menu_page( 'index.php' );
}
add_action( 'admin_menu', 'hide_the_dashboard' );

function your_login_redirect( $redirect_to, $request, $user )
{
    return admin_url( 'edit.php?post_type=st_kb' );
}
add_filter( 'login_redirect', 'your_login_redirect', 10, 3 );

I would greatly appreciate any help with this issue!

1
  • I see that the function only works when users are logging in (as I said earlier, the first time they access the admin panel). Is there a way to extend the functionality of this function so that it is called every time someone tries to access a certain page? (i.e. /wp-admin)
    – user73547
    Commented May 24, 2015 at 14:39

1 Answer 1

2

It will be bit tricky to detect if user just get into site. But generally there will be no referer, hence wp_get_referer() will return false.

My code check if there is referer, if no then they get redirected to post page.

NOTE:

When user access a page in admin by typying url in adressbar then redirect is applied to them also, because there are no referer.

This a simple solution, based on referer. It can be improved more.

/**
 * Redirect user if there is no referer.
 *  
 * @return void
 * @author Rahul Aryan <[email protected]>
 */
function my_redirect_users(){
    global $pagenow;

    /* Check current admin page. */
    if($pagenow != 'edit.php' && !wp_get_referer()){
        wp_redirect(admin_url('/edit.php?post_type=st_kb'));
        exit;
    }
}

add_action('admin_init', 'my_redirect_users');
1
  • Thank you for the response Aryan! Unfortunately this did not work for me. I see that the code I have now is only redirecting on login, and I want a function that will be called every time someone accesses the admin page. I'm not sure why this doesn't work.
    – user73547
    Commented May 24, 2015 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.