Uploading a picture in WordPress to your media gallery using xmlrpc
The Code:
include ‘wp-includes/class-IXR.php’;
//Wordpress Username
$username = “myWpUsername”;
//Wordpress Password
$password = “myWpPassword”;
//make sure you specify the exact path to your xmlrpc.php
$xmlrpc_url = ‘http://mywebsite.com/xmlrpc.php’;
$ixrClient = new IXR_Client($xmlrpc_url);
//url or image to add to the media library
$img = “http://wrmmedia.files.wordpress.com/2011/02/test-paper.jpg”;
//image_name.jpg
$image[‘name’] = md5($img). ‘.jpg’;
//type of image
$image[‘type’] = ‘image/jpg’;
//url or full path of an image
$image[‘bits’] = new IXR_Base64(file_get_contents($img));
//overwrite if exist
$image[‘overwrite’] = true;
//Upload the pic
if (!$ixrClient->query(‘wp.uploadFile’, ‘1’, $username, $password, $image))
{
//If upload is unsuccessfull, write the error in error log
error_log(‘An error occurred – ‘ . $ixrClient->getErrorCode() . “:” . $ixrClient->getErrorMessage());
}
else
{
//Get Response
$clientResponse = $ixrClient->getResponse();
//echo URL of Image
echo $clientResponse[‘url’];
}
Any questions? Please leave a comment below.