首页 > 解决方案 > 在我的 WordPress 网站中进行评论后,如何触发功能?

问题描述

我想为一个插件编写一些代码,在用户进行评论后,(wp_comments表被更新)它触发一个告诉它做某事的函数。

我如何知道是否进行了新的评论,因为评论作为评论保存到数据库中,带有comment_type= 'review'?

谢谢!

标签: phpwordpressfunctiontriggersaction

解决方案


您可以使用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
)

推荐阅读