Support

Account

Home Forums General Issues Stop acf_form() from publishing a post.

Helping

Stop acf_form() from publishing a post.

  • I’ve created a front end form and am unable to stop it when the back end validation fails.

    Here is my form code:

    <?php
    	$options = array(
    		'post_id' => 'new_post',
    		'new_post' => array(
    			'post_type'   	 => 'cpt'
    		),
    		'uploader' => 'basic',
    		'submit_value' => __("Create", 'themename'),
    		'updated_message' => __("Deal created.", 'themename'),
    	);
    	acf_form($options);
    ?>

    And here is my back end:

    
    function acf_validate_form( $post_id ) {
    
    	if( empty($_POST['acf']) ) {
    		return;
    	}
    
    	// my_validation_function() returns false
        $validate = my_validation_function();
    
        if ( !$validate ) {
        	wp_redirect(add_query_arg(array('custom-error' => 'true'))); 
    		exit;
        }
    }

    I’ve tried any of the following hooks:

    add_action( 'acf/save_post', 'acf_validate_form', 20 );
    add_action( 'acf/save_post', 'acf_validate_form', 1 );
    add_action( 'acf/pre_save_post', 'acf_validate_form', 10, 1 );
    add_action( 'acf/pre_save_post', 'acf_validate_form', 1 );
    add_action( 'acf/save_post', 'acf_validate_form' );

    However the submitted post appears in the admin dashboard. How can I prevent it from submitting?

  • Try adding in an acf/pre_save_post filter.

    unset($_POST['acf']);

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

You must be logged in to reply to this topic.