在服务器开启 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』);