在WordPress 4.4上,如何使用COMMENT_POST挂钩获取帖子ID

时间:2015-12-10 作者:Dudo1985

自WordPress 4.4以来,似乎不再可能使用get_the_ID()the_ID() 当你挂在墙上的时候comment_post 行动

我怎样才能得到post_id 何时保存新评论?

1 个回复
最合适的回答,由SO网友:Dave Romsey 整理而成

第三个参数,$commentdata 可用于获取发表评论的帖子的ID:

function wpse211367_comment( $comment_ID, $comment_approved, $commentdata ) {
    // The id for the post that the comment is related to is available
    // in the $commentdata array:
    $comment_post_id = $commentdata[\'comment_post_ID\'];
}
add_action( \'comment_post\', \'wpse211367_comment\', 10, 3 );
The$commentdata 阵列:

Array
(
    [comment_post_ID] => 149
    [comment_author] => dave
    [comment_author_email] => [email protected]
    [comment_author_url] => 
    [comment_content] => All work and no play makes Dave a dull boy.
    [comment_type] => 
    [comment_parent] => 0
    [user_ID] => 1
    [user_id] => 1
    [comment_author_IP] => ::1
    [comment_agent] => Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36
    [comment_date] => 2016-09-23 03:13:40
    [comment_date_gmt] => 2016-09-23 03:13:40
    [filtered] => 1
    [comment_approved] => 1
)

相关推荐

Delete comments function

我做了一个功能,向登录用户显示她的所有评论,并允许她通过与打印的每个评论相关的删除按钮单独删除这些评论。该功能起作用,但在执行表单操作后,刷新时页面仍会显示已删除的注释,并且仅当您再次转到该页面(不是刷新,而是通过单击浏览器地址栏上的返回键)时,页面才会显示已批准的注释。如何解决此问题?代码如下: function custom_delete_post_comment() { $comment_id = comment_ID();