我有一种情况,我想在每篇WordPress博客文章发布后创建一份cron作业。 Cron作业将在发布帖子7天后运行,并检查是否所有用户都已查看了帖子并通过用户列表将未查看帖子的列表发送给了电子邮件。我怎样才能做到这一点。它是一个WordPress网站,每个博客帖子发布用户收到通知后,大约有300多个用户。因此,想查看谁查看了谁,谁没有查看过该帖子。
答案 0 :(得分:0)
如果仅在首次发布帖子时才想开始cron作业,则需要使用draft_to_publish
钩子。
答案 1 :(得分:0)
有一些插件可以管理统计信息并跟踪用户活动。我不确定哪一种适合您的需求,但是我会尝试给出一种简单的方法来自己完成。
跟踪 我们可以为那些(帖子/帖子类型)设置一个自定义字段,在其中存储阅读该帖子的用户列表。我们称之为 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 );
}
?>
注意:我没有测试上面的代码!我这样做是为了向您展示如何做。但是这里没有太多工作要做。祝你好运
答案 2 :(得分:0)
我尝试了您的代码,它可以正常工作,但是我无法使其按我的意愿工作,您能调查一下吗。我不知道我在做什么错。
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 );
}