Home › Forums › Backend Issues (wp-admin) › Saving acf_form() through admin-ajax responds with 400 Bad Request › Reply To: Saving acf_form() through admin-ajax responds with 400 Bad Request
Apparently this is not supposed to be done like this, or I’m just stupid 😀
I ended up sending the data along with my already sent custom JSON for my custom post type like this:
// Add ACF data to our update request, if available.
var acf_form = $('.has_acf #acf-form');
if(acf_form) {
// Serialize data in acf form.
var acf_data = $(acf_form).serializeArray();
// Apply every field to the data object, we're sending.
for(var i = 0; i < acf_data.length; i++) {
var field = acf_data[i];
data[field.name] = field.value;
}
}
Originally, I wanted to avoid doing anything more complicated on the PHP side, but it turned out to be quite easy. I added the following statements to the new
and update
ajax methods:
// Update ACF Fields
if( function_exists( 'update_field' ) && isset( $_POST['acf'] ) ) {
foreach( $_POST['acf'] as $field_name => $field_value ) {
update_field( $field_name, esc_attr( $field_value ), $post_id );
}
}
It seems to work fine 🙂 (I just want basic support, didn’t test repeaters and more complicated fields.)
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.