Forum Replies Created

Viewing 1 replies (of 1 total)
  • Thread Starter fmdfrank

    (@fmdfrank)

    I relentlessly searched and read until I found an answer! Turns out, the right thing to use is actually MetaWeblog API and XMLRPC, which you’ll have to activate as it is disabled by default.
    First thing, visit the Settings menu in your admin panel and navigate to “Writing”. Scroll down until you see the XML-RPC check box under “Remote Publishing”. Once this is saved your pretty much all set.

    You’ll have to export posts and then read through that xml file to get the meta-names of your specific custom fields.
    Here’s the code I used:

    <?php
     $BLOGURL = "your.wordpress.root/folder";
        $USERNAME = "your_uesername";
        $PASSWORD = "your_password";
    
        function get_response($URL, $context) {
        if(!function_exists('curl_init')) {
        die ("Curl PHP package not installed\n");
        }
    
        /*Initializing CURL*/
        $curlHandle = curl_init();
    
        /*The URL to be downloaded is set*/
        curl_setopt($curlHandle, CURLOPT_URL, $URL);
        curl_setopt($curlHandle, CURLOPT_HEADER, false);
        curl_setopt($curlHandle, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));
        curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $context);
    
        /*Now execute the CURL, download the URL specified*/
        $response = curl_exec($curlHandle);
        return $response;
        }
    
        /*Creating the metaWeblog.newPost request which takes on five parameters
        blogid,
        username,
        password*/
    
        /*The title of your post*/
        $title = "Sample Post Title with Custom Fields";
    
        /*The contents of your post*/
        $description = "a collection lorem ipsums";
    
        /*Forming the content of blog post*/
        $content['title'] = $title;
        $content['description'] = $description;
        $content['categories'] = array("Uncategorized");
        /*Pass custom fields*/
        $content['custom_fields'] = array(
        	array( 'key' => 'your_custom_feild_meta-key', 'value' => 'place whatever value your custom field requires here' )
        	);
        /*Whether the post has to be published, false means it will be created as a draft*/
        $toPublish = false;
        $request = xmlrpc_encode_request("metaWeblog.newPost",
        array(1,$USERNAME, $PASSWORD, $content, $toPublish));
    
        /*Making the request to wordpress XMLRPC of your blog, you may have to change this to the correct path for your xmlrpc.php file*/
        $xmlresponse = get_response($BLOGURL."/xmlrpc.php", $request);
        $response = xmlrpc_decode($xmlresponse);
    
       /*Printing the response on to the console*/
       /*If it works, you'll see a number followed by :Post ID*/
       	echo ":Post ID"; print_r($response);
        echo "\n";
       ?>

    This is it! Now you can easily automate the creation of this file with some php, javascript, perl… whatever you want! Theres only a few variables to change.

Viewing 1 replies (of 1 total)