Home › Forums › General Issues › Frontend form – post via ajax? › Reply To: Frontend form – post via ajax?
Just to update as I realised I never finished this – to get past the problem of it returning the url, I added an extra hook for after ACF_Head had saved which wrote out a new form with the new post_id which I can replace the existing one with. That way I can resave the form again without creating another new entry.
So my final save code looks like this:
function my_pre_save_post($post_id) {
if( $post_id == 'new_data' ) {
$post = array(
'post_status' => 'publish' ,
'post_type' => 'saved_data',
);
$post_id = wp_insert_post($post);
}
return $post_id;
}
function my_acf_save_post( $post_id )
{
acf_form(array(
'post_id' => $post_id,
'field_groups' => array(180)
));
exit();
}
add_filter( 'acf/pre_save_post', 'my_pre_save_post' );
add_action( 'wp_ajax_save_app_data', 'acf_form_head' );
add_action( 'wp_ajax_nopriv_save_app_data', 'acf_form_head' );
add_action( 'acf/save_post', 'my_acf_save_post', 20 );
If anyone needs any help with this let me know but I also realise this might not be the most efficient way to achieve an AJAX save…
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!
2022 was a big year for Advanced Custom Fields, with more capabilities, a new generation of ACF Blocks, a refreshed UI, and a new home. Our year in review post looks at advancements we’ve made and offers a glimpse of the future.
— Advanced Custom Fields (@wp_acf) January 6, 2023
https://t.co/HahJUCcyH4
© 2023 Advanced Custom Fields.
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.