setcookie
setcookie

Reputation: 629

Wordpress post save/edit post hook

I'm looking for a admin hook for posts that gets fired after the post has been saved. The problem: the save_post does not contain the already changed data to the post object. The new changes can only be found in the $_POST array. But I need a way to update the permalink to a external API once the post_name changes. But it will not work since the $post object is still the old one before the save action.

Upvotes: 3

Views: 8859

Answers (3)

Gleb Kemarsky
Gleb Kemarsky

Reputation: 10398

Since WordPress 3.0.0 the post_updated hook is available. It helps to know what has changed in the post after the update. You can use the example in the WP Codex as a sample.

add_action( 'post_updated', 'check_updated_post_name', 10, 3 );
function check_updated_post_name( $post_ID, $post_after, $post_before ) {
    if ( $post_after->post_name != $post_before->post_name ) {
        // do what you need
    }
}

And if a post has just been inserted you can use save_post or save_post_{$post->post_type} hooks. Check the value of the third argument to make sure the post is new.

add_action( 'save_post', 'check_new_post_name', 10, 3 );
function check_new_post_name( $post_ID, $post, $update ) {
    if ( ! $update ) {
        // do what you need
    }
}

Upvotes: 1

locomo
locomo

Reputation: 211

You should be able to hook in after the post has been updated using the priority argument (set to 20 in this example):

add_action( 'save_post', 'your_function', 20, 1 );
function your_function( $post_id ) {
    // this should be the updated post object 
    $post = get_post( $post_id );
}

Upvotes: 4

Emir Akaydın
Emir Akaydın

Reputation: 5823

I think the most suitable method is to query the old values from the database and compare the values with $_POST array values.

Here is the link which should help you to read values from database.

http://codex.wordpress.org/wpdb#query_-_Run_Any_Query_on_the_Database

P.S: You should of course make this comparison "before" saving the new values to the database.

Upvotes: 1

Related Questions