之前看到给朋友做网站的英国老爷爷装了一堆 Wordpress Plugin,正好手边有个项目把 Wordpress 当 REST API 来用,就小小研究下了装了下面这几个 Plugin:
- JSON API
- Magic Fields 2
- WP Super Cache
- Disable Google Fonts
- WP Acceleration for China
- WordPress Security Firewall
- WP-DBManager
- Disable Comments
- Disable All WordPress Updates
- Custom Dashboard Help
- Admin Dashboard Last Edits
- Admin Menu Editor
如果安装 Wordpress Plugin 提示权限不够需要输入 FTP 账号验证的话,可以 SSH 到 Server 上配置下权限,Apache 和 Nginx 的默认用户是 www-data,通过以下文件可以看到:
$ cat /etc/nginx/nginx.conf
或者:
$ ps -ef |grep apache2
我们要做的就是把 Wordpress 所在目录的所有权交给 www-data:
$ sudo chgrp -R www-data ./wordpress
$ sudo chown -R www-data:www-data ./wordpress
接下来,重启,使配置生效:
$ sudo service nginx restart
然后就发现终于可以愉快地玩耍啦!
1. JSON API
虽然 Wordpress 在 4.4 Clifford 开始把 REST API 纳入到 Core 中,但是我接触的时候用了 JSON API 这个插件。
This plugin was created at The Museum of Modern Art for the weblog Inside/Out, which is served from Ruby on Rails. Instead of reimplementing the site templates as a WordPress theme, we opted for a Rails front-end that displays content served from a WordPress back-end. JSON API provides the necessary interface for retrieving content and accepting comment submissions.
事实上我就是看了这段介绍才对 REST API 有初步的认识,使用非常简单,比如对一个刚装完的 Wordpress 发送 GET request http://localhost/wordpress/?json=1
会返回 JSON 如下:
1 | { |
Our old friend, in JSON format now. LOL. 我用了 NodeJS 的 Request middleware 去 call Wordpress 的 REST API 获取 JSON。
2. Magic Fields 2
强烈推荐 Magic Fields 2 这个插件,可以根据不同需求生成表单,主要是