繁体   English   中英

WordPress在add_post_meta上发送电子邮件

[英]WordPress send email on add_post_meta

当自定义元字段通过add_post_metaupdate_post_meta添加到他们的WordPress帖子中时,我想向作者发送通知电子邮件。

到目前为止,我下面的代码可以成功运行,但是仅当我使用Save Post时才执行

function order_update_send_email( $post_id ) {

$email_sent = get_post_meta($post_id, 'email_sent', true);
$report = get_post_meta($post_id, 'report', true);

if ( $email_sent == 'Sent' ) {
    return;
}
if ( $report ) {
$post_title = get_the_title( $post_id );
$post_url = get_permalink( $post_id );
$subject = 'Your report for: ' . $post_title;

$message = "Your order is completed\n\n";
$message .= "Report for: " . $post_title . "\n\n Link:" . $post_url;
$message .= "\n\n" . $report;

wp_mail( 'test@email.me', $subject, $message );
update_post_meta($post_id, 'email_sent', 'Sent');
}
}
add_action( 'save_post', 'order_update_send_email' );

在上面的代码中,动作钩子save_post正常工作,但是我无法使add_post_metaupdate_post_meta正常工作。

任何人都请指教,谢谢。

您可以为此使用以下示例代码。

add_action( 'added_post_meta', 'wp_afterpostmeta', 10, 4 );
add_action( 'updated_post_meta', 'wp_afterpostmeta', 10, 4 );
function wp_afterpostmeta( $meta_id, $post_id, $meta_key, $meta_value )
{
    if ( 'wp_meta_key' == $meta_key ) {
        wp_do_something( $post_id, $meta_value );
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM