ACF with Ajax for new records persists values

I have been trying to get ACF to behave on my frontend, so that visitors can submit custom post types into the database with AJAX. Editing a post works like a charm, but when trying to insert a new post, whatever the user enters gets written to wp_options table with an option name like _{whatever I set as new post ID)_(field name)_new. For instance, if I send new_post as my post ID, and my field is called name, _new_post_name’ will be added to mywp_options` table which will be then used as default value for next new post.

This is the code that renders the acf form (this is called via ajax)

function new_post_callback() {
check_ajax_referer(‘my-special-string’, ‘security’);
switch ($_POST[‘post_type’]) {
case “contacts”:
$field_group = 80;
break;
case “vacations”:
$field_group = 103;
break;
case “messages”:
$field_group = 84;
break;
default:
wp_die();
}
$settings = array(
‘id’ => ‘acf-form’,
‘post_id’ => ‘new_post’,
‘new_post’ => array(‘post_type’ => $_POST[‘post_type’]),
‘return’ => ”,
‘field_groups’=>array($field_group),
‘html_before_fields’ => ”,
‘html_after_fields’ => ”,
‘submit_value’ => __(“Insert”, ‘acf’),
‘updated_message’ => __(“Post inserted”, ‘acf’),
‘label_placement’ => ‘top’,
‘instruction_placement’ => ‘label’,
‘honeypot’ => true,
‘html_submit_button’ => ‘<input type=”submit” class=”acf-button button button-primary button-large” value=”%s” />’,
‘kses’ => false
);
acf_form($settings);
wp_die();
}

This javascript is executed after acf_form is returned via AJAX, to prevent ACF from posting and instead call my save action:

function initACF(contentType) {
$(document).trigger(‘acf/setup_fields’, [
$(‘#poststuff’)
])
$(‘.acf-form’).on(‘submit’, (event) => {
event.preventDefault();

var form_data = {‘action’ : ‘acf/validate_save_post’, ‘post_type’: contentType};
jQuery(‘.acf-form :input’).each(function() {
form_data[jQuery(this).attr(‘name’)] = jQuery(this).val();
})

form_data.action = ‘save_my_data’;
$(‘html,body’).css(‘cursor’, ‘wait’);
jQuery.post(MyAjax.ajaxurl, form_data).done(function(save_data) {
$(‘html,body’).css(‘cursor’, ‘default’);
getContent(contentType);
})
});
}

And here’s my AJAX callback to save the form (which is kinda weird because I don’t need this for editing posts, but if I don’t have it for new posts, they won’t be saved)

function my_acf_save_post($post_id) {
if ($post_id !== ‘new_post’) {
return $post_id;
}

$post = array(
‘post_status’ => ‘private’,
‘post_type’ => $_POST[‘post_type’]);
switch ($_POST[‘post_type’]) {
case ‘contacts’:
$fields = array(‘name’=>$_POST[‘fields’][NAME_FIELD], ’email’=>$_POST[‘fields’][EMAIL_FIELD]);
break;
case ‘vacations’:
$fields = array(‘date_from’=>$_POST[‘fields’][DATE_FROM_FIELD], ‘date_to’=>$_POST[‘fields’][DATE_TO_FIELD]);
break;
case ‘messages’:
$fields = array(‘subject’=>$_POST[‘fields’][SUBJECT_FIELD], ‘receipients’=>$_POST[‘fields’][RECEIPIENTS_FIELD], ‘message’=>$_POST[‘fields’][MESSAGE_FIELD], ‘release_in’=>$_POST[RELEASE_IN_FIELD]);
break;
default:
return $post_id;
}

$post_id = wp_insert_post($post);
foreach ($fields as $key => $value) {
update_field($key, $value, $post_id);
}

return $post_id;
}

I have traced it down to line 200 of advanced-custom-fields/core/fields/_functions.php which basically says if post_id is a string, then persist these values in the database.

Is there any reason why my new posts are not stored automatically, and also, why passing new_post as post ID triggers this weird behavior? Am I missing anything here?

Read more here:: ACF with Ajax for new records persists values

Leave a Reply

Your email address will not be published. Required fields are marked *