Home › Forums › Bug Reports › Frontend Form Issues: pre_save_post & save_post
I have two functions to process a frontend form. These functions used to properly process my form as expected until v4.2.0.
The pre_save function:
/**
* ACF Pre Save Post
*
* @return int $post_id The ID of the new post;
*/
function my_acf_pre_save_post( $post_id ) {
if( $post_id != 'new' ) return $post_id;
$post_data = array(
'post_title' => 'Draft Post',
'post_status' => 'draft',
'post_author' => get_current_user_id()
);
$post_id = wp_insert_post( $post_data );
return $post_id;
}
add_filter( 'acf/pre_save_post' , 'my_acf_pre_save_post', 10, 2 );
… And the save_post function:
/**
* Add Post Data to ACF Form Post
*
* @return void
*/
function my_acf_save_post( $post_id ) {
// Don't do this on the ACF post type
if ( get_post_type( $post_id ) == 'acf' ) return;
// Get the Fields
$fields = get_field_objects( $post_id );
// Get the Post Type in the Hidden Field Default Value
$cpt = $fields["form_field_key_post_type"]['value'];
// Prevent Infinite Looping...
remove_action( 'acf/save_post', 'my_acf_save_post' );
// Grab Post Data from the Form
$post = array(
'ID' => $post_id,
'post_type' => $cpt
'post_title' => $fields['form_field_key_post_title']['value'],
'post_content' => $fields['form_field_key_post_content']['value'],
'post_status' => $fields['form_field_key_post_status']['value']
);
// Update the Post
wp_update_post( $post );
// Continue save action
add_action( 'acf/save_post', 'my_save_post' );
// Set the Return URL in Case of 'new' Post
$_POST['return'] = add_query_arg( 'updated', 'true', get_permalink( $post_id ) );
}
add_action( 'acf/save_post', 'my_acf_save_post', 10, 1 );
Adding these debugging statements into the pre_save_post
produced unexpected results considering acf_form_head
sets $post_id
to the value of $_POST['post_id']
.
/**
* ACF Pre Save Post
*
* @return int $post_id The ID of the new post;
*/
function my_acf_pre_save_post( $post_id ) {
var_dump( $post_id ); // OUTPUT: NULL
var_dump( $_POST['post_id'] ); // OUTPUT: string(3) "new"
die();
if( $post_id != 'new' ) return $post_id;
$post_data = array(
'post_title' => 'Draft Listing',
'post_status' => 'draft',
'post_author' => get_current_user_id()
);
$post_id = wp_insert_post( $post_data );
return $post_id;
}
add_filter( 'acf/pre_save_post' , 'mri_jb_acf_pre_save_post', 10, 2 );
Hi @tatemz
I’m not 100% sure why the $post_id is null, can you take out your custom acf/save_post filter.
Does the pre_save_post filter work correctly now?
Also, can you edit the code file and debug the $post_id param there too?
It is pretty weird huh? I’m absolutely weirded out by this one…
Okay, the save_post action is commented out and the following debugging statements were added to acf_form_head
(results below);
function acf_form_head()
{
// global vars
global $post_id;
// verify nonce
if( isset($_POST['acf_nonce']) && wp_verify_nonce($_POST['acf_nonce'], 'input') )
{
var_dump($post_id); // OUTPUT: NULL
// $post_id to save against
$post_id = $_POST['post_id'];
var_dump($post_id); // OUTPUT: string(3) "new"
// allow for custom save
$post_id = apply_filters('acf/pre_save_post', $post_id);
var_dump($post_id); // OUTPUT: NULL
die();
// save the data
do_action('acf/save_post', $post_id);
// ...
}
// ...
}
I haven’t looked deep enough into the code to see how you are using the new save_post_lock
& save_post_unlock
functions, but off the top of your head, might that have any effect on this situation?
Aha! I commented out your save_post_lock
and save_post_unlock
filters and all is well.
Adding a blank return statement to those functions did not solve this issue either…
However, adding a return $post_id;
to those functions did solve that issue.
No return statements on filter callbacks leaves the filtered variable undefined.
Thoughts?
function save_post_lock( $post_id )
{
$GLOBALS['acf_save_lock'] = $post_id;
return $post_id;
}
function save_post_unlock( $post_id )
{
$GLOBALS['acf_save_lock'] = false;
return $post_id;
}
I’m having what I think is the same issue. I created a front-end form a few days ago and it was working fine. Then my client updated to the latest version of ACF, and suddenly the form no longer saves the data. It doesn’t give any error messages, it just doesn’t actually create the new item.
The front-end form is here:
http://cookingcontestcentral.com/submit-a-recipe/
Let me know how I can help troubleshoot?
Edit the two functions above as I did and all should work.
WP filters need return statements (unless the NULL value is desired).
Hopefully an update will come our way soon, but in the meantime, you will have to be comfortable with editing the plugin itself.
Already submitted a pull request ( hopefully I didn’t miss anything ):
https://github.com/elliotcondon/acf/pull/131
Where can I find those functions? I just poked around a few of the theme files but didn’t locate them.
Thanks!
Here’s the edited forked repo:
https://github.com/tatemz/acf
Here’s the link to the updated zip file:
https://github.com/tatemz/acf/archive/master.zip
Changes were made to acf.php
Hooray! Just replaced the acf.php file with the one from the master.zip and it’s working again.
Thanks! 🙂
Hi @tatemz
You are a genius! Thanks mate. Yes, the locking actions need to return the $post_id, my bad.
This will be included in 4.2.1 (should be out in a few days)
The topic ‘Frontend Form Issues: pre_save_post & save_post’ 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.