QQ郵箱訂閱中有這樣一個功能:在讀者訂閱博客后將每天匯總該博客更新信息,并發送至QQ郵箱里。我們可以效仿此法,達到同等推廣效果。這樣一來,網站更新了什么內容即可直接調用信息,批量發送新文章內容至 Email 通知自己的用戶。
在服務器開啟 Mail 函數情況下,添加以下代碼至 Functions.php:
function newPostNotify($post_ID) {
if( wp_is_post_revision($post_ID) ) return;
global $wpdb;
$get_post_info = get_post($post_ID);
if ( $get_post_info->post_status == ‘publish’ && $_POST[‘original_post_status’] != ‘publish’ ) {
// 讀數據庫,獲取所有用戶的email
$wp_user_email = $wpdb->get_results(“SELECT DISTINCT user_email FROM $wpdb->users”);
// 依次給每個Email發郵件
foreach ( $wp_user_email as $email ) {
// 郵件標題:xx博客有新文章
$subject = ‘xx博客有新文章';
// 郵件內容:新文章網址:+ URL
$message = ‘新文章網址:’ . get_permalink($post_ID);
// 發郵件
wp_mail($email->user_email, $subject, $message);
}
}
}
// 鉤子,一旦WordPress有新文章發布或文章被修改即刻執行newPostNotify函數
add_action(‘publish_post’, ‘newPostNotify’);
新聞熱點
疑難解答