Creating a Front-end Form with Advanced Custom Fields to Create New Posts

Advanced Custom Fields gives WordPress developers the ability to add all kinds of data onto their WordPress posts, taxonomy terms, etc. via form fields in the WordPress admin, but it also allows developers to create forms on the front-end to collect data from users and also to edit data found in the WordPress database. In turn, this data can be collected and emailed via a simple contact form, used to edit existing WordPress posts, or used to create new ones.

Giving site visitors the ability to create posts might sound like a bit of a security risk, but coupled with the right precautions, it can prove to be a huge time-saver in populating a site with content. For instance, suppose you’re creating a site for an event and you’d like to display the biographies and social media links of your presenters or attendees. Typically, this type of user generated content would need to solicited, collected and then most likely copied and pasted into new posts. Instead, via Advanced Custom Fields, you could create a form that would allow event attendees to fill-out the requested information and turn the submissions into posts. Very handy!

The ACF documentation contains, some example code that serves as a good introduction to the general form-creation concept.

Taking this idea a step further, here’s an example of an instance of the appropriate option configuration when calling acf_form(), using ACF v5. This configuration creates a new post upon form submission, saving the post as a draft, and assigning the post a certain category:


$args = array(
'post_id' => 'new_post',
'post_title' => true,
'post_content' => true,
'new_post' => array(
'post_status' => 'draft',
'post_type' => 'post',
'post_category' => array(8)
),
'submit_value' => 'Create Post'
);

acf_form($args);

It is worth noting that the above example does not actually use any ACF groups, instead relying solely on some of the built-in ACF functions (acf_form(), and acf_form_head()) to process the form! Of course, this could be extended further to include defined field groups, making the types of data to be collected and stored virtually limitless.

Leave a Reply