1

I'm trying to redirect to user to another page after the form has been submitted using wp_mail.

// Hooking up our functions to WordPress filters
add_filter( 'wp_mail_from', 'wpb_sender_email' );
add_filter( 'wp_mail_from_name', 'wpb_sender_name' );

// Email Sender
add_action('wp_ajax_mySendEmail', 'mySendEmail');
add_action('wp_ajax_nopriv_mySendEmail', 'mySendEmail');
function mySendEmail() {
    $name        = $_POST['name'];
    $email       = $_POST['email'];
    $contact_no  = $_POST['contact_no'];
    $debts_value  = $_POST['debts_value'];
    $how_much  = $_POST['how_much'];
    $employment_type  = $_POST['employment_type'];
    $page_id  = $_POST['page_id'];
    $keyword  = $_POST['keyword'];
    $to = [ "mail@mail.com" ];
    $subject = '';
    $mybody = '';
    $mybody .='<table>';
    $mybody .='';
    $mybody .='<p>The following information has been submitted via your website.</p>';
    $mybody .='<tr><td><b>How</b></td><td>'.$how_much.'</td></tr>';
    $mybody .='<tr><td><b>How</b></td><td>'.$debts_value.'</td></tr>';
    $mybody .='<tr><td><b></b></td><td>'.$employment_type.'</td></tr>';
    $mybody .='<tr><td><b>Name</b></td><td>'.$name.'</td></tr>';
    $mybody .='<tr><td><b>Email</b></td><td>'.$email.'</td></tr>';
    $mybody .='<tr><td><b>Contact No</b></td><td>'.$contact_no.'</td></tr>';
    $mybody .='<tr><td><b>Page</b></td><td>'.$page_id.'</td></tr>';
    $mybody .='<tr><td><b>Keyword</b></td><td>'.$keyword.'</td></tr>';
    $mybody .='<p>Thank you,</p>';
    $mybody .='<p></p>';
    $mybody .='</table>';
    $body = $mybody;
    $headers = array('Content-Type: text/html; charset=UTF-8');
    $sendStatus = wp_mail( $to, $subject, $body, $headers );

    if ( $sendStatus ){
        wp_redirect( '/thank-you/', 301);
        exit;
    }

    // print_r($sendStatus); die;
}

It just dosent seem to redirect, not sure what im doing wrong?

  • wp_mail( $to, $subject, $body, $headers ); returns true? – André Kelling Feb 13 '19 at 10:33
  • @AndréKelling thanks for responding, what would be the best way to check this? – Henshall Feb 13 '19 at 10:44
1

I think home url not found. And make sure this condition true. if ( $sendStatus ){ }

Please try like this.

<?php wp_redirect( home_url('/thank-you/', 301) ); exit; ?>
|improve this answer|||||

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.