WP: Header warning after sending email
After sending a contact form I got following error:
Warning: Cannot modify header information - headers already sent by (output started at /home/clientsc/public_html/mypage/wp-includes/general-template.php:2680) in /home/client/public_html/mypage/wp-includes/pluggable.php on line 1171
I got this error when I put the function below in the file named functions.php
function send_my_awesome_form(){
if (!isset($_POST['submit'])) {
// get the info from the from the form
$form = array();
$form['fullname'] = $_POST['fullname'];
$form['company'] = $_POST['company'];
$form['email'] = $_POST['email'];
}
// Build the message
$message = "Name :" . $form['fullname'] ."n";
$message .= "Company :" . $form['company'] ."n";
$message .= "Email :" . $form['email'] ."n";
//set the form headers
$headers = 'From: Contact form <your@contactform.com>';
// The email subject
$subject = 'you got mail';
// Who are we going to send this form too
$send_to = 'myemail@gmail.com';
if (wp_mail( $send_to, $subject, $message, $headers ) ) {
wp_redirect(home_url( )); exit;
}
}
add_action('wp_head', 'send_my_awesome_form');
How can I solve this issue?
wp_head
hook is used in the header of your theme, so it's too late and the HTML is already partially displayed.
You have to use the init
hook instead :
add_action('init', 'send_my_awesome_form');
EDIT :
function send_my_awesome_form(){
if( isset($_POST['fullname']) && isset($_POST['company']) && isset($_POST['email']) ){
// your form treatment
// your redirect
}
}
链接地址: http://www.djcxy.com/p/69338.html
上一篇: Outlook Rest Api发送邮件请求返回状态400
下一篇: WP:发送电子邮件后的标题警告