• Resolved Elliot Taylor

    (@raisonon)


    Hi, Any advice appreciated on getting a success message on front-end submission.

    I am outputing a single meta-box as per this guide: https://github.com/WebDevStudios/CMB2/wiki/Bringing-Metaboxes-to-the-Front-End

    And I’ve kept the array param for my new_cmb2_box ‘save_fields’ as true. So it saves automatically and works fine. I now want to show a successfully submitted message on submit.

    I saw in the WebDev Studio post on FE submission, that they have a success message. But that involves writing a new submission function and disabling the save_fields option.

    I’ve repurposed that function a bit to show errors, but was wondering if there is a simple way to check for post submission to complete my function?

    You’ll see in my function that I only output errors. Any ideas here (or if this is possible without setting the save_fields to false and managing the submission through the function.

    Perhaps this could also be modified to redirect as well. As I saw was possible in the CMB2 snippets page… Hope that explains what I’m doing enough and thanks for help.

    function wds_handle_frontend_new_post_form_submission( $cmb, $post_data = array() ) {
    
    	// If no form submission, bail
    	if ( empty( $_POST ) ) {
    		return false;
    	}
    
    	// check required $_POST variables and security nonce
    	if (
    		! isset( $_POST['submit-cmb'], $_POST['object_id'], $_POST[ $cmb->nonce() ] )
    		|| ! wp_verify_nonce( $_POST[ $cmb->nonce() ], $cmb->nonce() )
    	) {
    		return new WP_Error( 'security_fail', __( 'Security check failed.' ) );
    	}
    
    	if ( empty( $_POST['gift_description'] ) ) {
    		return new WP_Error( 'post_data_missing', __( 'Gift Description is Empty.' ) );
    	}
    
    	if ( empty( $_POST['site_admin_first_email'] ) ) {
    		return new WP_Error( 'post_data_missing', __( 'Welcome Email is Empty.' ) );
    	}
    
    	// Do WordPress insert_post stuff
    	// Check if post submitted and return true
    	
    
    	return $new_submission_id;
    
    	/*
    	 * Redirect back to the form page with a query variable with the new post ID.
    	 * This will help double-submissions with browser refreshes
    	 */
    //	wp_redirect( esc_url_raw( add_query_arg( 'post_submitted', $new_submission_id ) ) );
    //	exit;
    
    }
Viewing 1 replies (of 1 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    If your ‘post_submitted’ query arg is staying on the resulting page after submission, you could check for that being present at the to of your shortcode output, and display a message then.

    if ( ! empty( $_GET['post_submitted'] ) ) {
        echo 'Successful submission!';
    }
    

    This way something shows only if something is set there in the query arg. Since you’re not using the value at all, and just checking that one exists, sanitization doesn’t take very high of a priority in this case.

Viewing 1 replies (of 1 total)
  • The topic ‘Success Message on Front End Submission’ is closed to new replies.