首先需要你的主机支持邮件发送,加入到当前主题的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');

 

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。