Support

Account

Home Forums ACF PRO Action hook when specific field changes

Solving

Action hook when specific field changes

  • Hi!

    There are any action hook that fire when a specific field is changed and saved in post/page?

    save_post action fire when 1, 2, 3 or all fields had changed. It doesn’t give me any data about what specific field had changed…

    I simply want an action where I can compare the old field value with the new posted value of a specific field (or a group of them) to make some logic to be stored in database.

    I’m using ACF PRO v5.3.9 and WP v4.5.3

    Thanks

  • Hi @giu-tae-kim

    I think you can use the acf/update_value filter or the acf/save_post action hooks to do it. Here’s the example how to do it with acf/update_value:

    function my_acf_update_value( $value, $post_id, $field  ) {
        // only do it to certain custom fields
        if( $field['name'] == 'custom_field_name' ) {
            
            // get the old (saved) value
            $old_value = get_field('custom_field_name', $post_id);
            
            // get the new (posted) value
            $new_value = $_POST['acf']['field_1234567890abc'];
            
            // check if the old value is the same as the new value
            if( $old_value != $new_value ) {
                // Do something if they are different
            } else {
                // Do something if they are the same
            }
        }
    
    	// don't forget to return to be saved in the database
        return $value;
        
    }
    
    // acf/update_value - filter for every field
    add_filter('acf/update_value', 'my_acf_update_value', 10, 3);

    Where ‘field_1234567890abc’ is the field key of your custom field(‘custom_field_name’ in this case).

    I hope this helps 🙂

  • Hello James and thanks for giving a hand!

    I find interesting this “update_value” filter but I think that the “save_post” is more suitable to my needs, as long I can put all fields logic in only one function.

    How can I access a specific field old and new posted value in acf/save_post action???

    Maybe:

    function my_acf_save_post( $post_id ) {
        
        // specific new field value
        $new_value = $_POST['acf']['field_abc123'];
        
        // specific old field value
        $old_value = get_field('custom_field_name', $post_id);
        
    }
    
    // run before ACF saves the $_POST['acf'] data
    add_action('acf/save_post', 'my_acf_save_post', 1);

    PS: Also can I use get_fields() instead get_field() in order to get all fields old values??

    Regards!

  • Hi @giu-tae-kim

    I believe your code should be working. Just take attention to advanced custom fields like post object, gallery, repeater, etc as the returned data from the database sometimes different from the posted one, even though they are actually referring to the same value.

    Regarding your second question, yes, I think you can use the get_fields() function instead.

    I hope this helps 🙂

  • Hi again. Many thanks but sadly I have some others questions….

    1) Inside the ‘acf/save_post’ hook the statement…

    $_POST['acf']['field_abc123'];

    … retrieve the new posted value of a certain field.

    Consider if the admin doesn’t write any new data inside that field in that post saving… that post value would fall back to the old posted value, or it will be simply empty?

    2) What do you mean that the gallery field would not return what I expect? Is that
    inconsistency normal?

    Many thanks!

  • Hi @giu-tae-kim

    When you load the form (backend or frontend), the fields will be filled with the saved (old) value. So, if there’s no new value set in the field, the $_POST data should contain the old value.

    Regarding your second question, if you check the database you will see that gallery field is saved as serialized image IDs. If you check the posted data, you will see that it is posted as an array of image IDs. And if you try to return the value using the get_field() function, you’ll see that the data is composed of an array of image objects. This is why I said that you need to make sure that you make the comparison with the right data.

    I hope this makes sense 🙂

  • Hi James

    Thank you very much. Your code worked for me very well.but it could not handle repeater fields. please help me to fix your code for repeaters.

  • @acf-support You’ve replied to my question here:

    https://support.advancedcustomfields.com/forums/topic/field-updates-after-post-update

    but I believe your given option in your first reply here above would be exactly what I need… I guess.

    Am I correct?

    What I want to accomplish:

    – A visitor makes a booking > A pending post is make with the booking fields filled in inside (booking status is auto set to PENDING) > Admin sets booking status to CONFIRMED > Email to the customer is being send.

    I thought your code would be just what I needed but no email is being send…

    This is what should happen when OLD and NEW value do not match:

    wp_mail( $to, $subject, $body, $headers );

Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘Action hook when specific field changes’ is closed to new replies.