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.
<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.
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.
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<?php/*Controller name: PostsController 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.");}}}}?>
<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.
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.
ReplyDeleteCan you help on this?
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.
DeleteI 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
DeleteI 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
DeleteHello! 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 ...
ReplyDeletewhen 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!
hello,
ReplyDeletei want to know that this plugin is require for third party api integration?
In this specific case, I had to use this plugin.
DeleteI am getting the below error even i am trying it by admin
ReplyDelete"error":"You need to login with a user capable of creating posts."}
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.
Deletemight be.
Deletei am using latest 4.9.4
on which version it is working perfect.
The plugin was tested up to:4.3.15
Delete"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.
ReplyDeleteGoogle 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."