【问题标题】:WordPress send email on add_post_metaWordPress 在 add_post_meta 上发送电子邮件
【发布时间】:2016-12-26 23:30:30
【问题描述】:

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 工作。

请大家指教,谢谢。

【问题讨论】:

  • 对不起他们是没有办法的。因为在 wordpress 中 update_post_meta 和 add_post_meta 之后/之前没有任何动作。您必须编辑 wordpress 核心才能创建操作。

标签: php wordpress


【解决方案1】:

您可以使用下面的示例代码。

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 );
    }
}

【讨论】:

    猜你喜欢
    • 2011-08-20
    • 2014-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-30
    • 1970-01-01
    • 2019-08-04
    • 2012-11-24
    相关资源
    最近更新 更多