$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Redirect to another page after submission using wp_mail|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Redirect to another page after submission using wp_mail

matteradmin9PV0评论

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; ?>
Post a comment

comment list (0)

  1. No comments so far