1

I would like to programmatically create posts of a custom post type with custom taxonomies over the XML-RPC API. This is what I have so far:

<?php

class XMLRPClientWordPress {

    var $XMLRPCURL = "";
    var $UserName = "";
    var $PassWord = "";

    // constructor
    public function __construct($xmlrpcurl, $username, $password) {
        $this->XMLRPCURL = $xmlrpcurl;
        $this->UserName = $username;
        $this->PassWord = $password;
    }

    function send_request($requestname, $params) {
        $request = xmlrpc_encode_request($requestname, $params);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
        curl_setopt($ch, CURLOPT_URL, $this->XMLRPCURL);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 1);
        $results = curl_exec($ch);
        curl_close($ch);
        return $results;
    }
    function create_post($title, $body, $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, $this->UserName, $this->PassWord, $content, true);

        return $this->send_request('metaWeblog.newPost', $params);
    }

}

$objXMLRPClientWordPress = new XMLRPClientWordPress("localhost/wordpress/xmlrpc.php", "admin", "abbas");

echo '<tr>';
echo '<td>metaWeblog.newPost</td>';
echo '<td>' . $objXMLRPClientWordPress->create_post('Hello WordPress XML-RPC', 'This is the content of post done via XML-RPC', '') . '</td>';
echo '</tr>';
?>
  • What you have tried so far? – Rahil Wazir Dec 31 '13 at 8:06
  • im trying this using xmlrpc. – fazalerabbi Dec 31 '13 at 9:29
  • Its ok. But show us your some code where the problem occurs. – Rahil Wazir Dec 31 '13 at 10:28
  • What code are you using to register your CPT and custom taxonomy type? – Brad Dalton Dec 31 '13 at 10:45
  • @RahilWazir Im using obove code for posting,its works for posting 'Post', now by using this i want to post my custom post type. Hope fully this helps you to answer me. – fazalerabbi Dec 31 '13 at 11:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.