为帖子字段创建通知

时间:2017-02-05 作者:user112513

我有一个带有自定义字段的帖子类型报价,我想创建一个通知,向电子邮件地址集发送电子邮件提醒。当日期条件匹配时发送It提醒。我写了这个函数脚本。

function set_mail_html_content_type() {
    return \'text/html\';
}

function notification_fields(){
    // Get the \'Profiles\' post type
    $args = array(
    \'post_type\' => \'quote\',
    );
    $loop = new WP_Query($args);
    while($loop->have_posts()){
        $loop->the_post();
        $days = "30";
        $eventName = types_render_field( "event-name", array( \'raw\' => true));
        $email = types_render_field( "email-user", array( \'raw\' => true));
        $eventdate = types_render_field( "event-date", array( \'raw\' => true) );
        $newDate = date("d-m-Y", strtotime($eventdate));
        $date = strtotime (\'-30 days\', strtotime ($newDate));
        $date = date (\'d-m-Y\', $date);
        $today = date("d-m-Y");
        $balance = types_render_field( "remaining", array( \'raw\' => true) );
        if (!empty($balance) && $today === $date ) {
            $to = $email;
            $subject = "Payment reminder";
            $content = \'Please pay the final payment here\';
            /*$from = "no-reply@website.com";
            $headers = "From:" . $from;*/
            $status = wp_mail($to,$subject,$content,$headers);
            add_filter( \'wp_mail_content_type\', \'set_mail_html_content_type\' );
            return $status;
            remove_filter( \'wp_mail_content_type\', \'set_mail_html_content_type\' );
        }
    }
    wp_reset_query();
    //return;
}
add_action( \'customemail_fields\', \'notification_fields\',10, 2 );

1 个回复
最合适的回答,由SO网友:Ignacio Jose Canó Cabral 整理而成

基本上,您需要的是发送这些提醒的日常任务,因为您将使用WP Cron。我不会进入实际构建电子邮件的逻辑(使用您的代码)。以下是基本设置:

if ( ! wp_next_scheduled( \'quote_reminder_hook\' ) ) {
  wp_schedule_event( time(), \'daily\', \'quote_reminder_hook\' );
}

add_action( \'quote_reminder_hook\', \'send_quote_reminder\' );

function send_quote_reminder() {

    $args = array(
    \'post_type\' => \'quote\',
    );
    $loop = new WP_Query($args);
    while($loop->have_posts()){
        $loop->the_post();
        $days = "30";
        $eventName = types_render_field( "event-name", array( \'raw\' => true));
        $email = types_render_field( "email-user", array( \'raw\' => true));
        $eventdate = types_render_field( "event-date", array( \'raw\' => true) );
        $newDate = date("d-m-Y", strtotime($eventdate));
        $date = strtotime (\'-30 days\', strtotime ($newDate));
        $date = date (\'d-m-Y\', $date);
        $today = date("d-m-Y");
        $balance = types_render_field( "remaining", array( \'raw\' => true) );
        if (!empty($balance) && $today === $date ) {
            $to = $email;
            $subject = "Payment reminder";
            $content = \'Please pay the final payment here\';
            /*$from = "no-reply@website.com";
            $headers = "From:" . $from;*/
            wp_mail($to,$subject,$content,$headers);
        }
    }
}
如果你有很多引用,我建议你把它们分成几批,或者把事件设置为每小时一次,这样它就不会超时,也不会占用你的服务器资源。

我建议您将cron设置为从系统任务调度器运行(这里有一个指南https://developer.wordpress.org/plugins/cron/hooking-into-the-system-task-scheduler/)

Cron引用https://developer.wordpress.org/plugins/cron/

当做

相关推荐

Organize functions.php

组织职能的最佳方式是什么。php的性能?我有几个add\\u操作调用、几个add\\u主题支持、几个add\\u过滤器和4个附加函数。对于面包屑、样式表、注册菜单和小部件。我们是否需要遵守订单?例如,首先是所有add\\u过滤器函数,然后是add theme\\u support等。不确定添加代码是否相关,因为这是一个一般性问题。如果需要,我很乐意更新这篇文章。