I am trying to add some code to be run when a post is updated. To get started I have added the following code to my functions.php:
function check_values($post_ID, $post_after, $post_before){
echo 'Post ID:';
var_dump($post_ID);
echo 'Post Object AFTER update:';
var_dump($post_after);
echo 'Post Object BEFORE update:';
var_dump($post_before);
}
add_action( 'post_updated', 'check_values', 10, 3 );
However, it doesn't seem to work. Nothing has changed when updating a post - nothing is echoed or dumped on screen.
Did I misunderstand something?
I am trying to add some code to be run when a post is updated. To get started I have added the following code to my functions.php:
function check_values($post_ID, $post_after, $post_before){
echo 'Post ID:';
var_dump($post_ID);
echo 'Post Object AFTER update:';
var_dump($post_after);
echo 'Post Object BEFORE update:';
var_dump($post_before);
}
add_action( 'post_updated', 'check_values', 10, 3 );
However, it doesn't seem to work. Nothing has changed when updating a post - nothing is echoed or dumped on screen.
Did I misunderstand something?
If it doesn't work you can try to use a wp_insert_post
action:
function insert_post_hook($post_id, $post) {
if ($post->post_date != $post->post_modified) {
// This post is being updated!
}
else {
// This post is being created!
}
}
add_action( 'wp_insert_post', 'insert_post_hook', 10, 2 );
If you want to see the latest version you could enable revisions:
define('WP_POST_REVISIONS', 3 );
This will save a maximum of 3 revisions.
After enabling you can check the following:
$latest_revision = array_shift(wp_get_post_revisions($post->ID));