{"id":81,"date":"2012-07-29T16:45:00","date_gmt":"2012-07-29T16:45:00","guid":{"rendered":"https:\/\/www.tech-and-dev.com\/blog\/2012\/07\/29\/uploading-a-picture-in-wordpress-to-your-media-gallery-using-xmlrpc\/"},"modified":"2012-07-29T16:45:00","modified_gmt":"2012-07-29T16:45:00","slug":"uploading-picture-in-wordpress-using-xmlrpc","status":"publish","type":"post","link":"https:\/\/www.tech-and-dev.com\/blog\/2012\/07\/uploading-picture-in-wordpress-using-xmlrpc.html","title":{"rendered":"Uploading a picture in WordPress to your media gallery using xmlrpc"},"content":{"rendered":"<div style=\"text-align: justify;\">If you&#8217;re  automatically creating a wordpress post or page from php code, rather than from the wordpress admin panel, sometimes you might need to add a pic from a different website to your upload directory, and then display that pic inside your wordpress page.<\/div>\n<div style=\"text-align: justify;\"><\/div>\n<div style=\"text-align: justify;\">In This post, I will show you to how to upload a picture to your wordpress inside your media library and retrieve its URL using xmlrpc library.<\/div>\n<div style=\"text-align: justify;\"><\/div>\n<h3 style=\"text-align: justify;\">The Code:<\/h3>\n<div style=\"background-color: #f3f3f3; padding-left: 5px;\"><span style=\"color: #38761d;\">\/\/make sure to include the correct path to class-IXR.php, depending on your&nbsp; current Directoryr<\/span><br \/>include &#8216;wp-includes\/class-IXR.php&#8217;;&nbsp;<span style=\"color: #38761d;\"><\/span><\/p>\n<p><span style=\"color: #38761d;\">\/\/Wordpress Username<\/span> <br \/>$username = &#8220;myWpUsername&#8221;;<br \/><span style=\"color: #38761d;\">\/\/Wordpress Password<\/span><br \/>$password = &#8220;myWpPassword&#8221;;<br \/><span style=\"color: #38761d;\">\/\/make sure you specify the exact path to your xmlrpc.php<\/span> <br \/>$xmlrpc_url = &#8216;http:\/\/mywebsite.com\/xmlrpc.php&#8217;;&nbsp;<span style=\"color: #38761d;\"><\/span><\/p>\n<p>$ixrClient = new IXR_Client($xmlrpc_url);<\/p>\n<p><span style=\"color: #38761d;\">\/\/url or image to add to the media library<\/span><br \/>$img = &#8220;http:\/\/wrmmedia.files.wordpress.com\/2011\/02\/test-paper.jpg&#8221;; <span style=\"color: #38761d;\"><\/span><br \/><span style=\"color: #38761d;\">\/\/image_name.jpg<\/span><br \/>$image[&#8216;name&#8217;] = md5($img). &#8216;.jpg&#8217;;<span style=\"color: #38761d;\"> <\/span><br \/><span style=\"color: #38761d;\">\/\/type of image<\/span><br \/>$image[&#8216;type&#8217;] = &#8216;image\/jpg&#8217;;<br \/><span style=\"color: #38761d;\">\/\/url or full path of an image<\/span><br \/>$image[&#8216;bits&#8217;] = new IXR_Base64(file_get_contents($img)); <br \/><span style=\"color: #38761d;\"> \/\/overwrite if exist<\/span><br \/>$image[&#8216;overwrite&#8217;] = true;<span style=\"color: #38761d;\"><br \/><\/span><\/p>\n<p><span style=\"color: #38761d;\"> \/\/Upload the pic<\/span><br \/>if (!$ixrClient-&gt;query(&#8216;wp.uploadFile&#8217;, &#8216;1&#8217;, $username, $password, $image))<br \/>{<br \/>&nbsp;&nbsp;&nbsp; <span style=\"color: #38761d;\">\/\/If upload is unsuccessfull, write the error in error log<\/span><br \/>&nbsp;&nbsp;&nbsp; error_log(&#8216;An error occurred &#8211; &#8216; . $ixrClient-&gt;getErrorCode() . &#8220;:&#8221; . $ixrClient-&gt;getErrorMessage());<br \/>}<br \/>else<br \/>{<br \/>&nbsp;&nbsp;&nbsp;&nbsp; <span style=\"color: #38761d;\">\/\/Get Response<\/span> <br \/>&nbsp;&nbsp;&nbsp; $clientResponse = $ixrClient-&gt;getResponse();<br \/>&nbsp;&nbsp;&nbsp; <span style=\"color: #38761d;\">\/\/echo URL of Image<\/span> <br \/>&nbsp;&nbsp;&nbsp; echo $clientResponse[&#8216;url&#8217;];&nbsp;&nbsp;<span style=\"color: #38761d;\"><\/span><br \/>} <\/p>\n<p><\/div>\n<p>Any questions? Please leave a comment below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you&#8217;re automatically creating a wordpress post or page from php code, rather than from the wordpress admin panel, sometimes you might need to add a pic from a different website to your upload directory, and then display that pic inside your wordpress page. In This post, I will show you to how to upload [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[22,111,8,112],"tags":[],"class_list":["post-81","post","type-post","status-publish","format-standard","hentry","category-php","category-scraper","category-wordpress","category-xmlrpc"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/posts\/81","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/comments?post=81"}],"version-history":[{"count":0,"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/posts\/81\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/media?parent=81"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/categories?post=81"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tech-and-dev.com\/blog\/wp-json\/wp\/v2\/tags?post=81"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}