Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mahbubur001/ada8fa64e751a4d7a68db29aceb143cd to your computer and use it in GitHub Desktop.
Save mahbubur001/ada8fa64e751a4d7a68db29aceb143cd to your computer and use it in GitHub Desktop.

Revisions

  1. mahbubur001 revised this gist Feb 7, 2019. 1 changed file with 24 additions and 1 deletion.
    25 changes: 24 additions & 1 deletion WP API v2 image upload from media endpoint
    Original file line number Diff line number Diff line change
    @@ -31,4 +31,27 @@ $response = $client->request(
    ],

    ],
    );
    );

    $response = $client->request(
    'POST',
    'wp/v2/media',
    [
    'multipart' => [
    [
    'name' => 'file',
    'contents' => $fdata,
    'filename' => basename($path),
    ],
    ],

    'query' => [
    'status' => 'publish',
    'title' => 'File Title!',
    'comment_status' => 'open',
    'ping_status' => 'closed',
    'alt_text' => 'File Alt Text',
    'caption' => 'File Caption',
    'description' => 'File Description',
    ],
    ]);
  2. @s-hiroshi s-hiroshi created this gist Nov 24, 2015.
    34 changes: 34 additions & 0 deletions WP API v2 image upload from media endpoint
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    /*
    * WP REST API version 2.0-beta7
    * API base url ishttp://www.example.com/wp-json
    *
    * Reference
    * https://wordpress.org/support/topic/new-post-with-image
    */
    /*
    * Get Guzzle HTTP Client. That client has been authenticated.
    */
    $client = ...
    /*
    * Get binary data of image.
    * $path is file path to be uploaded.
    */
    $handle = fopen($path, 'r');
    $fdata = fread($handle, filesize($path));
    /*
    * Post media.
    * Request to WP REST API media endpoint
    */
    $response = $client->request(
    'POST',
    'wp/v2/media',
    [
    'multipart' => [
    [
    'name' => 'file',
    'contents' => $fdata,
    'filename' => basename($path),
    ],

    ],
    );