0

Looking for a way to redirect the user from the dashboard straight to edit.php if wp_is_mobile() is true.

This is what I've tried:

function redirect_if_mobile() {
    $screen = get_current_screen();
    if($screen->base == 'dashboard') {
        if ( wp_is_mobile() ) {
            $url = admin_url( 'edit.php' );
            wp_redirect( $url );
        }
    }
}
add_action( 'admin_menu', 'redirect_if_mobile');

The problem is that get_current_screen() isn't defined in either admin_init or admin_menu (if you hook onto admin_head instead it's too late and headers have already been sent).

Ideas?

  • You could use global $pagenow; if('index.php'==$pagenow)/* etc */. Also, Adminimize has the option to hide the Dashboard menu item and redirect to other page. – brasofilo Nov 12 '12 at 23:47
2

The action you're looking for is auth_redirect, which is before the headers but still recognizes $pagenow to tell which page you're on:

add_action('auth_redirect', 'the_mobile_boot');
function the_mobile_boot() {
    global $pagenow;

    if ( $pagenow == 'index.php' && wp_is_mobile() ) {
        header( 'Location: ' . get_admin_url(null, 'edit.php') );
        exit;
    }

}
2

You can actually use the function you're using, you just need to use load-index.php (no need for globals if you don't need them):

add_action('load-index.php', function(){
    if(get_current_screen()->base == 'dashboard' && !wp_is_mobile())
        wp_redirect(admin_url('edit.php'));
});
0

Alright, found one solution, although not that elegant.

function load_if_mobile() {
    if ( wp_is_mobile() ) {?>
    <script>
        window.location = '/wp-admin/edit.php';
    </script><?php
    }
}
add_action('admin_head-index.php', 'load_if_mobile');
  • And here, your original wp_redirect() works, ain't it? – brasofilo Nov 12 '12 at 23:48

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.