php - 在 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;
}
解决方案
你需要把它交给一个动作钩子,而不是一个过滤器。如果要处理提交评论的用户,请使用 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);
推荐阅读
- windows - 将 unicode 文本导入 cmd.exe
- javascript - 如何通过 PHP 和 JS 重用相同的混合代码片段?最佳实践?
- regex - Monaco-editor 没有正确评估负前瞻正则表达式
- swift - 将数据从一个类快速传递到另一个类
- docker - nginx: [emerg] open() "/run/nginx.pid" 失败 (13: Permission denied)
- python - pandas 究竟在为这些数据切片做些什么呢?
- c++ - 使用 Qt 内存管理时对 clang 静态分析的误报
- php - 缺少属性 - 使用 SimpleXMLElement 解析 XML -
- css - 如何在 Rgba SASS/SCSS 中显示具有不透明度的 CurrentColor?
- gitlab - Gitlab CI/CD:命令脚本中的 Concat 变量