Home › Forums › General Issues › Callback for update succes › Reply To: Callback for update succes
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:
<?php
function my_acf_save_post_callback( $success ) {
// Do what you'd like here
}
function my_acf_save_post( $post_id ) {
if ( !isset( $_POST['acf'] ) ){
return;
}
$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 );
?>
EDIT:
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!
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.