php - 在我的 WordPress 网站中进行评论后,如何触发功能?
问题描述
我想为一个插件编写一些代码,在用户进行评论后,(wp_comments
表被更新)它触发一个告诉它做某事的函数。
我如何知道是否进行了新的评论,因为评论作为评论保存到数据库中,带有comment_type
= 'review'?
谢谢!
解决方案
您可以使用comment_post 操作挂钩在创建新评论时执行操作:
/**
* Performs an action after a review has been created.
*
* @param int $comment_ID
* @param int|string $comment_approved (1 if approved, 0 if not, 'spam' if spam)
* @param array $commentdata
*/
function show_message_function( $comment_ID, $comment_approved, $commentdata ) {
if ( 'review' == $commentdata['comment_type'] ) {
// A new review has been created, do something here.
}
}
add_action( 'comment_post', 'show_message_function', 10, 3 );
$commentdata
数组看起来像这样:
Array
(
[comment_post_ID] => 16
[comment_author] => John Doe
[comment_author_email] => me@example.com
[comment_author_url] =>
[comment_content] => My awesome review!
[comment_type] => review
[comment_parent] => 0
[user_ID] => 1
[user_id] => 1
[comment_author_IP] => ::1
[comment_agent] => Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0
[comment_date] => 2019-04-13 14:11:45
[comment_date_gmt] => 2019-04-13 14:11:45
[filtered] => 1
[comment_approved] => 1
)
推荐阅读
- python - ModuleNotFoundError:运行 Pyinstaller 时没有名为 PIL 的模块
- r - 比较两个数据帧之间的纬度和经度
- swift - 如何更改 Xcode 为每个堆栈创建单独的预览?
- python - Python:AttributeError:模块'kubernetes'没有属性'config'
- vimeo - vimeo API 如何进行批量请求
- reactjs - 如何生成动态 React 标记
- node.js - 公牛在第一次之后没有增加新的工作
- vue.js - 如何将本地存储值传递给控制器 laravel?
- android - 如何为提示和错误文本应用不同的颜色 | 材料设计文本字段
- awk - 如何循环遍历数组的所有子集?