【发布时间】:2020-04-28 02:37:45
【问题描述】:
我有一种情况,我想在每个 WordPress 博客文章发布后创建一个 cron 作业。 Cron 作业将在发布帖子 7 天后运行,并检查是否所有用户都查看了帖子,并通过电子邮件发送未查看帖子列表的用户列表。我怎样才能做到这一点。它是一个 WordPress 网站,大约有 300 多个用户,在每篇博文发布后用户都会收到通知。所以想看看谁看过,谁没看过。
【问题讨论】:
我有一种情况,我想在每个 WordPress 博客文章发布后创建一个 cron 作业。 Cron 作业将在发布帖子 7 天后运行,并检查是否所有用户都查看了帖子,并通过电子邮件发送未查看帖子列表的用户列表。我怎样才能做到这一点。它是一个 WordPress 网站,大约有 300 多个用户,在每篇博文发布后用户都会收到通知。所以想看看谁看过,谁没看过。
【问题讨论】:
如果您只想在第一次发布帖子时启动 cron 作业,您需要使用 draft_to_publish 挂钩。
【讨论】:
有一些插件能够管理统计信息和跟踪用户活动。我不确定哪一个适合您的需求,但我会尝试提供一种简单的方法来自己完成。
跟踪 我们可以为那些(帖子/帖子类型)设置一个自定义字段,我们将在其中存储阅读该帖子的用户列表。我们称它为 readerslist
在 single.php 文件中,我们将这些行添加到更新此字段。
$list = explode(',',get_post_meta($post->ID, 'readerslist', true));
$user = get_current_user_id();
if (!in_array($user,$list)){
array_push($list, $user);
update_post_meta( $post->ID, 'readerslist', implode(",", $list) );
}
现在我们可以检索谁查看了这篇文章?我们以后可以找到那些还没有读过的人。
然后在functions.php中,我们可以设置一个钩子,在帖子发布时执行,设置一个预定的cron作业来发送缺席的读者。
<?php
function wpdocs_run_on_publish_only( $new_status, $old_status, $post ) {
{
if ($new_status != 'publish') {
return;
}
//You better add limitations to choose specific post_types you are targeting
$postid=$post->ID;
add_cron($postid);
}
add_action( 'transition_post_status', 'wpdocs_run_on_publish_only', 10, 3 );
function add_cron($postid){
$nxtweek = strtotime("+1 week");
add_action('sendemail', 'stats_mail', 10, 1);
wp_schedule_single_event(time() + $nxtweek, 'sendemail',array( $postid);
}
function stats_mail($postid)
{
$readerslist = get_post_meta( $postid, 'readerslist', true );
$users = get_users( array( 'fields' => array( 'ID' ) ) );
$absents = array_diff($users, explode(",",$readerslist));
//Now that you got the list of absent users
$names='';
foreach ($absents as $x){
$names.=$x.' : '.get_user_meta( $x,'first_name' , true ).' '.get_user_meta( $x,'last_name' , true ).'<br>';
}
$title=get_the_title($postid);
$to = 'you@example.com';
$subject = 'Users who didn\'t read '.$title;
$headers = array('Content-Type: text/html; charset=UTF-8');
wp_mail( $to, $subject, $names, $headers );
}
?>
注意:我没有测试上面的代码!我给你做这个是为了告诉你怎么做。但是这里没有太多工作要做。祝你好运
【讨论】:
我试过你的代码,它工作正常,但我无法让它按我想要的方式工作,你能调查一下吗?我不知道我做错了什么。
function wpdocs_run_on_publish_only( $new_status, $old_status, $post ) {
if ($new_status != 'publish') {
return;
}
$postid=$post->ID;
add_cron($postid);
}
add_action( 'transition_post_status', 'wpdocs_run_on_publish_only', 10, 3 );
function add_cron($postid){
$nxtweek = strtotime("+1 week");
add_action('sendemail', 'stats_mail', 10, 1);
if ( ! wp_next_scheduled( 'sendemail') ) {
wp_schedule_event(time() + $nxtweek, $nxtweek, 'sendemail', array($postid));
// i want to run cron every 7 days from the date of publishing post, until all user reads it, i tried this code but not working.
// wp_mail( 'email@company.com', $postid, 'test', null );
// For testing purpose if i send mail from here, it goes.
}
}
function stats_mail($postid) {
wp_mail( 'email@company.com', $postid, 'test', null );
// this doesnt work.
$readerslist = get_post_meta( $postid, 'readerslist', true );
$users = get_users( array( 'fields' => array( 'ID' ) ) );
$absents = array_diff($users, explode(",",$readerslist));
//Now that you got the list of absent users
$names='';
foreach ($absents as $x){
$names.=$x.' : '.get_user_meta( $x,'first_name' , true ).' '.get_user_meta( $x,'last_name' , true ).'<br>';
}
$title=get_the_title($postid);
$to = 'email@company.com';
$subject = 'Users who didn\'t read '.$title;
$headers = array('Content-Type: text/html; charset=UTF-8');
//wp_mail( $to, $subject, $names, $headers );
}
【讨论】: