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

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.