Skip to content

Instantly share code, notes, and snippets.

@kurozumi

kurozumi/api-post.php

Last active Oct 30, 2018
Embed
What would you like to do?
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