actions - How to use the post_updated hook before and after arguments

I want to check if the title of the post has changed and do some stuff if it did.I thought of using the post_updated hoo

I want to check if the title of the post has changed and do some stuff if it did.

I thought of using the post_updated hook, the codex says "Use this hook whenever you need to compare values before and after the post update.", but i can't figure out how to use the before and after arguments.

function check_values($post_ID, $post_after, $post_before){
        echo 'Post ID:';
        var_dump($post_ID);

        echo '<br>Post Object AFTER update:';
            echo '<pre>';
            print_r($post_after);
            echo '</pre>';

        echo '<br>Post Object BEFORE update:';
            echo '<pre>';
            print_r($post_before);
            echo '</pre>';
    }

    add_action( 'post_updated', 'check_values', 10, 3 );

and i'm echoing it echo check_values($post->ID, get_post(),get_post()); inside a metabox.

Currently I'm getting the same values.

If i echo check_values(); I get missing argument error and on echo check_values($post_ID, $post_after, $post_before) i get NULL.

How can i get the values before and after the update?

Update

function check_values($post_ID, $post_after, $post_before){
    if ($post_before->post_title != $post_after->post_title) { 
        // do stuff
    }   
}
add_action( 'post_updated', 'check_values', 10, 3 );

I want to check if the title of the post has changed and do some stuff if it did.

I thought of using the post_updated hook, the codex says "Use this hook whenever you need to compare values before and after the post update.", but i can't figure out how to use the before and after arguments.

function check_values($post_ID, $post_after, $post_before){
        echo 'Post ID:';
        var_dump($post_ID);

        echo '<br>Post Object AFTER update:';
            echo '<pre>';
            print_r($post_after);
            echo '</pre>';

        echo '<br>Post Object BEFORE update:';
            echo '<pre>';
            print_r($post_before);
            echo '</pre>';
    }

    add_action( 'post_updated', 'check_values', 10, 3 );

and i'm echoing it echo check_values($post->ID, get_post(),get_post()); inside a metabox.

Currently I'm getting the same values.

If i echo check_values(); I get missing argument error and on echo check_values($post_ID, $post_after, $post_before) i get NULL.

How can i get the values before and after the update?

Update

function check_values($post_ID, $post_after, $post_before){
    if ($post_before->post_title != $post_after->post_title) { 
        // do stuff
    }   
}
add_action( 'post_updated', 'check_values', 10, 3 );
Share Improve this question edited Jan 6, 2016 at 9:47 CK13 asked Jan 6, 2016 at 8:25 CK13CK13 451 gold badge4 silver badges10 bronze badges 2
  • compare $post_before and $post_after like if ($post_before->post_title != $post_after->post_title) { //title changed! } – Reigel Gallarde Commented Jan 6, 2016 at 8:30
  • 1 wait... you're doing it wrong... you can't just echo check_values($post->ID, get_post(),get_post()); inside a metabox... this function is an action when a post is saved or updated.. you need something else if you want to check the title of the post – Reigel Gallarde Commented Jan 6, 2016 at 8:33
Add a comment  | 

2 Answers 2

Reset to default 6

First off you are using the function wrong. The function is tied to an action within wordpress whenever you use the add_action function. In this case you have added it to the post_updated action.

Basically your check_values function gets added to a queue which will then get called by the action when its time to do the action.

You can see where the action gets called here: https://core.trac.wordpress/browser/tags/4.4/src/wp-includes/post.php#L3359

See line 3359 of this file. There is a function called do_action and its going to call all the functions tied to the post_updated action. This would include your check_values function. This is where your function is getting called and as you can see it passes in the necessary parameters for your function to interact with.

Try updating or adding a post. You should see some output there because this is when wordpress is calling your function.

Then in order to check to see if there was a change compare the two titles from the two before and after post objects

<?php 

function check_values($post_ID, $post_after, $post_before){
    if( $post_after->post_title !== $post_before->post_title ) {
        // do something
    }
}

add_action( 'post_updated', 'check_values', 10, 3 );
?>

When I used this action "post_updated" and update the post from latest updated gutenburg editor that time I get same post name in before post and after post details array.

It means suppose post name is "abc-test" and I changed this to a new post name as "abc-test-2" then I get "abc-test-2" name in both post after and before details.

Why its showing same data?

Thanks in advance.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745636077a4637399.html

相关推荐

  • actions - How to use the post_updated hook before and after arguments

    I want to check if the title of the post has changed and do some stuff if it did.I thought of using the post_updated hoo

    2小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信