首页 > 解决方案 > 在 WordPress 网站上通过电子邮件通知来宾作者

问题描述

我在 WordPress 网站上使用了一个名为 User Submitted Posts 的插件。该插件使用简码创建表单。当用户填写表单时,提交的信息用于创建帖子。通过表单提交的姓名作为来宾作者存储在数据库中。提交的电子邮件存储为 user_submit_email。

我遇到的问题是,当有人评论他们的帖子时,来宾作者没有收到通知。我正在尝试弄清楚如何设置它,以便通过电子邮件将通知发送给来宾作者,而无需网站所有者对每个帖子提交进行手动操作来设置它。

下面的代码是我从用户提交的帖子插件中找到的。我将它放在网站子主题的functions.php 文件中。

add_filter( 'the_author', 'guest_author_name' );
add_filter( 'get_the_author_display_name', 'guest_author_name' );
 
function guest_author_name( $name ) {
global $post;
 
$author = get_post_meta( $post->ID, 'guest-author', true );
 
if ( $author )
$name = $author;
 
return $name;
}

标签: phpwordpress

解决方案


你需要把它交给一个动作钩子,而不是一个过滤器。如果要处理提交评论的用户,请使用 wp_insert_comment_hook。

假设你存储了 post meta guest_author,你会做类似的事情

function wpr_authorNotification( $comment_id, $comment_object) {
    $post = get_post($comment_object->comment_post_ID);

    if ( ! isset($post->guest_author) ) {
        return;
    }
    $message = "";
    $subject = "";;
    wp_mail($post->guest_author, $subject, $message);
}

add_action('wp_insert_comment', 'wpr_authorNotification', 99, 2);

推荐阅读