在 Wordpress 中使用 wpmail() 作为条件语句 functions.php

Using wpmail() as a conditional statement in Wordpress functions.php

我的 Wordpress 站点上有 'Advanced Custom Fields' 运行,我想根据更改的 Select 选项触发通知。

例如,如果 'Content Status' 更改为 'Collect',则应发送电子邮件。我设法让它作为一个简码工作,所以当我把这个简码放在自定义 post 类型上时它就可以工作了。但是,每次 selection 更改它只发送一次邮件。

所以假设电子邮件是在我 select 'Collect' 作为选项时发送的。然后我将选项更改为 'Delivered',然后再次返回 'Collect',它不会再次发送收集通知。我错过了什么吗?

// Shortcode to send mail based on Content Status being 'Collect'
function my_vc_shortcode_mail( $atts ) {


if ( get_field( 'content_status' )  == 'collect' ): ?>


<?php
add_filter( 'wp_mail_content_type', 'wpdocs_set_html_mail_content_type' );
 
$to = 'john@johndoe.com';
$subject = 'Time to collect your products';
$body = 'The email body content';
 
wp_mail( $to, $subject, $body );
 
// Reset content-type to avoid conflicts -- https://core.trac.wordpress.org/ticket/23578
remove_filter( 'wp_mail_content_type', 'wpdocs_set_html_mail_content_type' );
 
function wpdocs_set_html_mail_content_type() {
    return 'text/html';
}
?>

<?php endif;

}

add_shortcode( 'my_mail', 'my_vc_shortcode_mail');

给你,当你保存 post 时,这将发送一封 HTML 格式的电子邮件。

function user_collect_send_email( $post_id ) {

    if ( get_field( 'content_status', $post_id ) === 'collect' ):
        $to = 'john@johndoe.com';
        $subject = 'Time to collect your products';
        $body = 'The email body content';
        wp_mail( $to, $subject, $body );
    endif;
}

add_action( 'save_post', 'user_collect_send_email' );

function emails_set_content_type(){
    return "text/html";
}
add_filter( 'wp_mail_content_type', 'emails_set_content_type' );

或者,您可以通过设置 headers.

来设置每个单独的电子邮件格式,而不是设置内容类型过滤器
$headers[] = "Content-type: text/html";
wp_mail( $to, $subject, $body, $headers );