How to prevent page from resubmit on refresh when “Cannot modify header information – headers” warning shows

I know that there are many possible duplicates but i keep receiving this error each time

I’m currently working on a WordPress contact form, which is created as plugin. I need after submitting the form to redirects to the same page with a new clean form. The problem is that if i put header(“location…) it shows me the message

Warning: Cannot modify header information – headers already sent by
(output started at
C:xampphtdocsWPwp-contentthemesgroup-testheader.php:13) in
C:xampphtdocsWPwp-contentpluginsform-exampleform-example.php on
line 84

The form is working correctly and it sends email after i press submit button, but the problem is that it keeps the old input values and it resends them again after every reload or submit pressing even the form is empty… The best decision for me would be not to reload the page but only to shows some message after i press “Submit” and to empty the input values from the form as others WP forms functionality(Contact 7 etc.) but i know that Ajax is needed… But firstly i will be glad if someone shows me where i am wrong.

Plugin Name: Example Contact Form
Plugin URI:
Description: Contact Form
Version: 1.0
Author: JD
Author URI:
function html_form_code() {
<form action=”<?php esc_url( $_SERVER[‘REQUEST_URI’] )?>” method=”post” class=”contact-form” id=”newsletterform”>
<div class=header-contact>
<p><h2>Contact Form</h2></p>

<div class=input-containers>
<input type=”text” id=”name” name=”cf-name” pattern=”[a-zA-Z0-9 ]+” value=”” size=”40″ placeholder=”Име и фамилия”/>
<div class=input-containers>
<input type=”email” name=”cf-email” value=”” size=”40″ placeholder=”Поща”/>
<div class=input-containers>
<input type=”text” name=”cf-subject” pattern=”[a-zA-Z ]+” value=”” size=”40″ placeholder=”Относно”/>
<div class=input-containers>
<textarea rows=”10″ cols=”35″ name=”cf-message” placeholder=”Текст”></textarea>
<div class=input-containers>
<input type=”submit” name=”cf-submitted” value=”Send” >
//function to generate response

function deliver_mail() {
require_once “wp-includes/class-phpmailer.php”;
$verificationMessages = array();

// if the submit button is clicked, send the email
if ( isset( $_POST[‘cf-submitted’] ) ) {
// sanitize form values
$name = sanitize_text_field( $_POST[“cf-name”] );
$email = sanitize_email( $_POST[“cf-email”] );
$subject = sanitize_text_field( $_POST[“cf-subject”] );
$message = esc_textarea( $_POST[“cf-message”] );

$headers = “From: $name <$email>” . “rn”;
// Localhost
$mail = new PHPMailer(true);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->CharSet = ‘UTF-8’;

$mail->SMTPDebug = 0; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = “ssl”; // sets the prefix to the servier
$mail->Host = “”; // sets GMX as the SMTP server for example:
$mail->Port = 465; // set the SMTP port for the GMX server

$mail->Username = $email;
$mail->Password = ‘pass’;

$mail->SetFrom($email, $name);

$mail->Subject = $subject;

$headers .= “Content-Type: text/html; charset=utf-8”;
$headers .= “Content-Transfer-Encoding: 8bit”;

try {
$msg = “An email has been sent for verfication.”;
$msgType = “success”;
array_push($verificationMessages, “Check your email to activate your account”);
header(“location: http://localhost/wp/”);

} catch (Exception $ex) {
$msg = $ex->getMessage();
$msgType = “warning”;
array_push($verificationMessages, “Wrong”);
header(“location: http://localhost/wp/”);

function cf_shortcode() {


//return ob_get_clean();
add_shortcode( ‘sitepoint_contact_form’, ‘cf_shortcode’ );

Read more here:: How to prevent page from resubmit on refresh when “Cannot modify header information – headers” warning shows

Leave a Reply

Your email address will not be published. Required fields are marked *