WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Image upload via form on php script (3 posts)

  1. Whithil XBR
    Member
    Posted 1 year ago #

    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.

  2. praeix
    Member
    Posted 1 year ago #

    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

  3. Whithil XBR
    Member
    Posted 1 year ago #

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

    $rpcurl='http://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".

Topic Closed

This topic has been closed to new replies.

About this Topic