Home › Forums › ACF PRO › Front-end forms pro or no? › Reply To: Front-end forms pro or no?
@james – Thanks. I got it to work late yesterday. In the end, I used the following code. The area where I had to do a little digging was getting the field_group
property assigned correctly. After that I set the post_id
to 'new'
in both files and I’m set.
Edit – changed the check on $post_id
to 'new_post'
to clear the form after each use.
function pre_save_request_form($post_id) {
// check if there's a new request based on post id
var_export($post_id);
// 'new' vs 'new_post'
if ($post_id != 'new_post') {
return $post_id;
}
if (is_admin()) {
return;
}
$post = array(
// check to see if a 'draft' status can be used.
'post_status' => 'draft',
// make the title a date w/ timestamp.
'post_title' => date("F j, Y, g:i a"),
'post_type' => 'request_form'
);
// insert the posts
$post_id = wp_insert_post($post);
// return the new id
return $post_id;
}
add_filter('acf/pre_save_post', 'pre_save_request_form');
/*
Enable a front end ACF form which will generate a new post on submit.
*/
$fields = get_field_objects();
$fieldGroup;
// var_dump($fields);
foreach ($fields as $field) {
$fieldGroup = $field['field_group'];
// var_dump($field, $fieldGroup);
}
// var_dump($fieldGroup);
$options = array(
'post_id' => 'new_post',
// field groups has to be an array...
'field_groups' => array($fieldGroup),
'submit_value' => 'Start a request',
'return' => home_url('thank-you')
);
acf_form($options);
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.