0

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

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

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.