php - Cannot modify header information - headers already sent

admin2025-06-06  10

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749185648a317079.html

最新回复(0)