2

I am developing API for Wordpress using REST-API. Now I am stuck at one point where I am not able to develop the API through which I can upload the image to wordpress using Rest API.

I am using Postman to test API developed for my website

  • kinda hard to tell you what is the problem with your code when you do not show it ;) – Mark Kaplun May 31 '17 at 6:42
3

You can upload images just like your normal PHP/Wordpress file uploads.

Reference => wp_handle_upload

$mimes = array(
    'bmp'  => 'image/bmp',
    'gif'  => 'image/gif',
    'jpe'  => 'image/jpeg',
    'jpeg' => 'image/jpeg',
    'jpg'  => 'image/jpeg',
    'png'  => 'image/png',
    'tif'  => 'image/tiff',
    'tiff' => 'image/tiff'
);

$overrides = array(
    'mimes'     => $mimes,
    'test_form' => false
);

$upload = wp_handle_upload( $_FILES['YOUR_INPUT_FILE_NAME_HERE'], $overrides );
remove_filter( 'upload_dir', array($this, 'change_upload_dir') );
if ( isset( $upload['error'] ) ){
    // SOME UPLOAD ERROR OCCURED
} else {
    // File uploaded successfully. 
    $uploadedFileURL = $upload['url'];
    $uploadedFileName = basename($upload['url']);
}

And you can attach files in postman following way.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.