在伺服器開啟 Mail 函式情況下,新增以下程式碼至 Functions.php:
- function newPostNotify($post_ID) {
- if( wp_is_post_revision($post_ID) ) return;
- $get_post_info = get_post($post_ID);
- if ( $get_post_info->post_status == 『publish』 && $_POST[『original_post_status』] != 『publish』 ) {
- // 讀資料庫,獲取所有使用者的 email
- 「SELECT DISTINCT user_email FROM $wpdb->users」);
- // 依次給每個 Email 發郵件
- foreach ( $wp_user_email as $email ) {
- // 郵件標題:xx 部落格有新文章
- $subject = 『xx 部落格有新文章';
- // 郵件內容:新文章網址:+ URL
- // 發郵件
- wp_mail($email->user_email, $subject, $message);
- }
- // 鉤子,一旦 WordPress 有新文章釋出或文章被修改即刻執行 newPostNotify 函式
- add_action(『publish_post』, 『newPostNotify』);