Home Forums General Issues Callback for update succes


Callback for update succes

  • Is there a way to be sure that the update_field or add_row are successfully added to the database?
    I had some issues when some fields weren’t updating and i also had a hard time figuring out why. Is there a way to get an error message or a succes true or false boolean?

  • Hi woutervanerp,

    The acf/save_post action should help you here.

    This code should suffice for what you want, though I haven’t tested it:

    function my_acf_save_post_callback( $success ) {
        // Do what you'd like here
    function my_acf_save_post( $post_id ) {
        if ( !isset( $_POST['acf'] ) ){
        $fields = $_POST['acf'];
        add_action( 'acf/save_post', function( $post_id ) use ( $fields ) {
            remove_action( 'acf/save_post', $this );
            $db_fields = array();
            foreach( $fields as $field_key => $value ) {
                $db_fields[$field_key] = get_field( $field_key, $post_id );
            // Serialize the value arrays from $_POST and the Database, so that they are single-level arrays, and array_diff_assoc them to generate the array of differences...
            $success = empty( array_diff_assoc( array_map( 'serialize', $fields ), array_map( 'serialize', $db_fields ) ) );
            my_acf_save_post_callback( $success );
        }, 11 );
    add_action( 'acf/save_post', 'my_acf_save_post', 9 );


    Actually, that would work for a form save, but not for update_field or add_row, which works entirely server-side.

    Code Update:

    function my_acf_update_field_wrapper( $field_key, $value, $callback, $post_id = null ) {
        $new_values = array(
            $field_key => $value
        // We need to hook the default WordPress save_post action
        add_action( 'save_post', function( $post_id ) use ( $new_values ) {
            remove_action( 'save_post', $this );
            $db_fields = array();
            foreach( $new_values as $field_key => $value ) {
                $db_fields[$field_key] = get_field( $field_key, $post_id );
            // Serialize the value arrays from $new_values and the Database, so that they are single-level arrays, and array_diff_assoc them to generate the array of differences...
            $callback ( empty( array_diff_assoc( array_map( 'serialize', $fields ), array_map( 'serialize', $db_fields ) ) ) );
        } );
        if ( $post_id == null ) {
            update_field( $field_key, $value );
        } else {
            update_field( $field_key, $value, $post_id );
    function my_acf_save_post_callback( $success ) {
        // Do what you'd like here
    function my_func() {
        // We need to update a value here
        my_acf_update_field_wrapper( 'field_abcdef123456', 'new-value', 'my_acf_save_post_callback' );


    Help your fellow forum-goers and moderation team; if a response solves your problem, please mark it as the solution. Thank you!

  • Hi Jonathan,

    Thanks for your reaction and i’m going to try your code right now!
    Is there also a way i can also return a Error when there is no Succes? Preferably also with an error code for me as a developer to keep everything stabble.

    Thanks again

  • Or what is the proper way to inform the frontend that the code is succesfully updated or added?

  • That sounds tricky; I think I need to know more about your use case.

    Could you give me a basic walkthrough of what you are trying to do? It will let me come up with a better solution for you 🙂

  • I want logged in users to edit their posts that have extra fields but they don’t get succes or error messages when the fields are or aren’t updated. Returning an error is also vey helpful for me if something goes wrong so i can fix it.

    Also this ‘fields’ consist out of a repeater field and i already had some trouble with not adding and it took a long time to figure out why.

  • So these are editors or contributors editing their own posts in the backend with ACF fields? Or is it a frontend form using acf_form()?

  • Frontend and without the acf_form() so i can style it properly.

  • Hi woutervanerp;

    I apologize for the long delay between now and my last response, things have been a bit hectic on my end.

    I’m not sure how you are executing your update if you aren’t using acf_form() to render the frontend form. If you are trying to update the post via standard WP functions, the acf action above won’t be fired.

    If you are absolutely sure you cannot use acf_form, then you would need to be properly serializing everything when storing to the database.

    That all said, as a frontend form, you would want to use the original version but with the standard save_post action as the hook instead of acf/save_post. You would also want to check for the proper form element names instead of $_POST[‘acf’][*].

    Depending on how you code this (I am assuming AJAX), you could return a status code in several different ways. The easiest in an AJAX call would be to add a json_encode followed by wp_die() at the end of your success callback.

    echo json_encode( array(
        'success'=> $success
    ) );
  • i just use update_field() at the moment and i don’t use ajax.
    So i make the form and update with $_POST.

  • update_field returns true on success and false on failure, but considers a useless update (meaning the value has not changed) failure.

    You could call update_field, and then compare the new value in $_POST to the value returned by get_field to see if the update actually took place correctly, and return a message based on that test.

    The underlying code is using WordPress’ update_metadata function; you could probably get the last MySQL error (maybe using $wpdb->last_error?) to get a more detailed error message on why an UPDATE statement failed.

  • Hmm that’s alot of work and ugly code. First of all thank you for responding to my question. By your answers i was curious how to communicate with ACF trough Ajax?

    Didn’t quite see that in the documentation or tutorials. If i would have more control over my ACF fields trough ajax that would great!

    Is it hard to update and request fields trough ajax? and can i check for changes?

    So that more people on one page can see the change’s without reloading the page.

  • It isn’t so much more control, as it is a better frontend experience. People typically don’t want the entire page to have to refresh these days when submitting a form that shouldn’t change the rest of the page layout.

    Wordpress has builtin capabilities for adding custom AJAX function endpoints.

    You can add an AJAX handler using this code:

    // Sent from admin_users
    add_action( 'wp_ajax_my_ajax_action', 'my_ajax_handler' );
    // Sent from non-admin and anonymous users
    add_action( 'wp_ajax_nopriv_my_ajax_action', 'my_ajax_handler' );
    function my_ajax_handler() {
        // Access $_POST to see what your javascript sent to the AJAX endpoint here
        $result = array(
            'success' => false,
            'errorMessage' => 'Uh oh! An error has occurred...'
        // Manipulate your custom fields here, modifying $result as necessary
        echo json_encode( $result );
        // Note that AJAX handlers should exit() or wp_die() instead of returning
    wp_localize_script( 'your-js-script-id', 'ajax_object', array( 
        'ajax_url' => admin_url( 'admin-ajax.php' )
    ) );

    You may call this handler by sending an AJAX POST to the ajax_object.ajax_url defined above with action set to my_ajax_action (note that this is the suffix of the add_action calls’ action name.

    For example:

    var data = {
        'action': 'my_ajax_action',
        'someOtherData': 0
    };, data, function(response) {
        response = JSON.parse(response);
        if (response.success) {
            // Success processing
        } else {
            // What to do on failure....
Viewing 14 posts - 1 through 14 (of 14 total)

The topic ‘Callback for update succes’ is closed to new replies.