Home › Forums › Front-end Issues › Front End Form to CPT data not saving
Hi everyone,
I’m having some issues with creating a front end form to post a custom post type. I’ve run into quite a few problems, while most have them have been resolved by hunting through the documentation and forums, a few issues remain. Following the instruction at Using acf_form to create a new post, I’ve gotten the form to show up, but submitting is still giving me issues.
First, the code:
The function
function add_new_player( $post_id )
{
// check if this is to be a new post
if( $post_id != 'new' )
{
return $post_id;
}
// Create a new post
$player_name_title = ($_POST["fields"]['field_5278542fc80cc']=='english' ? $_POST["fields"]['field_5278516dc80c5'] : $_POST["fields"]['field_522f30035c85b']);
$post = array(
'post_status' => 'publish' ,
'post_title' => $player_name_title ,
'post_type' => 'player' ,
);
// insert the post
$post_id = wp_insert_post( $post );
// update $_POST['return']
$_POST['return'] = add_query_arg( array('post_id' => $post_id), $_POST['return'] );
// return the new ID
return $post_id;
}
add_filter('acf/pre_save_post' , 'add_new_player' );
The form
<?php if ( !current_user_can( 'edit_posts' ) ) {
the_content();
}
else {
echo '<h3>'.__("Hello, ").$current_user->display_name.'<h3><div class="divider"></div>';
$options = array(
'post_id' => 'new', // post id to get field groups from and save data to
'field_groups' => array( 145 ), // this will find the field groups for this post (post ID's of the acf post objects)
'submit_value' => 'Register Player', // value for submit field
);
acf_form( $options );
}?>
The issues:
1) When I submit the form on the front end the page refreshes and gets stuck/no page is generated (same url). Not entirely sure what happens.
2) I look at the backend and find that the post has indeed been published, but with no title, and only the first field of the form filled out.
Any help with this would be greatly appreciated!
Hi @heykafei
Thanks for providing the data, your code looks great and I am surprised that you are having any issues at all.
Lets do some debugging to find out where the problem is:
1. your add_new_player function, before anothing else, can you simple print out the $_POST data like so:
<?php
echo '<pre>';
print_r($_POST);
echo '</pre>';
die;
?>
Does all the data look correct?
2. Print out the value of $player_name_title after you have set it’s value. Is that correct?
Apart from that, I’m not sure what the issue could be…
The 1 field being saved is a strange issue and may be related to running out of PHP memory during the save? Perhaps you could even check your server logs for any error reports during the save?
Let me know how you get on.
Thanks
E
Hi @elliot,
Thanks for responding and such a great plugin; it’s much appreciated and I’ve learned a lot reading through the documentation / code examples you’ve graciously provided.
I’ve gone ahead and checked if the data looks correct;
1. The $_POST data is correct, it’s exactly what I input into the form.
2. The value of $player_name_title is correctly output as well.
I was honestly assuming the problem was with the code I had mangled in their but I guess I’ll need to look elsewhere to solve this specific problem. I will most definitely look into my server logs and php memory limit, will reply when I find something!
Thanks again,
Kafei.
Hi @heykafei
You have a fantastic attitude! Great work, I’m sure the issue will surface soon and you can get it fixed.
Thanks
E
The topic ‘Front End Form to CPT data not saving’ 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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.