Skip to content

Instantly share code, notes, and snippets.

@kurozumi
Last active October 30, 2018 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kurozumi/f3a50493c2114b2f1d2d65663b476580 to your computer and use it in GitHub Desktop.
Save kurozumi/f3a50493c2114b2f1d2d65663b476580 to your computer and use it in GitHub Desktop.
WP REST API v2で新規投稿
<?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