DISCLAIMER: this post is older than one year and may not be up to date with latest WordPress version.

Who said you have to be logged on your WordPress dashboard to publish a new post on your blog? PHP and cURL can do it easily for you. Let see how.

Here is the function. This code is not made for being used within WordPress, so don’t paste it on your functions.php file (or any other).

Please note that you must activate the XMLRPC posting option in your WordPress blog. If this option isn’t activated, the code will not be able to insert anything into your blog database. Another thing, make sure the XMLRPC functions are activated on your php.ini file.

function wpPostXMLRPC($title,$body,$rpcurl,$username,$password,$category,$keywords='',$encoding='UTF-8') {
    $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'=>array($category)
    );
    $params = array(0,$username,$password,$content,true);
    $request = xmlrpc_encode_request('metaWeblog.newPost',$params);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
    curl_setopt($ch, CURLOPT_URL, $rpcurl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 1);
    $results = curl_exec($ch);
    curl_close($ch);
    return $results;
?>

If you’re interested in that kind of stuff (PHP/cURL) you should definitely have a look at the 10 awesome things to do with cURL list I published two days ago on my other blog Cats Who Code.

Credits : http://porn-sex-viagra-casino-spam.com/coding/poster-automatiquement-sur-wordpress-avec-php/

Tags

8 Comments

  1. Is there a way to add onto this function to set a specific date when you want the post to be posted?

    Thanks

  2. this is not working for me !

  3. Nikita Mishra

    not working for me too 🙁

  4. Pawan Mishra

    not working for me too

  5. it is working for me, dont forget to set the correct rcpurl which is http://your_blog/xmlrpc.php

  6. Can you anyone give resource to create posting on wordpress.com blog… by suing oauth or anything method…

    Thanks

  7. hello Guys… can You give me another method??

  8. Good news…it appears that XML-RPC is enabled by default for WordPress 3.5 and on. So no need for users to take an extra step.

    http://codex.wordpress.org/XML-RPC_Support

Leave a Comment

Your email address will not be published. Required fields are marked *