Home › Forums › General Issues › Front-end form not saving entries
Hi,
I have a front-end form which creates a post as a custom post type. The form already has a submit button for users and I’m trying to get ACF fields to work in this existing form.
I’ve created a simple text field but on saving the custom post, it doesn’t save the data in either the front-end nor the back end when viewing it. I see the custom field but always shows the placeholder text I set.
Here’s my code (Mmy custom post ID is called spot_id):
<?php
$options = array(
'post_id' => $post->ID,
'field_groups' => array(),
'form' => false,
'form_attributes' => array(
'id' => 'post',
'class' => '',
'action' => '',
'method' => 'post',
),
'return' => add_query_arg( 'updated', 'true', get_permalink() ),
'html_before_fields' => '',
'html_after_fields' => '',
'submit_value' => 'Update',
'updated_message' => 'Post updated.',
);
acf_form( $options );
?>
My question is two-fold:
1. How can I get the data to save against the field entry? At the top of my template file I have this:
$spot_id = $_GET['id'];
$spot = get_post($spot_id);
…and if I try to change the ACF code to
'post_id' => $spot,
…it still doesn’t save the data.
2. I can remove the form update button with form set to false (so I can use the existing save button only), is there a way to keep the inserted data in the field if the user wants to edit the listing (post) in future. Maybe when I can sort (1) out, will happen anyway?
Any help on this would be most appreciated.
Actually, if I add a value in the back-end of the custom post, it shows correctly in the front-end form when I go back to edit it – so that works.
It’s just a case of getting the acf value to save in my custom post type post.
Certainly have. I also had to add the admin styles snippet into functions too. Is there a way to test if the acf_form_head has been done correctly?
Although, I suspect the issue is trying to get my custom post type linked to the acf form.
Hi @phil.owen
I just re-read your code above and noticed that you have set form => false
. This means there will be no form
tag, and no data will ever be posted!
Perhaps this is why your form is not saving.
You can test the acf_form_head function by editing the function itself which is found in the core/api.php file.
Thanks
E
It was true originally, but I tested it with false and pasted here the latest code.
Even when set to true, I still can’t get it working.
<?php
$options = array(
post_id' => $post->ID,
'field_groups' => array(),
'form' => true,
'form_attributes' => array(
'id' => 'spot',
'class' => '',
'action' => '',
'method' => 'post',
),
'return' => add_query_arg( 'updated', 'true', get_permalink() ),
'html_before_fields' => '',
'html_after_fields' => '',
'submit_value' => 'Update',
'updated_message' => 'Post updated.'
);
acf_form( $options );
?>
Also is there a way to remove the submit button? I’m already using this inside a form in the front-end, so only want the field to be part of the existing form, rather than creating a form within a form.
Still can’t get the form saving the data wwhen added front-end.
Hi @phil.owen
Thanks for the follow up. I believe the entire issue is that you are using acf_form
within another form. This explains why the acf_form_head function is not running correctly.
Where does your custom form post to? Does it re-direct the page?
The acf_form_head function expects no redirection, and for the $_POST data to exist at the time it is called (at the top of the template file).
Does this help?
Thanks
E
Hello everyone, I don’t know why my form is not saving in my custom post type?
here is my code:
add_shortcode( ‘schmooze-post-form’, ‘schmoozePostForm’ );
function schmoozePostForm() {
acf_form(array(
‘post_id’ => ‘new_post’,
‘post_title’ => true,
‘field_groups’ => array(1732),
‘form’ => true,
‘new_post’ => array(
‘post_type’ => ‘schmooze_blog’,
‘post_status’ => ‘publish’
),
‘submit_value’ => __(“Submit New Post”, ‘acf’)
));
}
I’m already using this inside a form in the front-end, so only want the field to be part of the existing form, rather than creating a form within a form. MyKFCExperience
actually this wasn’t working for me, anyway thanks for your efforts mykfcexperience
The topic ‘Front-end form not saving entries’ 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.