Support

Account

Home Forums Backend Issues (wp-admin) Update ACF fields in WordPress admin using Ajax Reply To: Update ACF fields in WordPress admin using Ajax

  • … It is necessary to write and add code to everything in the success section?

    Yes.

    none of this has been tested

    
    // changes to ajax function
    $source_fields = get_field_objects($another_post_id);
    $response = array();
    foreach ($source_fields as $field) {
      $response[$field['key']] = $field['value'];
    }
    echo json_encode($response);
    exit;
    
    
    // changes to success code
    // basic example up updating a field
    for (var key in json) {
      var field = acf.getField(key);
      field.val(json[key]);
    }
    

    As to your other question, if you are updating the field values on the active page using AJAX then there is no reason to have an acf/save_post action that duplicates the work done with AJAX. Instead of a button that needs to be clicked you would create an on chnage event for the field containing the post ID that you want to get the values from so that the fields you want to update will be done automatically whenever they user alters their selection. If you keep the button approach then yes you will need to duplicate the work using acf/save_post since you will not know if the values have been updated already or not.