save_post_(post_type)
Fires whenever a post of the specified type (post, page) is created or updated, including when it is published through import, XML-RPC, or email.
This is a copy of the save_post hook, except that (post_type) can be replaced with the post type for which the hook should fire.
Usage
add_action( 'save_post_(post_type)', 'wp_kama_save_post_type_action', 10, 3 );
/**
* Function for `save_post_(post_type)` action-hook.
*
* @param int $post_id Post ID.
* @param WP_Post $post Post object.
* @param bool $update Whether this is an existing post being updated.
*
* @return void
*/
function wp_kama_save_post_type_action( $post_id, $post, $update ){
// action...
}
- $post_ID(int)
- ID of the post being updated.
- $post(WP_Post)
- The post object being updated. This is the same object normally available in the global
$postvariable. - $update(bool)
true— an existing post is being updated.
false— a new post is being added.
Examples
#1 Send an email when a book post is updated
This example shows how to send an email to the site administrator whenever a post is updated.
add_action( 'save_post_book', 'my_project_updated_send_email' );
function my_project_updated_send_email( $post_id ) {
// Do not send an email if this is a revision.
if ( wp_is_post_revision( $post_id ) ){
return;
}
// Do not send an email if the post status is not "Published".
if ( get_post($post_id)->post_status != 'publish' ){
return;
}
$post_title = get_the_title( $post_id );
$post_url = get_permalink( $post_id );
$subject = 'The post has been updated';
$message = "The following post on your site has been updated:\n\n";
$message .= $post_title . ": " . $post_url;
// Send the email.
wp_mail( get_option('admin_email'), $subject, $message );
}
#2 More examples
See the save_post event description for more examples.
Changelog
| Since 3.7.0 | Introduced. |
Where the hook is called
save_post_(post_type)
save_post_(post_type)
wp-includes/post.php 5275
do_action( "save_post_{$post->post_type}", $post_id, $post, $update );
wp-includes/class-wp-customize-manager.php 3118
do_action( "save_post_{$post->post_type}", $post->ID, $post, true );
wp-includes/post.php 5461
do_action( "save_post_{$post->post_type}", $post->ID, $post, true );
Where the hook is used in WordPress
wp-includes/default-filters.php 778
add_action( 'save_post_wp_template_part', 'wp_set_unique_slug_on_create_template_part' );