Displaying a single ACF field form

In my last post I described how to inject your own content into a form rendered by acf_form(). Today we’re doing the opposite by requesting a single ACF field’s form view. This example shows this technique in a <form> field but should work about anywhere.

What you’ll need

You’ll need the field key of the field you intend to display. You can reveal these in the Screen Options tab on the field group edit screen.

Get the field object

First you’ll need the field object. It’s an array representation of the settings you set in the field editor.

Clear the data

When ACF returns a field object, it will supply any data saved to the current post id (which is a global variable set during “the loop”). When we render the field, that data will be set ie. checkboxes will be checked. This may or may not be what you want. Remember that if you do want to show data from a specific post, just pass its ID as the second parameter to get_field_object(). I’m creating a search form and don’t want anything pre-selected so we just nullify the field objects data.

Render the field

Now we just call one of two internally defined ACF methods. I should remind you however that these are undocumented and not officially supported methods. You should always verify that they still work each time you upgrade ACF.

acf_form_head() WARNING

ACF instructs you to call acf_form_head() in the <head> of any page that uses acf_form(). Doing so will ensure enough CSS and JS to ensure the field behaves appropriately. However, it will also process any form data present in the $_POST array, which again, may not be what you want. I recommend you don’t use this technique on any page that does so. Further, this technique is untested with ACF’s more javascript heavy fields but is likely a matter of manually enqueuing or possibly just calling acf_enqueue_scripts() which is also undocumented.

Why not just use a simplified acf_form()?

I should point out that you can achieve almost the exact same result by calling acf_form with just the field keys you want with the <form> tag suppressed. That may very well suit your needs but you will always get the result of using acf_render_field_wrap( $field ) and possibly some additional overhead you don’t want.

Hope that helps!