Skip to main content

Publish to Wordpress using JSON API plugin and Coldfusion

I was using Postie to automatically publish to wordpress blog. I was searching a better method of publishing using soap api or json api. I found an excellent json plugin that can be used to publish posts and comments to wordpress.
Please follow the below step by step, this code will let you publish automatically for froma different interface to wordpress rather than using postie or email.

Step one: Install json plugin, enable the plugin and then go to settings > JSON API and  activate post

Step two: you have to modify the file: yourwordpressdirectory/wp-content/plugins/json-api/controllers/post.php

Please replace the file content with the below or add the missing Authenticate method. This method is necessary to authenticate a user to wordpress to be able to post content without a valid session.


<?php
/*
Controller name: Posts
Controller description: Data manipulation methods for posts
*/
class JSON_API_Posts_Controller {
  public function create_post() {
    global $json_api;
    $this->authenticate();
    if (!$json_api->query->nonce) {
      $json_api->error("You must include a 'nonce' value to create posts. Use the `get_nonce` Core API method.");
    }
    $nonce_id = $json_api->get_nonce_id('posts', 'create_post');
    if (!wp_verify_nonce($json_api->query->nonce, $nonce_id)) {
      $json_api->error("Your 'nonce' value was incorrect. Use the 'get_nonce' API method.");
    }
    nocache_headers();
    $post = new JSON_API_Post();
    $id = $post->create($_REQUEST);
    if (empty($id)) {
      $json_api->error("Could not create post.");
    }
    return array(
      'post' => $post
    );
  }
  
  /**
* Attempts to authenticate user if author and user_password fields exist
*
* @return void
* @author Achillefs Charmpilas
*/
  private function authenticate() {
    global $json_api;
    if ($json_api->query->author && $json_api->query->user_password) {
      $user = wp_signon(array('user_login' => $json_api->query->author, 'user_password' => $json_api->query->user_password));
      if (get_class($user) == 'WP_Error') {
        $json_api->error($user->errors);
      } else {
        if (!user_can($user->ID,'edit_posts')) {
          $json_api->error("You need to login with a user capable of creating posts.");
        }
      }
    } else {
      if (!current_user_can('edit_posts')) {
        $json_api->error("You need to login with a user capable of creating posts.");
      }
    }
  }
}
?>
Step three: Create a component (Class) publish.cfc first you need to request nonce code this is returned using the first cfhttp call and then I pass it to the create_post method of the json-api. The code below is very clear and self explanatory


<cfcomponent output="false">
 <cfhttp url="http://yourwordpressurl/?json=get_nonce&controller=posts&method=create_post" result="nonce" />
  <cffunction name="createPost" access="public" output="true" returntype="any" hint="Post">
      <cfargument name="status" required="true" type="string">
      <cfargument name="title" required="true" type="string">
      <cfargument name="content" required="true" type="string">
      <cfargument name="author" required="true" type="string">
      <cfargument name="user_password" required="true" type="string">
      <cfargument name="categories" required="true" type="string">
      <cfargument name="tags" required="true" type="string">
      <cfhttp url="http:// yourwordpressurl/api/posts/create_post/" result="addpost">
      <cfhttpparam name="nonce" type="URL" encoded="yes" value="#DeserializeJSON(nonce.filecontent).nonce#">
      <cfhttpparam name="status" type="URL" encoded="yes" value="#status#">
      <cfhttpparam name="title" type="URL" encoded="yes" value="#title#">
      <cfhttpparam name="content" type="URL" encoded="yes" value="#content#">
      <cfhttpparam name="author" type="URL" encoded="yes" value="#author#">
      <cfhttpparam name="user_password" type="URL" encoded="yes" value="#user_password#">
      <cfhttpparam name="categories" type="URL" encoded="yes" value="#categories#">
      <cfhttpparam name="tags" type="URL" encoded="yes" value="#tags#">
      </cfhttp>
      <cfreturn addpost />
  </cffunction>
</cfcomponent>

Step Four: Now in your cfm page something like post.cfm you invoke the class as below:

<cfset postCFC = createObject("component", "publish")>

Now call the method and pass the content you want to publish:

<cfset postCFC.createPost("Publish","#title#","#content1#","username","password","#category#","#tags#")>

The create_post method attributes above are as below:

1- Post status (Draft, Published)
2- Post Title
3- Post Content
4,5- Username and password for a user who has authority to publish
6- list of categories
7- list of tags

Enjoy auto posting and or a new posting interface to wordpress. If you have any question please comment to the post.

Copying the content and posting it in another blog is strictly prohibited.


Comments

  1. Hi there. I am using your article successfully to post to my blog but it seems to stop working after the content variable gets over about 6000 characters. I am not sure if it's a limit with the CFC or with Wordpress.

    Can you help on this?

    ReplyDelete
    Replies
    1. This issue definitely is not caused by coldfusion, you have to check the json plugin. Send them an email and let me know the respond.

      Delete
    2. I agree from what I have tested. It is a limit on the json plugin. I'll put in a support request today and get back to you on it. Thanks

      Delete
  2. Hello! I found this page very informative, but can't seem to get it to work ... I'm wondering if there's something in the later versions of WP that prevents it from posting ...

    when I manually go to the first url (http://?????.com/blog/?json=get_nonce&controller=posts&method=create_post) I get the response:
    {"status":"ok","controller":"posts","method":"create_post","nonce":"e9c1e6b6fd"}

    and the second url in the cfc (http://?????.com/blog/api/create_post/) brings me to the main blog page ...

    I'm not sure if you're still supporting this code, but, if you are, would appreciate some advice! Thanks!

    ReplyDelete
  3. hello,
    i want to know that this plugin is require for third party api integration?

    ReplyDelete
    Replies
    1. In this specific case, I had to use this plugin.

      Delete

Post a Comment

Popular posts from this blog

Error: Write to Disk Access Denied - Troubleshooting - BitTorrent

I have downloaded Bit Torrent software and when trying to download I got an error after few seconds saying:

Error: Write to Disk Access Denied
Solving this problem is so simple:
Shut down BitTorrent program.
Go to Start and in the small search box on top of windows start button start typing Bittorrent and the program will show, right click with the mouse on the icon and Run as Administrator.
All ur problems are sorted out now and you can enjoy downloading...
Good Luck.

Importing SSL certs to Coldfusion Railo's keystore

If you are having the below error:
Railo 3.3.4.003 Error (javax.net.ssl.SSLHandshakeException)Messagesun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested targetCausejavax.net.ssl.SSLHandshakeException

This means you are trying to invoke an https webservice. to invoke an SSL webserver you need to import the certificate into Railo by using keytool command. Below are the steps on how to do this:

1- use fire fox to open the webservice and click on the padlock as shown on the below image:

2-Click on more information as below:
 3- Click on View Certificate as below:
4-Click on details as below:
 5- Now export the certificate and save it to your computer with .cer extension.

6- Search in railo folder for the keytool command location, in my case it was under railo\jdk\bin

7-  Search in railo folder for the cacerts location, in my case it was under railo\lib\railo-s…