Support

Account

Home Forums General Issues Frontend form – post via ajax? Reply To: Frontend form – post via ajax?

  • Just to update as I realised I never finished this – to get past the problem of it returning the url, I added an extra hook for after ACF_Head had saved which wrote out a new form with the new post_id which I can replace the existing one with. That way I can resave the form again without creating another new entry.

    So my final save code looks like this:

    function my_pre_save_post($post_id) {
    	if( $post_id == 'new_data' ) {
    	    $post = array(
    	        'post_status'  => 'publish' ,
    	        'post_type'  => 'saved_data',
    	    );
    	    $post_id = wp_insert_post($post); 
    	}
    	return $post_id;
    }
    
    function my_acf_save_post( $post_id )
    {
    	acf_form(array(
    		'post_id'		=> $post_id,
    		'field_groups' => array(180)
    	));
    	exit();
        
    }
    
    add_filter( 'acf/pre_save_post', 'my_pre_save_post' );
    add_action( 'wp_ajax_save_app_data', 'acf_form_head' );
    add_action( 'wp_ajax_nopriv_save_app_data', 'acf_form_head' );
    add_action( 'acf/save_post', 'my_acf_save_post', 20 );

    If anyone needs any help with this let me know but I also realise this might not be the most efficient way to achieve an AJAX save…