save_post

Home » Hooks » save_post

The "save_post" hook is a WordPress action hook that is triggered when a post or a page is saved or updated. This hook is mostly used to perform additional actions when a post is saved, like updating some meta data or sending an email notification.

When a post is saved, WordPress triggers this hook and passes the ID of the post that was saved to the callback function. Developers can then use this ID to retrieve the post data and perform any additional actions they need.

Here is an example of how to use the "save_post" hook to update some meta data when a post is saved:

function my_save_post_function( $post_id ) {
    // Check if this is a revision, if true, do nothing
    if ( wp_is_post_revision( $post_id ) )
        return;

    // Update some meta data
    update_post_meta( $post_id, 'my_meta_key', 'my_meta_value' );
}
add_action( 'save_post', 'my_save_post_function' );

In this code, we define the "my_save_post_function" function and attach it to the "save_post" hook using the "add_action" function. Inside the function, we first check if the post is a revision using the "wp_is_post_revision" function. If it is a revision, we do nothing. If it’s not a revision, we update some meta data using the "update_post_meta" function.

This is just one example of how the "save_post" hook can be used. Developers can use this hook to perform many other actions when a post is saved or updated.

Learn More on WordPress.org

Register an account to save your snippets or go Pro to get more features.