• Hello!

    I’m trying to set up my custom form to be able to redirect back to the same page, and display a success message to the user after submitting. I’m also trying to set it up so that a refresh does not re-submit the form. I’ve got the following code (below) so far, but I’m getting the following error:

    Warning: Cannot modify header information – headers already sent by (output started at C:\Users\Tim\Documents\wamp\www\redrhino\wp-content\themes\redrhino\header.php:51) in C:\Users\Tim\Documents\wamp\www\redrhino\wp-includes\pluggable.php on line 1207

    I understand that this is because wp_redirect() is running after get_header(), but I’m not sure how to make it run before the header without losing the url and anchor, and the check to see if user has submitted. Any help would be so greatly appreciated!! I’ve been at this for many hours.

    <?php
    /*
    Plugin Name: Contact Form Sender
    Plugin URI: https://www.collectivepitchmedia.com
    Description: Simple non-bloated WordPress Contact Form
    Version: 1.0
    Author: Tim Tucker
    Author URI: https://www.collectivepitchmedia.com
    */
    function html_form_code() {
            if (isset($_GET['success']) && $_GET['success'] == 1) {
                echo '<div class="col-12">';
                echo '<p class="cf-thanks">Thanks for your interest. We will be in touch soon.</p>';
                echo '</div>';
            }
            echo '<form id="cf" action="' . esc_url( $_SERVER['REQUEST_URI'] ) . '" method="post">';
            echo '<div class="col-4">';
            echo '<label class="clear">Name</label>';
            echo '<input id="cf_name" type="text" name="cf-name" pattern="[a-zA-Z0-9 ]+" required="required" value="' . ( isset( $_POST["cf-name"] ) ? esc_attr( $_POST["cf-name"] ) : '' ) . '" size="35" />';
            echo '</div>';
            echo '<div class="col-4">';
            echo '<label class="clear">Phone</label>';
            echo '<input id="cf_phone" type="tel" name="cf-phone" required="required" value="' . ( isset( $_POST["cf-phone"] ) ? esc_attr( $_POST["cf-phone"] ) : '' ) . '" size="35" />';
            echo '</div>';
            echo '<div class="col-4 submit-wrap"><input id="submit" type="submit" name="cf-submitted" value="Request Assessment"/></div>';
            echo '</form>';
    }
    function deliver_mail() {
        // if the submit button is clicked, send the email
        if ( isset( $_POST['cf-submitted'] ) ) {
            // sanitize form values
            $name    = sanitize_text_field( $_POST["cf-name"] );
            $phone   = sanitize_text_field( $_POST["cf-phone"] );
            $subject = "RedRhino Form Submission from $name" . "\r\n";
            $message = "$name would like to be contacted at $phone" . "\r\n";
            // get the blog administrator's email address
            $to = get_option( 'admin_email' );
            $headers = "From: $name" . "\r\n";
            // If email has been process for sending, display a success message
            if ( wp_mail( $to, $subject, $message, $headers ) ) {
                $str = get_permalink() . "#cf";
                $new_url = add_query_arg( 'success', 1, $str );
                wp_redirect( $new_url, 303 );
                exit();
            } else {
                echo 'An unexpected error occurred';
            }
        }
    }
    function cf_shortcode() {
        ob_start();
        deliver_mail();
        html_form_code();
        return ob_get_clean();
    }
    add_shortcode( 'contact_form', 'cf_shortcode' );
  • The topic ‘how to set up redirect with custom form plugin’ is closed to new replies.