• Hi,

    I’ve created a custom form that sends an email when a users fills-in some data and clicks send. This form points to admin-post.php:

    <form action="<?php echo esc_url( admin_url('admin-post.php') ); ?>" method="post">
    
    // My code
    									  
    </form>

    I use a function hooked to:

    add_action( 'admin_post_nopriv_send_my_email', 'send_my_email' );
    add_action( 'admin_post_send_my_email', 'send_my_email' );

    When after processing the data I use wp_redirect to come back to the same page or other pages it just doesn’t work. Headers have been sent. How to overcome this?

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Does anything at all happen after the data is processed? What do the sent headers look like?

    Also, what does your wp_redirect() code look like?

    Moderator bcworkz

    (@bcworkz)

    If your send_my_email() function generates any output to the browser whatsoever, you will get a headers already sent error when redirecting. I did a quick test with GETting through admin-post.php that does an immediate wp_redirect() on entry and the redirect is successful. So generally speaking redirects are possible from admin-post.php callbacks.

    Thread Starter Kevin Mamaqi Kapllani

    (@kevinmamaqi)

    Thank you both, the error was with the wp_redirect

    Thread Starter Kevin Mamaqi Kapllani

    (@kevinmamaqi)

    Hi @bcworkz – Do you mean then if I use a wp_mail function and after I try to redirect it is already not possible?

    @jarretc my wp_redirect looks like this:

    
    wp_redirect( $_SERVER["HTTP_REFERER"], 302, 'WordPress' );
    exit;
    status_header(200);
    die("Gracias, hemos recibido tu pedido.");
    

    I opened it again as it stopped working.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Redirect after form submission using admin_post hook’ is closed to new replies.