有么有那一刻感觉明明自己很闲,却感觉很累呢?
唠叨一会
当你看到这篇文章的时候就意味着某个小程序已经再做评论的路上了!愿天堂的代码没有bug,愿rest api接口可以做个人;wordpress rest api创建评论接口实现留言+apifox接口测试
api文档
Wodpress Rest Api Create a Comment:https://developer.wordpress.org/rest-api/reference/comments/
说明
通过以上的文档说明可以看到我们小程序获取的评论是
GET /wp/v2/comments
而新建一个评论提交依然还是这个接口,只是get换成了post ==是不是感觉很神奇
POST /wp/v2/comments
参数
author |
The ID of the user object, if author was a user. |
author_email |
Email address for the object author. |
author_ip |
IP address for the object author. |
author_name |
Display name for the object author. |
author_url |
URL for the object author. |
author_user_agent |
User agent for the object author. |
content |
The content for the object. |
date |
The date the object was published, in the site’s timezone. |
date_gmt |
The date the object was published, as GMT. |
parent |
The ID for the parent of the object. |
post |
The ID of the associated post object. |
status |
State of the object. |
meta |
Meta fields. |
apifox请求
难得有一个测试接口的软件是中文的 Apidox下载:https://www.apifox.cn/
下载安装后打开安下图的方式新建一个接口,请求方式改成post和输入我们我们的接口地址,因为我是本地测试随意用取了域名,我们采用最简单的url传参数方式来做测试;在请求参数中我们选择Params,数据中我们有几个是必要,例如:post(评论文章的id),author_name(评论用户名称),author_email(评论用户电子邮件),添加好了之后我们点击保存随后运行
点击运行后,来到了发起请求的界面,要注意右上方要选择环境我们可以选择测试环境或者正式环境,随后点击发送
报错了,提示我们“抱歉,您必须登录后再评论”,因为post请求必须要登录,可是我们在小程序中就不好操作了;虽然百度很多满分作文但还是有写靠谱的,我们先在主题的functions.php文件添加代码
function filter_rest_allow_anonymous_comments() { return true; } add_filter('rest_allow_anonymous_comments','filter_rest_allow_anonymous_comments');
在点击一次发送,返回201,后台收到留言内容。如果有提示没有对应的文章内容可以在运行界面下检测Params请求数据(切记)
评论列表(10条)
老板你好,用星宿1.4部署好以后在详情页提交问题,提示“创建失败”是因为什么呀
@我是傻逼:文档里面有说明 需要修改下数据库
不错,值得收藏分享!
辛苦了!~~
是准备集成到小程序里吗
@一位好心的富婆:是的
你好呀
@小天:哎
帅
@嘀嘀嘀:嘟嘟咯