• Resolved Whithil XBR

    (@whithil-xbr)


    I’m using a simple XMLRPC form to create new posts for a real estate website, so people from there would be able to quickly and easily add new posts.
    Everything went ok, even the custom fields, now I just need a way to send images to the wordpress media library and get the link from the uploaded image in a custom field. But I couldn’t figure out this part.
    Is there a way to do it?

    Any help will be appreciated.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Maybe this post will help? It uses the Settings API (which everybody should be using anyway).

    Multiple Upload Inputs In A WordPress Theme Options Page

    Thread Starter Whithil XBR

    (@whithil-xbr)

    Thanks but I already found this code, and it worked fine to upload the images:

    $rpcurl='https://www.geeklab.info/xmlrpc.php';
    $username='admin';
    $password='your-password';
    $blogid=1; //Post ID
    
    $file=file_get_contents('file.jpg');
    $filetype = "image/jpeg";
    $filename = "remote_filename.jpg";
    
    xmlrpc_set_type($file,'base64'); // <-- required!
    $params = array($blogid,$username,$password,
                array('name'=>$filename,'type'=>$filetype,'bits'=>$file,'overwrite'=>false));
    $request = xmlrpc_encode_request('wp.uploadFile',$params);
    
    $result = go($request,$rpcurl);
    print_r($result);
    
    function go($request,$rpcurl){
        $ch = curl_init();
        curl_setopt($ch,CURLOPT_POST,1);
        curl_setopt($ch,CURLOPT_URL,$rpcurl);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$request );
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        $result = curl_exec($ch);
    }

    now I just need to get the url from the uploaded file in wordpress’ “media-library”.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Image upload via form on php script’ is closed to new replies.