save_post_{$post->post_type}
云策文档标注
概述
save_post_{$post->post_type} 是一个动态 WordPress Hook,在文章保存时触发,允许开发者针对特定文章类型执行自定义操作。它提供更精确的控制,但需注意其触发时机和与其他 Hook 的交互。
关键要点
- Hook 名称动态部分 $post->post_type 对应文章类型 slug,例如 save_post_post 或 save_post_page。
- 参数包括 $post_id(文章 ID)、$post(文章对象)和 $update(是否为更新操作)。
- Hook 在点击“添加新”选项创建草稿、调用 wp_update_post 函数、以及文章移至垃圾箱或从垃圾箱恢复时也会触发。
- save_post_{$post->post_type} 在通用 save_post Hook 之前触发,覆盖第三方插件代码时需使用 save_post 并检查文章类型。
- 使用时应检查 $post->post_status 是否为“auto-draft”以避免在草稿创建时执行操作,并利用 $update 参数区分新建和更新。
代码示例
// 示例:通知订阅者新书添加
add_action( 'save_post_book', 'wpdocs_notify_subscribers', 10, 3 );
function wpdocs_notify_subscribers( $post_id, $post, $update ) {
if ( $update ) {
return;
}
$subscribers = array( 'john@doe.com', 'jane@doe.com', 'someone@else.com' );
$subject = 'A new book has beed added!';
$message = sprintf( 'We've added a new book, %s. Click here to see the book', get_the_title( $post ), get_permalink( $post ) );
wp_mail( $subscribers, $subject, $message );
}注意事项
- Hook 触发频繁,包括自动保存和草稿创建,建议添加条件检查以避免不必要的操作。
- 由于触发顺序,覆盖第三方插件功能时需在 save_post Hook 中实现并验证文章类型。
原文内容
Fires once a post has been saved.
Description
The dynamic portion of the hook name, $post->post_type, refers to the post type slug.
Possible hook names include:
save_post_postsave_post_page
Parameters
$post_idint-
Post ID.
$postWP_Post-
Post object.
$updatebool-
Whether this is an existing post being updated.
Source
do_action( "save_post_{$post->post_type}", $post_id, $post, $update );
Changelog
| Version | Description |
|---|---|
| 3.7.0 | Introduced. |
Skip to note 4 content
Andrew Dixon
The hook also fires when clicking on the “Add New” option for the custom post type, as it creates the draft in the DB, and when calling the
wp_update_postfunction as well.if ( $post->post_status == "auto-draft" ) { return; }It’s also good to note that this hook is also called when trashing a post or restoring it from trash.
Skip to note 5 content
squarecandy
It’s kind of sad that this more explicit action fires before the less explicit ‘save_post’ action.
This means if a 3rd party plugin does something using ‘save_post’ you need to use ‘save_post’ to override it too.
// someone else's code... add_action( 'save_post', 'wpdocs_do_something_custom', 15 ); // try to override with CPT action - DOES NOT WORK add_action( 'save_post_third_party_cpt', 'wpdocs_override_something_custom', 20, 2 ); // you must do this to override add_action( 'save_post', 'wpdocs_override_something_custom', 20, 2 ); function override_something_custom( $post_id, $post ) { // bail out if this is an autosave if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { return; } // bail out if this is not an event item if ( 'third_party_cpt' !== $post->post_type ) { return; } // ... do override stuff }Skip to note 6 content
Nazmul Ahsan
Notify your newsletter subscribers when a new book (custom post) is added-
add_action( 'save_post_book', 'wpdocs_notify_subscribers', 10, 3 ); function wpdocs_notify_subscribers( $post_id, $post, $update ) { // If an old book is being updated, exit if ( $update ) { return; } $subscribers = array( 'john@doe.com', 'jane@doe.com', 'someone@else.com' ); // list of your subscribers $subject = 'A new book has beed added!'; $message = sprintf( 'We've added a new book, %s. Click <a href="%s">here</a> to see the book', get_the_title( $post ), get_permalink( $post ) ); wp_mail( $subscribers, $subject, $message ); }