Skip to content

Instantly share code, notes, and snippets.

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/media';
// ユーザー名(Application Passwordsプラグインでパスワードを発行したユーザーのもの)
$username = 'test';
// パスワード(Application Passwordsプラグインで発行されたパスワード)
$password = 'c42C RIop uj9I QAlm ZOWx wwww';
$file = "https://grapee.jp/wp-content/uploads/32187_main2.jpg";
$data = wp_remote_get($file);
$response = wp_remote_post( $url, array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( $username.':'.$password ),
'Content-Disposition' => 'form-data;filename=' . basename($file)
),
'body' => $data["body"]
) );
$data = json_decode($response['body']);
echo $data->id;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment