WordPress主题制作过程中,我认为最为复杂的就是评论处理部分。当然这个复杂只是相对而言,不论多么复杂到最后都是对WordPress函数的灵活运用,下面我们一起用wp_insert_comment函数插入一条评论到数据库中,了解下wp_insert_comment函数的构成与使用方法。

WordPress主题制作过程中,我认为最为复杂的就是评论处理部分。当然这个复杂只是相对而言,不论多么复杂到最后都是对WordPress函数的灵活运用,下面我们一起用wp_insert_comment函数插入一条评论到数据库中,了解下wp_insert_comment函数的构成与使用方法。

函数描述

插入一条评论到数据库中。

函数原型

wp_insert_comment函数位与wp-includes/comment.php文件中,由于源码太长,这里就不贴代码了,大家可以去官方查看wp_insert_comment函数的源码,地址:https://developer.wordpress.org/reference/functions/wp_insert_comment/

wp_insert_comment( array $commentdata )

参数说明

$commentdata

评论数据数组,内容如下:

  • ‘comment_agent’
    (string) 用户评论时的代理标识,默认空。
  • ‘comment_approved’
    (int|string) 是否有评论已经得到了批准,默认1。
  • ‘comment_author’
    (string) 评论者的名字,默认为空。
  • ‘comment_author_email’
    (string) 评论者的邮箱地址,默认为空。
  • ‘comment_author_IP’
    (string) 评论者的ip,默认为空。
  • ‘comment_author_url’
    (string) 评论者的url地址,默认为空。
  • ‘comment_content’
    (string) 评论内容。
  • ‘comment_date’
    (string) 评论提交的日期,手动指定时必须指定日期时区comment_date_gmt参数,默认当前日期。
  • ‘comment_date_gmt’
    (string) 评论提交时的时区,默认是站点所选时区。
  • ‘comment_karma’
    (int) The karma of the comment. Default 0.这玩意儿看不懂是什么,默认0
  • ‘comment_parent’
    (int) 评论所属父评论id,如果有的话(就是楼主评论id)
  • ‘comment_post_ID’
    (int) 涉及到的评论文章id,默认0。
  • ‘comment_type’
    (string) 评论类型,默认空。
  • ‘comment_meta’
    (array) 键/值对数组存储在commentmeta新评论。
  • ‘user_id’
    (int)评论用户的id,默认0。

返回值

成功插入返回插入的评论id,失败返回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_insert_comment( $commentdata );

评论插入数据库的WordPress函数还有:WordPress函数wp_new_comment添加新评论

————下载必看————

1、本站资源均通过互联网公开合法渠道获取,仅供阅读测试,请在下载后24小时内删除,谢谢合作!
2、如无特殊说明,解压密码为:yudouyudou
3、部分压缩为part1、part2类型的,需要全部下载到电脑才能解压
4、解压工具推荐:电脑端推荐使用Bandizip(最新版),苹果电脑端用RAR解压王。
5、关于下载速度:下载速度慢的,请开通百度网盘超级VIP会员下载。
6、关于付费:所有付费行为均为支持本站日常运作。
7、版权归作者或出版社方所有,本站不对涉及的版权问题负法律责任。若版权方认为本站侵权,请联系客服或发送邮件处理。
资源失效请添加客服微信 “ s45246130 ”,发送资源名称,我们会及时重新补发