Contents
WordPress JSON REST API
wp-api is an easy-to-use API which Access your WordPress site’s data through HTTP REST API.
wp-api Documentation:
- http://wp-api.org/ — WP REST API Documentation
- http://v2.wp-api.org/ — WP REST API v2 Documentation
This site will help to understand wp-api with sample source code.
I will guide to learn how to Setup a WordPress site with wp-api and use cURL, PHP, Java and AppInventor to innovate a web service.
* Curently I using http://wp-api.pw/ as a test site on sample code, you can change to your own wp-api site URL.
<TBD>
公式サイト:
* http://wp-api.org/ — original document
PythonでAPI検証する
自動投稿の準備 自動投稿について書かれているサイトを見ると、「WP REST API プラグインを入れる」とか「Application Passwords プラグインを入れる」とか、書かれていますが、全部、不要でした。
WordPress 左側にある [ユーザー]-[ユーザー一覧] から、自動投稿に使うユーザーを選択し、[編集]を押します。そして、下の方に「アプリケーションパスワード」という欄があります。ここでパスワードを発行するだけです。
import requests from urllib.parse import urljoin from datetime import datetime def wp_post(): url_base = 'https://apitest.tokyoec.com/' # your api site url = urljoin(url_base, 'wp-json/wp/v2/posts/') user = 'silky' # your api user name password = '1111 2222 3333 4444 5555 6666' # your api passwd content = 'これは自動投稿テストです。下記記事参照。<br>[kanren id="394"]' post = { 'title': 'WordPress自動投稿テスト', 'status': 'publish', # draft(下書き), publish(公開) 'slug': 'wordpress-auto-post-test', # パーマリンク 'categories': 5, # カテゴリID 'date': datetime.now().isoformat(), 'content': content, } res = requests.post( url, json=post, auth=(user, password), ) return res # if __name__ == '__main__': # wp_post() res = wp_post() res.text