wordpress Posting Content via XML RPC
 Rating: 1 Star2 Stars3 Stars4 Stars5 Stars
Loading ... Loading ...

WordPress XMLRPC – Posting Content From Outside WordPress Admin Panel



Many WordPress blog owners these days prefer to post their blog content from outside. This facilitates the site owners to post blog posts without even loging into their WordPress admin panel. WordPress supports XMLRPC which can take requests and perform specific operations i.e. ‘Add Post’, ‘Edit Post’, ‘View Recent Posts’ or ‘Save a draft’.

Lets start coding now, but before we go into any details lets ensure that our WordPress installation is allowing XMLRPC (default is turned off). Go to WordPress Admin => Settings => Writing and ensure the XML-RPC is enabled. Click save after checking the enable box. We are good and our WordPress is ready to take XML-RPC commands.

XML-RPC is by default enabled in WordPress 3.5+ that’s cool. On the other hand what if you are not using XMLRPC you can anytime disable it like this. Just place the following line of code in your themes functions.php file and you are all set.

But for the sake of our article, i assume you will not disable XMLRPC.

We will use IXR Library to incorporates both client and server classes, as it is designed to hide as much of the workings of XML-RPC from the user as possible. A key feature of the library is automatic type conversion from PHP types to XML-RPC types and vice-versa. This should enable developers to write web services with very little knowledge of the underlying XML-RPC standard.

Download IXR_Library.php.inc (rename it to IXR_Librabry.php.inc) or download the latest version from Incutio

Add a WordPress Blog Post via php

Get Recent 10 WordPress Blog Posts via php

Edit WordPress Blog Post via php

Posting Images and Marking Featured Via WordPress XMLRPC

The new wordpress thumbnail feature is so great, now all you have to do is upload an image and mark it as featured. If this is something you are looking for via WordPress XMLRPC then look at the following code

Code Explanation

Here is how it works

1. We are assuming you have already uploaded the file in your application, all you need to do is read the image file by this code

2. Connect to your WordPress XMLRPC

3. Now, upload the file to wordpress via XMLRPC

Finally lets see the full code example, how this uploaded image is marked as featured in your WordPress site.

Based on the code given below you can define your own logic of posting images in your article. Suppose you want to show the uploaded image within your content, you can append the $thumbnail_image within your $body object e.g.

I have made a centralized Word-press control panel where i am posting blog posts in more than 20 different blogs using the same admin area. There is no need to log into 20 blog accounts to post my content. I would love to hear your thoughts on this. Please leave me a comment and let me know. Don’t forget to subscribe our RSS to receive latest updates.our RSS to receive latest updates.

Related Posts

php-domain-information

How To Get Domain, Subdomain, TLD, CTLD & Path from URL In PHP

braintree-payment-gateway-integration-php

Integrating BrainTree Payment Gateway With PHP

How to create XML with PHP Mysql

How To Create XML With PHP MySQL

wordpress-gallery

How To Fix WordPress Gallery Issues Without A Plugin

  • http://www.webmasterdeck.com Diego

    Superb!!! I've being looking for this for around 15 days!!! and this is the only code I've found that works. Thanks, you saved my day :)

  • http://juneinc.blogspot.com Wirat O

    Hello Ashish !! You've done the brilliant thing! I'm searching for this info around half day but no one seems pretty easy for me as yours.

  • http://www.hurricanesoftwares.com Ashish

    I hope the code worked perfectly for you. I have programmed the same code for blogger, posterous and tumblr as well.

  • Wirat O

    Great :-)

    Ashish, about creating new post via php script. Is there anyway to choose post-url I want?

    Example, my new post title is Best WordPress Plugin I Recommend and I want post-url to be best-wordpress-plugin-i-recommended

    Thanks

  • http://www.hurricanesoftwares.com Ashish

    I haven't used and thought of sending post-URL as a parameter. To my knowledge WordPress automatically creates a new post URL (permalink) from the post title. Can you tell me your exact requirement so i can think deeper in this.

  • Wirat O

    What I really want to do is to make a connection of all posts I have created similar to this picture http://download.smilesquare.com/linkwheel.gif

  • http://www.hurricanesoftwares.com Ashish

    Look at get recent 10 posts code and it will give you the last 10 blog posts. Just find out the permalink and use in your own logic. I don't think you need to send blog post URL specifically.

    You can see all the blog posts from all the blogs (blog1-blog10) and then fetch and show the desired data in your money site.

  • Wirat O

    Thank you Ashish for your suggestion! I have implemented some basic algorithms and now it's working as I want.

  • Gulshan Saini

    Hello Ashish,

    I am facing problem while posting to multiple categories

    Thanks
    Gulshan

  • http://www.hurricanesoftwares.com Ashish

    Gulshan,

    WordPress will take categories as an array e.g. $category=”category1, category2″; you can see this at the top of my code. Keep in mind that when you associate a post in multiple categories WordPress will take the category with lowest id while assigning Permalink.

    What's the exact problem you are facing?

  • Bergmp

    Nice script friend, thanks, I have a simple question: how do I include images into post, and how to align them?

  • Code Seo

    Sorry for my englis, i'm French but great article to understand the way to multi post.

    If you want i've developped a little class for WordPress and XMLRPC: http://www.scripts-marketing.com/lfe-une-classe-et-un-outil-pour-faire-vivre-votre-ferme/

    With anonymous comment posting because a default comment is for the admin :)

  • http://www.hurricanesoftwares.com Ashish

    Currently in the above script there is no way to attach images and upload them to the blog server but you can anytime upload images in flickr or anywhere else and use img src of flickr.

  • Admin

    Dear Ashish,

    how to add post if I have the same sub-ategory name in several main categories ?
    Example main_category1 -> subcategory1 and maincategory2 -> subcategory1

  • http://www.hurricanesoftwares.com Ashish

    If you add posts to the same subcategory name as per your example, this script will add a post to all occurrences of that subcategory name in your blog.

  • Harry

    Is it possible to add multiple posts in bulk and if so how can i do that?

  • http://www.hurricanesoftwares.com Ashish

    You have complete control over the loop so just play with it nicely and send as many posts as you want. Loop through the get the entire new posts content in array and loop $client->query('metaWeblog.editPost', $params)) to post multiple posts.

  • Zeekapitein

    Hi

    Do you know how to add a custom date to the new post? Thnx for the great post!

    greetz

  • http://www.directorysubmissionservices.net Nick

    WordPress is made to get and update. You can’t just set.
    I added the code you need to the samples code … it’s last.

    There are two ways to go both explained there. You can’t use a template name but the actual php file name:
    basename($template);

    To create a page with a different template just assign $page->wp_page_template = $file;
    There’s no set_template function but I might add it. I’ll have to think page templates a bit through to make it easier.

  • Eric

    I got the script to work beautifully but I don’t understand how to add an image to my post.

  • http://blog.nickolabs.com/ Nickolas Simard

    The Incutio XML-RPC Library has been updated to version 1.7.4 as of September 7th. Does those script works with the current version of XML-RPC (1.7.4)? Because I’m experiencing some issue with it… :S

  • http://www.hurricanesoftwares.com Ashish

    I haven’t tested this with the current Incutio XML-RPC but i don’t think it should break. Could you tell me what is the error you are receiving while executing the code?

  • http://blog.nickolabs.com/ Nickolas Simard

    I had a “not 200 http code”. Upon investigating, it’s because the website was using an active “Maintenance Mode” plugin… This is still weird, because when I get to the website using a browser, I do receive a http code 200…

    Anyway, we could say it’s solved.

    Also, I tested the script with the latest library and it was working perfectly (apart from the acute character breaking, such as é, à… but this is just an encoding problem).

    Thanks for the support!

  • http://www.hurricanesoftwares.com Ashish

    I tested the code myself and it worked fine, encoding problems can be resolved easily with normal php coding. So, before you call functions make sure you take care of encoding characters.

  • Pete

    good stuff you have here, but I have one issue, please could you explain how and where am to add the above php file in my wp? I really dont know what else to do after enabling XMLRPC.

  • http://blog.nickolabs.com/ Nickolas Simard

    You don’t really add that PHP into your WordPress installation. The XMLRPC protocol is to enable publishing into a remote WordPress website…

    Let’s say I want “website1.com” to publish on “blogsite2.com”, I’d put the PHP script on “website1.com” (let’s say because you got a database there that will push post into your “blogsite2.com” wordpress) and configure it using “blogsite2.com” user access, password, and xmlrpc.php path (which would be, by default, “blogsite2.com/xmlrpc.php”).

    I hope I was clear! :D

  • http://www.iy8.me Y8

    How to post with:
    $category=”12, 15″; with 12 is category id 1 and 15 is category 2

  • http://www.hurricanesoftwares.com Ashish

    What’s the problem here you have wrote the code in correct way

    $category = “12, 15″; is correct, just make sure you have categories pre-created in your WordPress which have id’s 12 and 15 respectively.

  • http://blog.nickolabs.com/ Nickolas Simard

    You can refer to your categories using their actual real name:
    $category = array(‘Propriétés’,
    ‘Jonquière’,
    ‘Mon Méga Blog!’);

  • Casdas

    the category thing is not working, please sort it out,
    using array($category) it does not work, but with array(‘Action’,'games’) it works. I want to put a variable in the array. Please tell me how can it be done. The current codes does not work.

  • Contact

    it just not post in the category added in the array.

  • http://www.stylishandtrendy.com/ Dev @ Women Magazine

    It worked for me, i think $category is not meant for passing arrays. You should provide actual categories (which exists in your blog). So this is how you should pass the values $category=”Health, Fitness”; Considering Health and Fitness categories already created in your blog, if not it will simply neglect them.

    For passing additional values you should code properly and make sure you provide valid values to $categories and not array.

  • Contact

    thanx for the reply, the category I am assigning are $category=”2010, Action”;

    I already have this two categories in my wordpress blog. 2010 and Action exists.
    However when I use ‘$categories’=array(’2010′, ‘Action’) ….It works properly. New posts gets assigned with 2010 & action.
    But I want it to be variable. So please tell me I am not able to use this ‘categories’=>array($category),

    One more thing the above code works if there is only one value in $category which is assigned before. Just it does not work for multiple categories

  • Contact

    please somebody reply as soon as possible.
    I already have this two categories in my wordpress blog. 2010 and Action exists.
    However when I use ‘$categories’=array(’2010′, ‘Action’) ….It works properly. New posts gets assigned with 2010 & action.
    But I want it to be variable. So please tell me I am not able to use this ‘categories’=>array($category),

    One more thing the above code works if there is only one value in $category which is assigned before. Just it does not work for multiple categories

  • http://blog.nickolabs.com/ Nickolas Simard

    Here’s what I did:

    $category = array(21, // Hardcoded
    utf8_encode(‘Catégorie’), // DBB query
    ‘Action’,
    ’2010′
    );

    The utf8_encode is because that particular name came from a database query… so it might not be needed actually…
    Also, it IS better to use the IDs of the categories… so you could build yourself a “get_cat_id” function if needed…

    Finally, which is probably your issue here, when setting the “category” parameters of the “$post” object, you DO NOT need to redeclare your array as an array.

    ‘categories’=>array($category) // This is WRONG, since $category is already an array… what this does it place the array into another array, making the category itself to deep to be easily queryable.
    ‘categories’=>$category // This will work, if $category is already an array.

  • Contact

    thanks for the reply.

    Its still not working. I used the codes you mentioned
    $category = array(21, // Hardcoded
    utf8_encode(‘Catégorie’), // DBB query
    ‘Action’,
    ’2010′
    );

    and

    ‘categories’=>$category // This will work, if $category is already an array.

  • Contact

    For more info this is the code I am using

    $category = array(21, // Hardcoded
    utf8_encode(’2009′), // DBB query
    ‘Action’,
    ’2010′
    );

    $title = htmlentities($title,ENT_NOQUOTES,$encoding);
    $keywords = htmlentities($keywords,ENT_NOQUOTES,$encoding);

    $content = array(
    ‘title’=>$title,
    ‘description’=>$body,
    ‘mt_allow_comments’=>0, // 1 to allow comments
    ‘mt_allow_pings’=>0, // 1 to allow trackbacks
    ‘post_type’=>’post’,
    ‘mt_keywords’=>$keywords,
    ‘categories’=>$category
    ‘custom_fields’ => array($customfields)

    );

  • http://blog.nickolabs.com/ Nickolas Simard

    You lack a comma “,” after the $category. Also, the code I provided is an example; you have to use your own IDs…

    For more information about array usage, see http://php.net/manual/en/language.types.array.php

  • Contact

    WOWWWWWWWWWWWWW super man…………thanks a lot….I tried it once again from starting. IT works like charm……..thanks man……really appreciate.
    I am posting a request at your blog. plz see to it

  • http://blog.nickolabs.com/ Nickolas Simard

    Glad I could help.

  • Contact

    can we add new categories using xmlrpc or any other method?

  • http://pulse.yahoo.com/_WDRKRZ6JO7QE6VL63UTINEFJOI Stanley

    Would u please tell me where should I place the IXR_Librabry.php.inc file? Thanks.

  • http://www.hurricanesoftwares.com Ashish

    IXR_Library.php.inc should be placed in same folder which contains the rest of the code files. If you plan to place the libraries under specific folder you can do so but you should change the path in this code.

  • Stian

    Hey

    I am using this script and it works perfectly. However, it sometimes returns false even tho the post is added to the blog.

    Do you know why this happens?

    - Stian

  • http://BrownShoes.net Brown Shoes

    Thank you for the script! It worked right out of the box. I wish there is a way to attach an image as well.

  • Bruce

    what about “permalinks”?

  • http://www.hurricanesoftwares.com Ashish

    Permalinks are automatically created based on the Article TITLE. People write post title and Permalink is automatically created, same mechanism is used here.

  • http://www.hurricanesoftwares.com Ashish

    You can use metaWeblog.newMediaObject as that should work for uploading files via XML-RPC.

    Code should look something like this

    Similarly, you can upload images, mpg, mp3′s etc.

  • amit

    Ashish as you mentioned you are using this script to post in more than 20 blog, can u share full script with me. you have my email id. plz revert me back.

  • http://www.hurricanesoftwares.com Ashish

    Amit, the script i shared above is the same which i am using. with little bit of php you can modify and send the parameters of your choice which helps in posting multiple sites

  • http://www.diysolarwindpower.info B R Stimpson

    Is it possible to use xmlrpc to get a list of categories before the post is submitted ?

  • http://www.hurricanesoftwares.com Ashish

    Yes you can use metaWeblog.getCategories function to retrieve all the categories before you insert a new post. Use it like this

    metaWeblog.getCategories (blogid, username, password)

    This will returns an array of structs that contains one struct for each category. Each category struct contains both a description and title field which each contain the name of the category. Following is a format which will be returned by metaWeblog.getCategories

    struct{
    string description;
    string title;
    }

  • Pingback: How to post content to a Wordpress blog from the command line | Free Software Tips and Tricks at Zona-M

  • http://stop.zona-m.net Marco Fioretti

    Hi,
    just for completeness, it is possible to do (almost) the same things even without PHP. Here’s how I do it with Perl:
    http://freesoftware.zona-m.net/how-to-post-content-to-a-wordpress-blog-from-the-command-line/
    HTH

  • Pingback: Darence ANG » Blog Archive » How to post content to a WordPress blog from the command lineHow to post content to a WordPress blog from the command line

  • Arifin

    Hi Its really very helpful .. help me a lot … Works perfectly …. Thanks.

    I have a small Clarification … Is it possible to Do The “Schedule” Posting rather then the “draft” or “published” post?

    I Will be glad if u provide any help …

    Thanks Again for this rocking Article … It helps me a lot

  • umer singhera

    thanx a lot
    this code is so so so helping for the beginers of php

  • MatadorM

    I see you use “‘title’=>, ‘description’=>,”ok great now can anyone tell me where to find the rest of the fields… not the ones you have here but say for images. even a gallery and so on

  • dave

    Great post. Is there a way to use a different template for the post?

  • Felix Weber

    Hey,
    I now your post is really old.
    But I tried to implemnt your code! And its working very well.
    Except, that I cannot post any HTML links (<a href=).

    If I want i get an error saying "Not well formated".
    If I use htmlentities() the post goes online but the link does not show up!
    Any suggestions?

  • http://www.hurricanesoftwares.com Ashish

    It could be your server settings i.e. “When double_encode is turned off PHP will not
    encode existing html entities. The default is to convert everything.”

    If that’s not the case, you must be doing something wrong. Posting links was the primary reason why i wrote this code. This is something which is working fine on my end. Please test that $bodycontent is having values as it suppose to be

  • http://www.hurricanesoftwares.com Ashish

    Why not, you can use any template you want, just use different $body for different templates. Of course you have create them first.

  • Rajeev Ja

    Thank you so much. Extremely useful.

  • http://www.angrybirdsrio.biz angry birds rio

    thank you very much. I find it two today. hope you share more

  • http://twitter.com/Acantara_S_E_O Acantara

    Acantara ist eine Agentur für Online Marketing und Suchmaschinenoptimierung und betreut Klienten aus Deutschland, Österreich und der Schweiz. Unsere Mitarbeiter sind mit den unterschiedlichen Optionen zur erfolgreichen und professionellen Suchmaschinenoptimierung vertraut und konnten ihre Fähigkeiten bereits bei vielen Projekten erfolgreich unter Beweis stellen. Wir sind von unserem Ansatz und unserer Professionalität überzeugt, und kÖnnen Ihnen daher unsere Leistungen zur Suchmaschinenoptimierung Ihrer Domain ohne Einmalkosten auf erfolgsabhängiger Basis fair und transparent anbieten. Neben einer professionellen Suchmaschinenoptimierung kann Acantara Sie auch in anderen Bereichen des Online Marketings unterstützen, so z.B. bei Keyword Advertising / Suchmaschinenwerbung, Blogging, Social Media Marketing, Online PR Maßnahmen, Adwords Betreuung und –Optimierung etc.

  • http://www.sagive.co.il/ sagive

    I am getting a weird error even tough i know var are defined.. used the script as is..  “Notice: Undefined variable: encoding in C:wampwwwremoteWPwp-contentthemesremoteWPsubmitter.php on line 15″  – and the same for line 16 – the vars are $title and $keywords..

    They are being sent ok to the website i am publishing on but still get this error.. any ideas why ?

  • gabrielem

    Very good script!

    Thank you for sharing it, I’m working on something similar, but have difficulties in find how to delete post, can you give me a way to delete a post?

  • Terry

    Hi I use this code it’s work but when I use spin such as { xxxx | aaaa } . it can’t spun. it post all of { xxxx | aaaa } how can I do it

  • http://www.hurricanesoftwares.com Ashish

     Spin or no spin, whatever content you are posting back to wordpress via XMLRPC you should ensure you do alll the magic before you call
    $client->query(‘metaWeblog.newPost’, $params)

  • Dylan

    How would I create a GUI to be able to enter in the necessary information for the post, “title” “body” etc. and have it update and run the php file? Is this possible? It would be nice to be able to fill in the information without having to directly edit the code.

  • Rubert Fitz

     What you are asking is pretty basic stuff. You need to create forms take titles and other data, then validate and then push to wordpress via XML RPC.

    So, the code you see above should only be accessed when you have a form post, second everything is validated.

  • Dylan

    I figured it was pretty basic. I’m in a computer science program at school learning C#, php looks very similar, but I don’t know how to do this part of the process:
    “then validate and then push to wordpress via XML RPC.”

    Ideally, I’d like to make the form to enter in Title, Body, Keywords etc. on a WordPress page. I just don’t know how to pass those values to this php file.

  • Dylan

    Any idea where I could find some sample code to make this happen? I’ve been looking around but I can’t find anything to get me on the right track. I’m reading up some PHP tutorials so hopefully I’ll be able to write some code soon.

  • Ben

    This is a great! But how do you create more than 1 custom field?
    $customfields=array(‘key’=>’Author-bio’, ‘value’=>’Autor Bio Here’); // Insert your custom values like this in Key, Value formatThe syntax for a key=>value would be easy, but since it is key=>X, value=>Y if I try to add another key=>email, value=email address it only creates one custom field…Thanks!

  • Richard

     Since it’s an array i think you will be able to add more data to it i.e.
    $customfields=array('key'=>'Author-bio', 'value'=>'Autor Bio Here', 'key'=>'email', 'value'=>'emailaddress');

    Although, i haven’t given it a try but i think it should work.

  • Pingback: Sdílení jedné databáze « Fórum podpory WordPressu

  • Nasim

    what about feature image? how can I post feature image? thanks in advance.

  • Patti

    i am getting a 32300 transport error – could not open socket and i have no idea what is means…can you help me and tell me how to fix this error>
    thanks

  • http://www.hurricanesoftwares.com Ashish

    First of all upgrade your wordpress to the latest stable version. Then check the memory limit in your php.ini.

    I was able to fix this issue simply by allotting more memory via “memory_limit” in my php.ini file

  • Pingback: Carga de datos | Los Pérez

  • Pingback: Add a post to wordpress with PHP | digitalwhores

  • Pingback: Most efficient way to insert a post outside WordPress?