Support

Account

Home Forums Front-end Issues acf_form() for contact form without creating a post

Solved

acf_form() for contact form without creating a post

  • I’m trying to use acf_form() to render a form on my contact page but I don’t want to create a new post upon submit. I just want to send an email via wp_mail() with the values.

    I’m not really sure if there’s a way to cleanly short circuit acf’s form processing behavior. Any help would be great.

  • I’ve wondered about this myself. After looking through the ACF code I believe that it possible. I have not tested this but it looks like if you unset $_POST['acf'] in your afv/pre_save_post filter that this will effectively short circuit things and stop ACF from saving the post.

  • Hmm, not a bad idea to play with. That may create an issue as the form processor needs that goodies in there.
    Here’s what I’m currently using as my solution but I’d love to hear if it’s faulty.

    Basically I made a copy of acf_form_head() and removed the part that tries to update/create posts. Just put this before get_header() instead of acf_form_head()

    https://gist.github.com/joelstransky/c87cc56923abe15b144b

  • That may work as well. The reason that I say to just unset $_POST[‘acf’] is that this is what acf_form_head() does.

    1) Verify the nonce
    2) Validate the values
    3) Runs your pre_save_post filter if any. I’m assuming that you’d put your code to send an email into this filter
    4) Calls acf_save_post()

    The first thing that acf_save_post() does it to see if $_POST[‘acf’] contains anything, if it’s empty then it returns without saving anything.

    The only place this will be a problem is if there is an error during the submission process, for example they didn’t enter an email address into an email field.

    Like I said, your solution might work as well, you just need to remove the line that calls acf_save_post()

  • I didn’t realize acf_save_post() returns early. That’s a far better approach imo since I can just leverage filters rather than hacky wet alterations.

    There’s already enough client side validation to prevent an empty email field etc. so I’m marking your post as the answer. well done.

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

The topic ‘acf_form() for contact form without creating a post’ is closed to new replies.