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添加新评论

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。