duangua6912 2019-05-06 02:14
浏览 301

使用save_post挂钩检查它是否是新的自定义帖子或更新

I'm trying to send a mail using wp_mail function and hook it on save_post. How can I check it if it is a new post or it is just updating post? I tried the update parameter of save_post but always gave me the boolean true answer. I only need to send it if its a new post after published. Is it possible?

Here is my code:

add_action( 'save_post', 'dt_email_client_new', 13, 3 );
function dt_email_client_new( $post_id, $post, $update ) {

$post_type = get_post_type($post_id);

if($post_type != 'dt_appointments'){
    return;
}

if ( wp_is_post_revision( $post_id ) ) {
    return;
}

// Stop WP from clearing custom fields on autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
    return;

// Prevent quick edit from clearing custom fields
if (defined('DOING_AJAX') && DOING_AJAX)
    return;

remove_action( 'save_post', 'dt_email_client_new', 13, 3 );//this will avoid duplicate emails

    //how can I check if its new post or update?
    $get_first_name = get_post_meta($post_id, 'first_name', true);
    $get_last_name = get_post_meta($post_id, 'last_name', true);
    $get_date_time = get_post_meta($post_id, 'appointment_date_time', true);
    $get_email = get_post_meta($post_id, 'email', true);
    $get_doctor_id = get_post_meta($post_id, 'doctor', true);
    $get_phone = get_post_meta($post_id, 'phone', true);

    $user_info = get_userdata( $get_doctor_id );
    $firstname = $user_info->first_name;
    $lastname = $user_info->last_name;

    $get_sched_date_format = date('F d, Y', strtotime($get_date_time));
    $get_sched_time_format = date('H:i A', strtotime($get_date_time));

    $to = $get_email;
    $headers = array('Content-Type: text/html; charset=UTF-8','From: Test <sample@gmail.com>');
    $subject = 'sample subj here...';
    $body = '<p><strong>Hi '.$get_first_name.',</strong></p>
    <p style="margin: 0px;">Your appointment request has been confirmed. Please proceed to ......</p>
    <p></p>';

    wp_mail( $to, $subject, $body, $headers );

}
  • 写回答

2条回答 默认 最新

  • douzhiba6873 2019-05-06 06:47
    关注

    Please try below code:

    add_action('publish_post', 'send_admin_email');
    function send_admin_email($post_id){
    
        $postdata = get_post($post_id);
        $time_differ = round(abs(strtotime($postdata->post_modified) - strtotime($postdata->post_date)) / 60,2);
    
        if ( $time_differ < 0.20 ) {
             wp_mail('test@gmail.com', 'Test', 'Test message');
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测