Can Not Redirect from Plugin-Registered Admin Page

You can’t redirect to another page, because WordPress has already rendered admin header and sidebar templates. If you really wish to redirect users to another page, you need to do it earlier, before header and sidebar are rendered. You can use action of following structure to redirect to another page:

load-{parent_page_slug}_page_{plugin_subpage_slug}

The full snippet:

define( 'WPSE8170_REDIRECT_PAGE_SLUG', 'wpse8170-redirect-page' );

add_action('admin_menu', 'register_my_custom_submenu_page');
function register_my_custom_submenu_page() {
    add_submenu_page( 'tools.php', 'My Custom Redirect Page', 'Redirect Page', 'manage_options', WPSE8170_REDIRECT_PAGE_SLUG, 'wpse8170_redirect_page_callback' );
}

function wpse8170_redirect_page_callback() {
    // ...
}

add_action( 'load-tools_page_' . WPSE8170_REDIRECT_PAGE_SLUG, 'wpse8170_mypage_redirect' );
function wpse8170_mypage_redirect() {
    if ( WPSE8170_REDIRECT_PAGE_SLUG == filter_input( INPUT_GET, 'page' ) ) {
        wp_redirect( 'http://google.com' );
        exit;
    }
}

Leave a Comment