2
votes

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.

1
do you want to upload using upload field or just want to pass it the image?dipmala
i want to upload using upload fieldRuturajsinh Zala

1 Answers

2
votes

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']);
        }