問題描述

每當釋出頁面或帖子時,有沒有辦法讓 WordPress 發郵件給我?

最佳解決方案

有一個 few plugins that handle email notifications,但它們似乎都像一個 WordPress 使用者的訂閱服務。

要在釋出資訊或頁面時通知您:

/**
 * Send an email notification to the administrator when a post is published.
 *
 * @param   string  $new_status
 * @param   string  $old_status
 * @param   object  $post
 */
function wpse_19040_notify_admin_on_publish( $new_status, $old_status, $post ) {
    if ( $new_status !== 'publish' || $old_status === 'publish' )
        return;
    if ( ! $post_type = get_post_type_object( $post->post_type ) )
        return;

    // Recipient, in this case the administrator email
    $emailto = get_option( 'admin_email' );

    // Email subject, "New {post_type_label}"
    $subject = 'New ' . $post_type->labels->singular_name;

    // Email body
    $message = 'View it: ' . get_permalink( $post->ID ) . "nEdit it: " . get_edit_post_link( $post->ID );

    wp_mail( $emailto, $subject, $message );
}

add_action( 'transition_post_status', 'wpse_19040_notify_admin_on_publish', 10, 3 );

您可以將其放在主題的 functions.php 中,也可以將其儲存為外掛 (可能更適合,因為它不是完全相同的’theme’) 。

次佳解決方案

沙 – 它回答了這個問題,提出了所有情況下發布的解決方案不起作用的知識。

24 小時後,我可以更新我所貢獻的知識。該位置的解決方案 (Notify admin when page is edited?) 適用於上述解決方案沒有的伺服器。引用執行緒與在兩個上下文中更好的解決方案我嘗試:

來自 wpcodex 的原始指令碼工作正常:

 add_action( 'save_post', 'my_project_updated_send_email' );
 function my_project_updated_send_email( $post_id ) {
    //verify post is not a revision
    if ( !wp_is_post_revision( $post_id ) ) {
         $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:nn";
         $message .= "<a href='". $post_url. "'>" .$post_title. "</a>nn";
         //send email to admin
         wp_mail( get_option( 'admin_email' ), $subject, $message );
   }
}

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。