Support

Account

Home Forums Front-end Issues acf_form() validation and feedback

Solving

acf_form() validation and feedback

  • I’m creating a front-end form for submitting new post (ACF PRO latest version) with the following code:

    
    $options = array(
        'post_id' => 'new_post',
            'new_post' => array(
            'post_type'   	 => 'deal',
            'post_status'   => 'post'
        ),
        'html_before_fields' => $html_before_fields,
        'html_after_fields' => $html_after_fields,
        'uploader' => 'basic',
        'submit_value' => __("Create", 'domain'),
        'updated_message' => __("Your post was submitted successfully! Please wait for it to be approved.", 'domain'),
    );
    
    acf_form($options);
    

    And here is a snippet of my functions.php;

    
    function _action_ssd_acf_submit_post_pre_save( $post_id ) {
    
        if( empty($_POST['acf']) ) {
            return;
        }
    
        if ( !isset($_POST['acf_post_submit']) ) { // this is a hidden input field in the form
            return;
        }
    
        if (  ) { // some more checks here
            wp_mail(); //removed parameters for cleaner code, I send email to the admin
        }  
    
        if( empty(get_user_meta(get_current_user_id(), 'this_user_can_submit', true)) ) {
            exit;
            return;
        } 
    		
    	   
    }
    
    add_action( 'acf/pre_save_post', '_action_ssd_acf_submit_post_pre_save' );
    

    How can I stop the post from saving as it is always saved as Draft?

    Also I can’t seem to find a way to change the update_message when there is an error wth the submission. For example, if the user doesn’t have the needed user_meta.

    So, yeah, my problem is that I am unable to stop the submission of the post (in case of a validation error) and send a front end message feedback after the page has refreshed.

  • hi @exalted

    I think the problem lies with 'post_status' => 'post'

    Your post status should be publish. post isn’t a valid option for post_status, so this is why it’s defaulting to a draft post.

  • Yeah, ‘ I’ve ma ed a mistake with that example code. It should be

    'post_type'   	 => 'post',
     post_status'   => 'publish'

    I think I solved my problem with using:

    wp_redirect(add_query_arg( 'error', 'true', get_permalink($submit_page_id)));

    in the _action_ssd_acf_submit_post_pre_save function in the following hook:

    add_action( 'acf/pre_save_post', '_action_ssd_acf_submit_post_pre_save', 1 );

    This way I don’t use the return of the acf_form() function and redirect to the same page with the form but the parameters in the URL which I can get to show the error notice on that page.

    Don’t know if this is the right way though.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.