Hook into edit_post
捕捉更改。看看wp_transition_post_status()
在插入和更新时调用:
function wp_transition_post_status($new_status, $old_status, $post) {
do_action(\'transition_post_status\', $new_status, $old_status, $post);
do_action("{$old_status}_to_{$new_status}", $post);
do_action("{$new_status}_{$post->post_type}", $post->ID, $post);
}
在上
publish 你陷入了
draft_to_publish
, pending_to_publish
和auto-draft_to_publish
. 对于edits 钩入publish_to_publish
.示例
在发布或编辑后通知所有作者的迷你插件。<?php
/**
* Plugin Name: (#56779) Notify authors
*/
add_action( \'transition_post_status\', \'wpse_56779_notify_authors\', 10, 3 );
function wpse_56779_notify_authors( $new_status, $old_status, $post )
{
if ( \'publish\' !== $new_status )
return;
$subject = \'publish\' === $old_status
? __( \'Edited: %s\', \'your_textdomain\' )
: __( \'New post: %s\', \'your_textdomain\' );
$authors = new WP_User_Query( array( \'role\' => \'Author\' ) );
foreach ( $authors as $author )
{
wp_mail(
$author->user_email,
sprintf( $subject, $post->post_title ),
$post->post_content
// Headers
// Attachments
);
// Slow down
sleep( 5 );
}
}