-1

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.

  • You can't print output at wp_loaded it's too early. That's what the warning is. – Jacob Peattie Nov 13 '18 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 Nov 13 '18 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 Nov 13 '18 at 12:25
  • Then how do I redirect before headers are sent? – Limpuls Nov 13 '18 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 Nov 13 '18 at 12:41
1

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

  • And the line echo 'data has been save' ; right before the redirect should be removed. – ahendwh2 Nov 13 '18 at 12:30

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.