最近我们在开发以wordpress作为后端管理的小程序的时候,需要用户在小程序前端提交评论给文章,于是就用到了这个函数wp_new_comment();
其实这个函数和另外一个向wordpress数据库添加新评论函数基本差不多,那就是wp_insert_comment();唯一的区别就是wp_new_comment()会通过过滤器和钩子判断评论数据是否被允许添加到wordpress数据库中,而wp_insert_comment函数则不会检查,直接添加到数据库。
参数说明
wp_new_comment函数参数与wp_insert_comment函数参数差不多,只是多了一个是否返回wp_error错误对象的参数而已。
wp_new_comment( array $commentdata,bool $avoid_die = false )
$commentdata
(array) 评论数组。
‘comment_agent’
‘comment_approved’
(int|string) 是否有评论已经得到了批准,默认1。
‘comment_author’
‘comment_author_email’
‘comment_author_IP’
‘comment_author_url’
‘comment_content’
‘comment_date’
(string) 评论提交的日期,手动指定时必须指定日期时区comment_date_gmt参数,默认当前日期。
‘comment_date_gmt’
‘comment_karma’
(int) The karma of the comment. Default 0.这玩意儿看不懂是什么,默认0
‘comment_parent’
(int) 评论所属父评论id,如果有的话(就是楼主评论id)
‘comment_post_ID’
‘comment_type’
‘comment_Meta’
(array) 键/值对数组存储在commentMeta新评论。
‘user_id’
$avoid_die
(bool)是否返回wp_error错误对象,默认false,不返回直接输出错误。
简单使用
$commentdata = compact('comment_post_ID','comment_author','comment_author_email','comment_author_url','comment_content','comment_type','comment_parent','user_ID');
$comment_id = wp_new_comment( $commentdata,true);
if(is_wp_error($comment_id)){
err( '重复评论' );
}
}
如果使用了ajax提交评论,返回wp_error错误对象会导致你的文章布局错乱,正确的ajax评论提交接口应判断是否返回了wp_error错误对象,根据返回不同自定义返回内容。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。