Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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;
}
share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

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;
    }
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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