Last active
July 16, 2020 10:24
-
-
Save RadGH/cc8ceeaf2a413dac1b947c1cbfea1282 to your computer and use it in GitHub Desktop.
Queue an action that occurs after acf has completed saving a post, providing the callback, post id, and arguments up front. Useful if other fields that are saved with the post need to be overwritten.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Triggers an action after the "acf/save_post" action, priority 1000. | |
* If hooks are not called, they will be called at the end of the PHP thread | |
* | |
* Specify your callback as the first argument, and any arguments to pass to it as the second argument. | |
* | |
* @param callable Callback | |
* @param post_id The ACF-formatted post id you want to hook into | |
* @param mixed Additional parameters to pass to the callback | |
* | |
* @return array|null | |
*/ | |
function add_action_after_save_post( $cb, $post_id, $args ) { | |
static $data = null; | |
if ( $data === null ) $data = array(); | |
if ( $cb == "@get" ) { | |
// get data | |
return $data; | |
}else{ | |
// add data | |
$data[] = array( | |
'callback' => $cb, | |
'post_id' => $post_id, | |
'args' => $args | |
); | |
} | |
} | |
// Trigger callbacks from above during acf/save_post priority 1000 | |
// If hooks are not called, they will be called at the end of the PHP thread | |
function __after_saved_post( $post_id = null ) { | |
$data = add_action_after_save_post( '@get', 0, 0 ); | |
if ( !$data ) return; | |
// Loop through registered hooks | |
foreach( $data as $i => $d ) { | |
$cb = $d['callback']; | |
// Ensure the callback is valid | |
if ( !is_callable($cb) ) { | |
wp_die( new WP_Error('invalid-callable', 'Invalid callable function passed to queue_after_save_post_callback()', array( 'data' => $d, 'post_id' => $post_id, 'all data' => $data ))); | |
exit; | |
} | |
// Check that the post id matches (including prefix like user_1234) | |
if ( $post_id == $d['post_id'] ) { | |
call_user_func_array( $cb, $d['args'] ); | |
unset( $data[$i] ); | |
} | |
} | |
} | |
add_action( 'acf/save_post', '__after_saved_post', 1000 ); | |
register_shutdown_function( '__after_saved_post', '_shutdown' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a very niche solution. If you aren't sure what it does you probably want something else. The main reason you would need this is if you want to change some fields when a certain field is updated, but your changes get overwritten as ACF hasn't finished updating all other fields yet. You can use acf/save_post to get around this in most cases, but then you lose the ability to hook into direct field changes.
Usage:
And here is an example where that would be triggered (this method is thanks to register_shutdown_function because there is no acf/save_post):
It will also be triggered when updating that field from the WP dashboard