$post_id
是一个整数(只是post id),而不是post对象(包含id、status、title等的整个post)
因此,全球化$post
对象并从此处检查状态,例如:
function er_send_email_on_post_draft_save( $post_id ) {
global $post;
//verify post is not a revision
if ( $post->post_status == \'draft\' ) {
$post_title = get_the_title( $post_id );
$post_url = get_permalink( $post_id );
$subject = \'A post has been updated\';
$message = "A post has been updated on your website:\\n\\n";
$message .= "" .$post_title. "\\n\\n";
//send email to admin
wp_mail( \'[email protected]\', $subject, $message );
}
}
如果你想的话,你可以用钩子
only when post is saved as draft add_action(\'draft_post\', \'send_my_mail_on_draft\' );
function send_my_mail_on_draft( $post_id,$post){
$post_title = get_the_title( $post_id );
$post_url = get_permalink( $post_id );
$subject = \'A post has been updated\';
$message = "A post has been updated on your website:\\n\\n";
$message .= "" .$post_title. "\\n\\n";
//send email to admin
wp_mail( \'[email protected]\', $subject, $message );
}