0

I am doing the following in my plugin:

  1. Register a custom admin page and in the callback function (the last parameter of add_submenu_page)

  2. Redirect to another page

However, when I open the custom admin page I get the following error:

Warning: Cannot modify header information - headers already sent by (output started at ..\wp-admin\includes\template.php:1637) in ..\wp-includes\pluggable.php on line 878

Here's the callback function:

function myplugin_callback(){
   wp_redirect('http://google.com');
   exit;
}

1 Answer 1

3

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;
    }
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.