簡體   English   中英

如何根據 wordpress-method get_post_meta() 的值向用戶發送電子郵件?

[英]How to send an Email to User depending on the value of the wordpress-method get_post_meta()?

我在我的 Wordpress 管理面板中實現了一個功能,它是一個復選框。 我喜歡向該特定用戶發送電子郵件,當我選中他特定帖子上的框或取消選中該框並點擊右上角的保存按鈕時。

當我選中該框時,用戶帖子上會出現一個警告字段。 當我取消選中它時,警告消失。 現在我使用get_post_meta()方法。

如果我手動選中 warning-box ,我喜歡自動向用戶發送電子郵件作為警告

我試過這個,但它沒有用......

$warning = get_post_meta( $post->ID, 'warningbox', true );

if ($warning == 1) {
    sendMailToUser();
}

我想這不是正確的解決方案,那么我該如何實現這個功能呢?

謝謝 :)

您確定定義了sendMailToUser()嗎,這不是標准的 WordPress 函數。 無論哪種方式,那里都不需要自定義函數。

您需要掛鈎save_post鈎子,檢查post_meta字段的值,然后使用wp_mail()函數提醒帖子作者。 聽起來在這里使用 AJAX 函數會更方便,但這也更復雜。

這樣的事情應該綽綽有余:

add_action( 'save_post', 'alert_author_warningbox', 99, 2 );
function alert_author_warningbox( $post_ID, $post ){
    if( get_post_meta($post_ID, 'warningbox', true) == 1 ){
        $to      = get_the_author_meta( 'user_email', $post_ID );
        $subject = sprintf( 'Warning Box enabled for "%s"', $post->post_title );
        $message = 'The warning box for your post has been enabled. Please contact the admin to resolve this warning.';

        wp_mail( $to, $subject, $message );
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM