I have (hopefully) a quick question. Are front-end forms part of the standard version for ACF or just the pro version. I ask because I can get a form to appear in using the following code, but I can’t get it to create new posts on submission. Is that a pro feature? Thanks!
functions.php
function pre_save_request_form($post_id) {
// check if there's a new request based on post id
if ($post_id != 'new') {
return $post_id;
}
$post = array(
'post_status' => 'publish',
'post_title' => 'Request title???',
'post_type' => 'post'
);
// insert the posts
$post_id = wp_insert_post($post);
// return the new id
var_dump($post_id);
return $post_id;
}
add_filter('acf/pre_save_post', 'pre_save_request_form');<code></code>
header.php
<?php acf_form_head(); ?>
request-form.php
<?php
// try setting options for the form to send it to the back end
// as a new post and redirect to a thank you page.
// $options = array(
// 'post_id' => 'new_post',
// 'post_title' => true,
// 'post_content' => true,
// 'new_post' => array(
// 'post_type' => 'request_form',
// 'post_status' => 'publish'
// ),
// 'return' => home_url('thank-you'),
// 'submit_value' => 'Send'
// );
// var_dump($options);
// the $options variable doesn't breaks my form on the front-end when I pass it in...
acf_form();
?>
Hi @aberkow
There are some errors in your code. For example, you should check if the ID is “new_post” not “new”:
if ($post_id != 'new_post') {
Also, for the free version, there’s no “new_post” option for the acf_form()
function. Kindly check this page to learn how to add the form on the front end:
https://www.advancedcustomfields.com/resources/using-acf_form-to-create-a-new-post/?version=4.
I hope this helps 🙂
hi @James,
Thanks very much. I can’t believe I did this, but the group I’m working with is using v4 and I was looking at the v5 docs.
Can you clarify a point for me? In the v4 article of the link you posted and in v4 for acf_form()
, there’s an option for field_groups
. It should be an array, but I’m not clear on what it’s an array of. Is there a place to find an id for the group?
Similarly, in the same post, where the code is `function my_pre_save_post($post_id) {
if ($post_id !== ‘new_post’) {
return $post_id;
}
$title = $_POST[‘fields’][‘field_123456’];
$post = array(
‘post_status’ => ‘draft’,
‘post_type’ => ‘post’,
‘post_title’ => $title
);
$post_id = wp_insert_post($post);
return $post_id;
}
add_filter(‘acf/pre_save_post’, ‘my_pre_save_post’);`
Where do I get the ‘field_1234556` value from?
Ok. I’m a little closer using this code. Still need to be able to create new posts (and/or custom posts?), but at least I solved one of the problems.
<?php
$fields = get_field_objects();
$fieldGroup;
foreach ($fields as $field) {
$fieldGroup = $field['field_group'];
}
$options = array(
'post_id' => 'new_post',
// field groups has to be an array even though it's just one int...
'field_groups' => array($fieldGroup),
'submit_value' => 'Start a request',
);
acf_form($options);
?>
Hi @aberkow
You need to make sure that you have the code to handle the saving process in your functions.php file.
If you already have it, could you please share your XML export file and the code in your functions.php so I can test it out on my installation?
Thanks 🙂
@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);
The topic ‘Front-end forms pro or no?’ is closed to new replies.
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.