Created
January 7, 2020 10:04
-
-
Save Alipio123/ea9985b9eaf0e9db1baf2376ba9b38b5 to your computer and use it in GitHub Desktop.
Upload image in Wordpress media using Wordpress API REST using wp_remote_post.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Add this to your function.php | |
function upload_image( $imageID, $login ) { | |
$request_url = 'https://DOMAINNAME.com/wp-json/wp/v2/media'; //change the domainname | |
$image_file_path = get_attached_file($imageID); //change this to your file meda path if your not throwing media file to other server | |
$image = file_get_contents( $image_file_path ); | |
$mime = mime_content_type( $image_file_path ); | |
$api_media_response = wp_remote_post( $request_url, array( | |
'headers' => array( | |
'Authorization' => 'Basic ' . base64_encode( $login ), //login format USERNAME:PASSWORD | |
'Content-Disposition' => 'attachment; filename='.basename($image_file_path).'', | |
'Content-Type' => $mime | |
), | |
'body' => $image | |
) ); | |
//this function return wp_remote_post | |
// more info => https://developer.wordpress.org/reference/functions/wp_remote_post/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment