WP REST API v2で新規投稿
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once( dirname( __FILE__ ) . '/wp-load.php' ); | |
// APIエンドポイントのURL | |
$url = 'http://sample.com/wp-json/wp/v2/posts'; | |
// ユーザー名(Application Passwordsプラグインでパスワードを発行したユーザーのもの) | |
$username = 'test'; | |
// パスワード(Application Passwordsプラグインで発行されたパスワード) | |
$password = 'c42C RIop uj9I QAlm ZOWx wwww'; | |
$response = wp_remote_post( $url, array( | |
'headers' => array( | |
'Authorization' => 'Basic ' . base64_encode( $username.':'.$password ) | |
), | |
'body' => array( | |
'title' => 'My test', | |
'status' => 'draft', // ok, we do not want to publish it immediately | |
'content' => 'lalala', | |
'categories' => 5, // category ID | |
'tags' => '1,4,23', // string, comma separated | |
'date' => '2015-05-05T10:00:00', // YYYY-MM-DDTHH:MM:SS | |
'excerpt' => 'Read this awesome post', | |
'password' => '12$45', | |
'slug' => 'new-test-post' // part of the URL usually | |
// more body params are here: | |
// developer.wordpress.org/rest-api/reference/posts/#create-a-post | |
) | |
) ); | |
// 正常の場合にはJSONが返される | |
$data = json_decode($response['body']); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment