0

Below is the code I'm using for my short-coded form and to add the post.

Here's a basic overview of the problem I'm trying to remedy:

-This form is displayed on the page at /addmember/ -On this page is also a list of existing members, current and pending (members being a custom post type)

-This form is used to submit what will be a pending member

-On submit of this form the member is automatically given a pending status and would be then displayed in the pending members section of the /addmember/ page -So the idea is then that after the member is submitted the page is refreshed to show the new pending member in the pending member list on the /addmember/ page

-Right now this is functional BUT after a user clicks submit, the /addmember/ page appears to refresh--all the user sees is that same form with empty fields--the lists do not appear, no other information on the /addmember/ page appears. All they see is that empty form. -Then finally after a few seconds the are redirected to/the /addmember/ page refreshes

***I want to figure out a way to eliminate that odd moment when they see that almost empty page with just the empty form before it finally redirects/refreshes. How can I do this? I've tried using wp_redirect instead but that seems to have no effect. And I can't move the refresh action up any farther in the code.

<?php    
function ty_front_end_form() { ?>

            <form id="mreg" method="POST" action="">

            <label for="mfname">Name</label>
            <input type="text" id="mfname" name="mfname" value="" /><br/>

            <label for="mem">Email</label>
            <input type="text" id="mem" name="mem" value="" /><br/>

            <label for="mpw">Why</label>
            <textarea id="mpw" cols="19" rows="4" name="mpw"/></textarea><br/>

            <input type="submit" name="submit" value="Submit" style="margin-left: 201px; background: #000;"/>   

            <input type="hidden" name="second-nom" id="second-nom" value="No" />            
            <input type="hidden" name="start" id="start" value="1" />           
            <input type="hidden" name="post-type" id="post-type" value="add-members" /> 
            <input type="hidden" name="action" value="add-members" /> 

            </form>
            <?php

            if( isset( $_POST['start'] ) ) {
                ty_save_post_data();
            } ?>


<?php }

        add_shortcode('add-members','ty_front_end_form');


        function ty_save_post_data() {


        $post = array(
        'post_title' => $_POST[ 'mfname' ],
        'post_status' => 'publish', 
        'post_type' => 'add-members' 
        );  


        $post_id = wp_insert_post($post); 

        add_post_meta($post_id, 'mfname', sanitize_text_field( $_POST[ 'mfname' ] ), true);
        add_post_meta($post_id, 'mem', sanitize_text_field( $_POST[ 'mem' ] ), true);
        add_post_meta($post_id, 'mpw', esc_attr( $_POST['mpw'] ), true);
        add_post_meta($post_id, 'second-nom', sanitize_text_field( $_POST[ 'second-nom' ] ), true );
        $location = 'http://example.com/addmember/';  
        echo "<meta http-equiv='refresh' content='0;url=$location' />"; 
        exit;
        } 

?>

PER MILO'S ANSWER HERE IS THE UPDATED CODE

<?php    
function ty_front_end_form() { ?>

            <form id="mreg" method="POST" action="">

            <label for="mfname">Name</label>
            <input type="text" id="mfname" name="mfname" value="" /><br/>

            <label for="mem">Email</label>
            <input type="text" id="mem" name="mem" value="" /><br/>

            <label for="mpw">Why</label>
            <textarea id="mpw" cols="19" rows="4" name="mpw"/></textarea><br/>

            <input type="submit" name="submit" value="Submit" style="margin-left: 201px; background: #000;"/>   

            <input type="hidden" name="second-nom" id="second-nom" value="No" />            
            <input type="hidden" name="start" id="start" value="1" />           
            <input type="hidden" name="post-type" id="post-type" value="add-members" /> 
            <input type="hidden" name="action" value="add-members" /> 

            </form>


<?php }

        add_shortcode('add-members','ty_front_end_form');


        function ty_save_post_data() {

        if( isset( $_POST['start'] ) ) { 

            $post = array(
            'post_title' => $_POST[ 'mfname' ],
            'post_status' => 'publish', 
            'post_type' => 'add-members' 
            );  


            $post_id = wp_insert_post($post); 

            add_post_meta($post_id, 'mfname', sanitize_text_field( $_POST[ 'mfname' ] ), true);
            add_post_meta($post_id, 'mem', sanitize_text_field( $_POST[ 'mem' ] ), true);
            add_post_meta($post_id, 'mpw', esc_attr( $_POST['mpw'] ), true);
            add_post_meta($post_id, 'second-nom', sanitize_text_field( $_POST[ 'second-nom' ] ), true );
        } 
        }

add_action( 'init', 'ty_save_post_data' );

?>

1

Hook your form processing code to an action that runs before the page is rendered and no redirect will be necessary.

function wpd_check_submission(){
    if( isset( $_POST['start'] ) ) {
        // do your post insertion here
    }
}
add_action( 'init', 'wpd_check_submission' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.