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
    3. I will be honest – I ditched ClickBank and AdSense altogether from my blog….reason being there are too many merchants who’s products and services either get banned or leave ClickBank and as for AdSense, it gives visitors to your website another route to click and leave your site. If you have affiliate programs and products already on your site, you are guaranteed to make more money with them than through AdSense, unless your are driving some serious traffic. As for InfoLinks, it just looks spammy and the keywords and ads that appear are not targeted most the time. I personally say just do away with ads and ad networks and focus on affiliate programs to monetize your blog that have one or few solid products to offer, because like I mentioned, you will make more money that way.regreds:bestbitcointumblers.com


      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
  4. I am getting the below error even i am trying it by admin
    "error":"You need to login with a user capable of creating posts."}

    ReplyDelete
    Replies
    1. The JSON API plugin hasn’t been tested with the latest 3 major releases of WordPress. It may no longer be maintained or supported and may have compatibility issues when used with more recent versions of WordPress.

      Delete
    2. might be.
      i am using latest 4.9.4
      on which version it is working perfect.

      Delete
    3. The plugin was tested up to:4.3.15

      Delete
  5. "you are right after several time of adsense application rejection many people think that “Ohhh, now i can’t be able to do this” and many of them give up from blogging but it shouldn’t be.

    Google checks the passion and consistency of Bloggers so, we no need to worry about my adsense application is not being accepted. I think 3 or 4 time rejection is normal thing in these days.

    Thanks Bro for sharing your experience with us. Newbies will learn lot’s of things from here."

    ReplyDelete

Post a Comment

Popular posts from this blog

Top Google Adsense Alternatives

Google Adsense is a web tool that allows publishers in the Google Network of content sites to automatically serve text, image, video, and rich media adverts that are targeted to site content and audience. These adverts are administered, sorted, and maintained by Google, and they can generate revenue on either a per-click or per-impression basis.  Google servers advertisers using google adwords platform, while adsense is the publishers platform. Google Adsense is the top Ad Publishers platform over the web ranking number one in web advertising industry. Adsense offers contextual advertisements that covers web sites, blogs, games, videos, mobile browsing etc. What made Google Adsense no. 1 is the reliability, stability, variety of services and large number of publishers including google it self. Also google has a fair platform that detects invalid clicks so google successfully protects its advertisers and also offers its best publishers top CPC. Two reasons are behind people think

The Semantic Web

Semantic Web aims to create a meaning and define inter-relationship for information available on the web In the early stages of the World Wide Web (web) it was necessary to develop standards to view web content (HTML language) and to create communication channels (N-Tier applications, email, ftp, etc.). As the web started to be the world’s largest knowledge base, accessible world wide, it became important to develop tools to transfer knowledge between cultures. However, it is still not possible for applications and agents to interoperate with other applications and agents without having a predefined, human created common framework of the meaning of the information being transferred on both sides. Semantic Web (SW) alleviates this problem by providing a common framework that allows data to be shared and reused across application, enterprise, and community boundaries [W3C Semantic Web, 2019]. A clear example on SW application is schema.org. Google, Bing, Yahoo use schema

Arabs In London

I was not able to watch Arabs in London episodes during the last holy month of Ramadan. I watched it recently on MBC. I was really impressed by the performance of some actors and actresses. The most impressive one was Mais Hamdan who was able to present the Palestinian accent efficiently. This TV series was written and directed by Anwar Qawadri a Syrian director who studied and lived in London. The below video is about 8 minutes show when Sami finds out that his friend Eddy (Adnan) passed away.