$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Cannot modify header information - headers already sent|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Cannot modify header information - headers already sent

matteradmin9PV0评论

So I have a form on one page, which is submitting to admin-post.php, then there is a function in functions.php which is processing the form. It stores the input field data to database and then after that it should redirect the user to another page. It stores the information in database, but it is not redirecting. The problem is that it says

Cannot modify header information - headers already sent

Code:

<form action="<?php echo esc_url( admin_url('admin-post.php') ); ?>" method="post">
        <input id="user_name" type="text" name="yourname" placeholder="Name" class="form-control">
        <input id="user_email" type="text" name="email" placeholder="Email" class="form-control">
        <button id="button" type="button" name="submit">Go</button>
        <input type="submit" value="submit" name="submit" class="btn btn-success">
        <input type="hidden" name="action" value="login_form">
        </form>

functions.php

function prefix_send_email_to_admin() {
    /**
     * At this point, $_GET/$_POST variable are available
     *
     * We can do our normal processing here
     */

    // Sanitize the POST field
    // Generate email content
    // Send to appropriate email
        function my_print_error(){

            global $wpdb;

            if($wpdb->last_error !== '') :

                $str   = htmlspecialchars( $wpdb->last_result, ENT_QUOTES );
                $query = htmlspecialchars( $wpdb->last_query, ENT_QUOTES );

                print "<div id='error'>
                <p class='wpdberror'><strong>WordPress database error:</strong> [$str]<br />
                <code>$query</code></p>
                </div>";

            endif;

        }
            if (isset($_POST['submit'])) {
                global $wpdb;

                $wpdb->show_errors();

                $table = 'username';
                $data = array(
                    'name' => $_POST['yourname']
                );

                $success=$wpdb->insert( $table, $data );

                if($success){
                    echo 'data has been save' ;
                                wp_redirect("/projects/webdev/wordpress_nepal/");

                                exit;
                }
                else {
                    my_print_error();
                }

            }
    //  wp_redirect("/projects/webdev/wordpress_nepal/");

}
add_action( 'wp_loaded', 'prefix_send_email_to_admin' );

I tried both, wp_loaded hook after some googling and my original admin_post_nopriv_login_form but it doesn't help.

So I have a form on one page, which is submitting to admin-post.php, then there is a function in functions.php which is processing the form. It stores the input field data to database and then after that it should redirect the user to another page. It stores the information in database, but it is not redirecting. The problem is that it says

Cannot modify header information - headers already sent

Code:

<form action="<?php echo esc_url( admin_url('admin-post.php') ); ?>" method="post">
        <input id="user_name" type="text" name="yourname" placeholder="Name" class="form-control">
        <input id="user_email" type="text" name="email" placeholder="Email" class="form-control">
        <button id="button" type="button" name="submit">Go</button>
        <input type="submit" value="submit" name="submit" class="btn btn-success">
        <input type="hidden" name="action" value="login_form">
        </form>

functions.php

function prefix_send_email_to_admin() {
    /**
     * At this point, $_GET/$_POST variable are available
     *
     * We can do our normal processing here
     */

    // Sanitize the POST field
    // Generate email content
    // Send to appropriate email
        function my_print_error(){

            global $wpdb;

            if($wpdb->last_error !== '') :

                $str   = htmlspecialchars( $wpdb->last_result, ENT_QUOTES );
                $query = htmlspecialchars( $wpdb->last_query, ENT_QUOTES );

                print "<div id='error'>
                <p class='wpdberror'><strong>WordPress database error:</strong> [$str]<br />
                <code>$query</code></p>
                </div>";

            endif;

        }
            if (isset($_POST['submit'])) {
                global $wpdb;

                $wpdb->show_errors();

                $table = 'username';
                $data = array(
                    'name' => $_POST['yourname']
                );

                $success=$wpdb->insert( $table, $data );

                if($success){
                    echo 'data has been save' ;
                                wp_redirect("http://example.design/projects/webdev/wordpress_nepal/");

                                exit;
                }
                else {
                    my_print_error();
                }

            }
    //  wp_redirect("http://example.design/projects/webdev/wordpress_nepal/");

}
add_action( 'wp_loaded', 'prefix_send_email_to_admin' );

I tried both, wp_loaded hook after some googling and my original admin_post_nopriv_login_form but it doesn't help.

Share Improve this question asked Nov 13, 2018 at 12:12 LimpulsLimpuls 3071 gold badge3 silver badges16 bronze badges 5
  • You can't print output at wp_loaded it's too early. That's what the warning is. – Jacob Peattie Commented Nov 13, 2018 at 12:15
  • @JacobPeattie As I understand, wp_loaded should work because it firesb efore the headers are already sent which is when it's too late. But then which hook should I use? – Limpuls Commented Nov 13, 2018 at 12:17
  • No, the problem is outputting anything before the headers are sent. WordPress is trying to send headers but there's already been output to the screen, which messes it up. – Jacob Peattie Commented Nov 13, 2018 at 12:25
  • Then how do I redirect before headers are sent? – Limpuls Commented Nov 13, 2018 at 12:26
  • A redirect isn't output, they've got nothing to do with each other. It's the echo 'data has been save' ; and print "<div id='error'> lines that are causing this specific message to appear. – Jacob Peattie Commented Nov 13, 2018 at 12:41
Add a comment  | 

1 Answer 1

Reset to default 1

You should use the template_redirect action instead of wp_loaded. It's a good practice add die(); after wp_redirect() to.

Post a comment

comment list (0)

  1. No comments so far