我想在自定义帖子字段(JOB_STATUS==2)上发送电子邮件,但它不起作用

时间:2018-04-03 作者:Terry
add_action(\'updated_post_meta\', \'send_email_on_job_complete\',10,4);
function send_email_on_job_complete($meta_id, $post_id, $meta_key, $meta_value)
 {
    $post_type = get_post_type($post_id);
    if("jobs" !== $post_type) return;
    $job_status = types_get_field_meta_value("job-status-1",$post_id); 

    if($job_status == 1)
        {
            return;
        }    
    else
        {
            $user_id = get_post_meta($post_id,"user_id",true);
            $user_info = get_userdata($user_id);
            $user_full_name = $user_info->display_name;
            $user_email = $user_info->user_email;

            //sending email from user to admin
            $to_admin_email = get_option(\'admin_email\');
            $subject_to_admin = \'JOB COMPLETED\';
            $body_to_admin = \'Job is completed which is posted by \'.$user_full_name.\'.\';
            $headers_to_admin = array(\'Content-Type: text/html; charset=UTF-8\');

            //sending email from admin to user
            $subject_to_user = \'JOB COMPLETED\';
            $body_to_user = \'Your posted job is complete plz give review about it.\';
            $headers_to_user = array(\'Content-Type: text/html; charset=UTF-8\');
            wp_mail( $to_admin_email, $subject_to_admin, $body_to_admin, $headers_to_admin );
            wp_mail( $user_email, $subject_to_user, $body_to_user, $headers_to_user );
        } 
}
1 个回复
最合适的回答,由SO网友:Aniruddha Gawade 整理而成

这是关于对问题的评论。它需要在特定的元上运行,而不是在每次元值更新时运行mail函数。

您还需要相应地更改代码。

首先检查您的$meta_key 然后从$meta_value. 在这些字段中写入条件并相应地触发邮件。

add_action(\'updated_post_meta\', \'send_email_on_job_complete\',10,4);
function send_email_on_job_complete($meta_id, $post_id, $meta_key, $meta_value)
 {
    $post_type = get_post_type($post_id);
    if("jobs" !== $post_type) return;

    if("job-status-1" !== $meta_key) return; //Check if currently updated meta key is meta key you want to check.Else return.

    $job_status = types_get_field_meta_value("job-status-1",$post_id); 

    if($job_status == 1)
        {
            return;
        }    
    else
        {
            $user_id = get_post_meta($post_id,"user_id",true);
            $user_info = get_userdata($user_id);
            $user_full_name = $user_info->display_name;
            $user_email = $user_info->user_email;

            //sending email from user to admin
            $to_admin_email = get_option(\'admin_email\');
            $subject_to_admin = \'JOB COMPLETED\';
            $body_to_admin = \'Job is completed which is posted by \'.$user_full_name.\'.\';
            $headers_to_admin = array(\'Content-Type: text/html; charset=UTF-8\');

            //sending email from admin to user
            $subject_to_user = \'JOB COMPLETED\';
            $body_to_user = \'Your posted job is complete plz give review about it.\';
            $headers_to_user = array(\'Content-Type: text/html; charset=UTF-8\');
            wp_mail( $to_admin_email, $subject_to_admin, $body_to_admin, $headers_to_admin );
            wp_mail( $user_email, $subject_to_user, $body_to_user, $headers_to_user );
        } 
}
像这样的。

结束

相关推荐

在联系人页面上使用带有$emailTo的GET_OPTION

我已经创建了一个联系人页面,我只想将变量$emailto设置为dynamique。我的意思是,我在后端有一个选项页,我想让管理员更改联系人电子邮件。enter code here if(!isset($hasError)) { $emailTo = \'contact@sophrologie.com\'; $subject = \'Formulaire du contact \'.$name; $sendCopy =