2

I want upload image using api on my page so any tell me how i do it?

Now i work with this api

http://localhost/test_project/wp-json/wp/v2/media

but it's doesn't help me for upload image on page or post.

I use wordpress json api plugin.

2
  • do you want to upload using upload field or just want to pass it the image? Commented Aug 16, 2018 at 7:43
  • 2
    i want to upload using upload field Commented Aug 16, 2018 at 8:55

1 Answer 1

2

You can do using following code. Here I have use wp_handle_upload.

       $param = array('search' => 'XXXXXXXX');

        $imagetype = 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'
        );

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

        $upload_file = wp_handle_upload( $_FILES['YOUR_UPLOAD_FILE_NAME'], $override );
        remove_filter( 'upload_dir', array($this, 'change_upload_dir') );
        if ( isset( $upload['error'] ) ){
            // DO ACTION ACCORDINGLY
        } else {
            // File is uploaded successfully. 
            $uploaded_file_url = $upload_file['url'];
            $uploaded_file_name = basename($upload_file['url']);
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.