posts - Can't get post_updated hook to work

admin2025-01-07  3

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?

Share Improve this question edited Aug 9, 2016 at 7:27 user2806026 asked Aug 9, 2016 at 7:21 user2806026user2806026 3352 gold badges5 silver badges12 bronze badges 3
  • What is not working? – bravokeyl Commented Aug 9, 2016 at 7:25
  • I guess it is supposed to show the echoes and var_dumps on screen as I update a post? It doesnt do that :-) – user2806026 Commented Aug 9, 2016 at 7:26
  • Try writing logs. Sometimes due to the theme or any other reason data won't echo using var_dump. – pixelngrain Commented Apr 19, 2022 at 7:43
Add a comment  | 

1 Answer 1

Reset to default 0

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));
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736255586a294.html

最新回复(0)