Отправлять электронное письмо только по черновику
Я пытаюсь отправить электронное письмо только после сохранения сообщения в виде черновика, похоже, это не работает с текущим кодом:
add_action( 'save_post', 'er_send_email_on_post_draft_save' ); function er_send_email_on_post_draft_save( $post_id ) { //verify post is not a revision if ( $post_id->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 ); } }
Это сработает, если я изменю оператор if на:
!wp_is_post_revision( $post_id )
Но это не то, что я хочу, я хочу отправлять уведомления только в том случае, если они сохранены только как черновик.
2 answers
$post_id
является целым числом (только идентификатор записи), а не объектом записи (вся запись с идентификатором, статусом, заголовком...)
Поэтому глобализируйте объект $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 );
}
}
И если вы хотите, вы можете использовать крючок только тогда, когда сообщение сохранено как черновик
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 );
}
Попробуйте с этим:
function dddn_process($id) {
// emails anyone on or above this level
$email_user_level = 7;
global $wpdb;
$tp = $wpdb->prefix;
$result = $wpdb->get_row("
SELECT post_status, post_title, user_login, user_nicename, display_name
FROM {$tp}posts, {$tp}users
WHERE {$tp}posts.post_author = {$tp}users.ID
AND {$tp}posts.ID = '$id'
");
if (($result->post_status == "draft") || ($result->post_status == "pending")) {
$message = "";
$message .= "Draft updated on '" . get_bloginfo('name') . "'\n\n";
$message .= "Title: " . $result->post_title . "\n\n";
// *** Choose one of the following options to show the author's name
$message .= "Author: " . $result->display_name . "\n\n";
// $message .= "Author: " . $result->user_nicename . "\n\n";
// $message .= "Author: " . $result->user_login . "\n\n";
$message .= "Link: " . get_permalink($id);
$subject = "Draft updated on '" . get_bloginfo('name') . "'";
$editors = $wpdb->get_results("SELECT user_id FROM {$tp}usermeta WHERE {$tp}usermeta.meta_value >= " . $email_user_level);
$recipient = "";
foreach ($editors as $editor) {
$user_info = get_userdata($editor->user_id);
$recipient .= $user_info->user_email . ',';
}
mail($recipient, $subject, $message);
}
}
add_action('save_post', 'dddn_process');
?>
Просто создайте notification.php файл с описанием плагинов wp загрузите его в папку плагинов и активируйте как обычный плагин