php - Redirect to another page after submission using wp_mail

admin2025-06-03  3

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 = [ "[email protected]" ];
    $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?

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 = [ "[email protected]" ];
    $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?

Share Improve this question asked Feb 13, 2019 at 10:29 HenshallHenshall 1132 silver badges12 bronze badges 2
  • wp_mail( $to, $subject, $body, $headers ); returns true? – André Kelling Commented Feb 13, 2019 at 10:33
  • @AndréKelling thanks for responding, what would be the best way to check this? – Henshall Commented Feb 13, 2019 at 10:44
Add a comment  | 

1 Answer 1

Reset to default 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; ?>
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748912489a314756.html

最新回复(0)