在帖子过期后发送提醒电子邮件

时间:2014-10-02 18:03:27

标签: php email wordpress-plugin wordpress

我正在使用我在网上找到的一些代码,以便在自定义元框中设置过期日期时隐藏帖子。我能够做到这一点很好,但现在我想创建一个扫描我所有帖子的功能,如果他们中的任何人过去有一个日期我想触发一个提醒电子邮件,发送到一个电子邮件地址我指定使用自定义字段。

以下是我的代码。任何帮助将不胜感激:

//Sends Expirary Email
function plugin_email_reminder() {
    global $post;
    $args = array( 
        'post_type' => 'coupon',
        'p'         => $post_id
    );
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) : setup_postdata($post); 

    //Sets Expiration
    $expirationtime = get_post_custom_values('coupon_expiration');
        if (is_array($expirationtime)) {
        $expirestring = implode($expirationtime);
    }
    $secondsbetween = strtotime($expirestring)-time();
        if ( $secondsbetween <= 0 ) {

    // Start the code to send the emails
    $to = get_option('plugin_email_address');
    $subject = get_the_title(). ' Post has expired';
    $headers = 'From: '.get_bloginfo('name').' <no-reply@nomail.com>';
    $message = get_the_title(). ' Post has expired. If you would like it to appear please change the expiration date.';
    wp_mail($to, $subject, strip_tags($message), $headers);
    // End the code to send the emails          

    }           

    endforeach; 
}

当我将下面的代码放在我的single.php循环中时,我可以触发电子邮件

    $expirationtime = get_post_custom_values('coupon_expiration');
        if (is_array($expirationtime)) {
        $expirestring = implode($expirationtime);
    }
    $secondsbetween = strtotime($expirestring)-time();
        if ( $secondsbetween <= 0 ) {

    // Start the code to send the emails
    $to = get_option('plugin_email_address');
    $subject = get_the_title(). ' Post has expired';
    $headers = 'From: '.get_bloginfo('name').' <no-reply@nomail.com>';
    $message = get_the_title(). ' Post has expired. If you would like it to appear please change the expiration date.';
    wp_mail($to, $subject, strip_tags($message), $headers);
    // End the code to send the emails          

    }

但是,只有当我访问我网站上的single.php页面时,电子邮件才会消失。我希望这封提醒电子邮件是动态的。

提前致谢!

0 个答案:

没有答案