WordPress: Posting images using XML-RPC

The following code uploads a file using xmlrpc... and without using all kinds of libs, you just need the regular php-xmlrpc module.

$blogid=1; //Post ID
$filetype = "image/jpeg";
$filename = "remote_filename.jpg";
xmlrpc_set_type($file,'base64'); // <-- required!
$params = array($blogid,$username,$password,
$request = xmlrpc_encode_request('wp.uploadFile',$params);
$result = go($request,$rpcurl);
function go($request,$rpcurl){
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_POSTFIELDS,$request );
    $result = curl_exec($ch);
    return $result;
© GeekLabInfo WordPress: Posting images using XML-RPC is a post from GeekLab.info. You are free to copy materials from GeekLab.info, but you are required to link back to http://www.geeklab.info

1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 4.50 out of 5)

3 comments on “WordPress: Posting images using XML-RPC”

  1. The above code is not working with the new wordpress version. I am trying to upload a music album cover via xmlrpc, it is showing nothing. Can you please update the above code?

  2. I've tested it, code still works perfectly to upload images on WordPress 4.7. The print_r line was not giving back much information though, because the "return $result" line was missing.

  3. Hi, I can upload the file to WP through RPC. I upload images. But how can I call the WP image process to work? Because I need this process to do the cropping/resizing uploaded images too. Right now the uploaded image only like a text file contains base64 string in a filename of .jpg.

Leave a Reply