I think we can all agree that Elliot Condon’s incredible WordPress plugin Advanced Custom Fields is a massive time saver. If you’re like me however, you’re constantly running into situations where ACF would be a perfect solution if not for certain understandable limitations. Undeterred, over the last year or so I’ve had to coax ACF to do some pretty cool things. I plan on creating a series of posts detailing my adventures into undocumented methods, copious use of hooks and downright hacks to achieve the UX I want while maintaining that sweet sweet ACF meta data.
Let’s kick it off with something less common but fairly simple. acf_form() was a welcome addition to the ACF toolset but out of the box, it does little more than spit out the exact same stack of custom fields as it does in the post editor screen. Conveniently it allows you to include the native post title and post content fields in your front end form. I recently needed to also include the excerpt field. Here’s how I did it.
ACF has been generous with hooks so that’s always the first place I look. In the case of acf_form() there really aren’t any hooks intended for augmenting the contents of the resulting <form> tag. I wanted to inject an excerpt field right after the Title and Content fields. The key was to detect when the ACF field immediately following my insertion point was about to be rendered. acf/prepare_field does just that.
Inserting the form field
Rather than modify the $field argument at all, I just echo’d my form field and returned $field untouched. I matched ACF’s markup for a consistent look but that’s up to you.
Saving the custom form field data
Now all we need to do is save the form data to the post’s excerpt data.
The use of wp_update_post() has the added affect of generating a revision but the ACF data doesn’t come along for the ride. Fixing that is a simple matter of hooking into _wp_put_post_revision and cloning the field data. I’ll cover that in another post.
Hope that helps!