Support

Account

Home Forums ACF PRO acf_form Use form but prevent post and post_meta creation

Solving

acf_form Use form but prevent post and post_meta creation

  • Hi there,

    I want to use acf_form only for the form itself (to hook into validations, etc instead of having to code a custom form. Essentially, I want to use fields to complete other actions via acf/save_post (specifically running actions for an External, Third Party API like Salesforce based on the acf_form fields)

    My form

    
    $options = array(
    	'id'			=> 'my-id',
    	'post_id'		=> false,
    	'new_post'		=> false,
    	'post_title'		=> false,
    	'field_groups'		=> array(
    		1234
    	),
    	'submit_value' 		=> 'Submit',
    	'uploader'			=> 'basic',
    );
     acf_form($options);
    

    I am able to execute the third-party API functions with the above, however, the post where I’m embedding the above acf_form creates post_meta on that post. What I would like to do is prevent this from happening (I am currently just resetting/deleting all of the field values upon completion, but would like to know if there’s a better way.

    Thanks in advance!

  • This has not been tested and I don’t know if it will work.

    Use an acf/pre_save_post filter https://www.advancedcustomfields.com/resources/acf-pre_save_post/

    You’ll need to implement some checking to make sure you’re dealing with the right form, and you’ll also need to look in $_POST['acf'] to get the values.

    Once you’re done processing the form

    
    unset($_POST['acf']);
    

    ACF only begins the save process based on this having a value…. I think.

  • Thanks John!

    I will give this a go

  • I’m not sure the above works anymore. I keep running into empty posts created and can only be deleted from the database.

    The ACF method pre_save_post expects something numeric OR ‘new_post’ so what I do is pass neither of those things but instead 'post_id' => 'no_new_post' which seems to work. I also use acf/save_post hook at a priority less than 10 ( in my case 4 ).

    I also get errors when unsetting $_POST['acf'] so I’ve been opting to use $_POST['acf'] = array();

    Undefined index: acf in advanced-custom-fields-pro\includes\acf-form-functions.php on line 157

  • @howdy_mcgee I got that undefined index when I foolishly did do_action() instead of add_action(). Maybe you did the same.

  • acf/pre_save_post happens before the post is created.

    Trying to do it on acf/save_post, no matter what priority you set, will always happen after the post is created.

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

You must be logged in to reply to this topic.