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.

How to redirect to specified page after form was submitted?

Warning: Cannot modify header information - headers already sent by (output started at wordpress\wp-admin\menu-header.php:94) in wordpress\wp-content\plugins\test\test.php on line 16

<?php
/*
Plugin Name: TEST
Plugin URI: http://localhost/
Description: TEST
Version: 0.0.1
Author: TEST
Author URI: http://localhost/
*/
add_action('admin_menu', 'test');
function test() { 
    add_menu_page('test', 'test', 0, 'test/test.php', 'page');
} 
function page() {
    if(isset($_POST['submit'])) {
        header('Location: /index.php');
        exit;
    }
    echo '<form action="" method="post"><input type="submit" value="Submit form" name="submit" /></form>';
    var_dump($_POST);
}
share|improve this question

1 Answer 1

up vote 0 down vote accepted

You're trying to send a redirect header on the admin_menu action, which is fired after some of the page has been sent to the browser. You have to hook an earlier action before output starts, like init.

add_action( 'init', 'wpa_test' );
function wpa_test() {
    if(isset($_POST['submit'])) {
        wp_redirect( home_url() );
        exit;
    }
}
share|improve this answer
    
Milo, thanks! This is exactly what I need. –  Sergey L May 22 '13 at 17:08

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.