Create post using JSON api plugin

I am working on a project where the user writes their content on one wordpress blog (as a draft) but wants it published on their own wordpress blogging site.

This seems strange but it is the core concept for the business as the writers are collaborative and so that’s now what can be changed.

I’ve installed JSON api plugin. (I am totally new at api’s and still in the first year of PHP).

I’m wondering 1. How you would use the json info from site A to create a post for site B.

So far I’ve conceptualized it as the following.

  1. the plugin(on site b) pulls the json info from site a
  2. the plugin then takes the json and turns it into an array (attempting to match as much of it to post info)
  3. then the plugin inserts the posts into site b’s post db (I thought this would make it the fastest as this is something that twitter kindof does except for the data sits on your phone/computer??)
  4. The posts function as if they were created on site b (i.e. they follow the rules of the theme on site b).

So far EVERY attempt I’ve done to google and do it alone has resulted in horrible, horrible failure. (I have not fallen on my sword yet due to my high need of achievement.)

The first attempt was:

$response = wp_remote_get('urlhere/?json=get_post&post_id=120');
//$my_post = array($keys as $values);

wp_insert_post($response, $wp_error);

However… no results (I put urlhere to create a more universal response and not just focus on the url of the actual site.

// Create post object
$my_post_test = array(
  'post_title'    => "Hi this is a title!" ,
  'post_content'  => "This is a test",
  'post_status'   => 'publish',
  'post_author'   => 1,
  //'post_category' => array( 8,39 )
);

// Insert the post into the database
wp_insert_post( $my_post_test, $wp_error );

Second issue: This code produces 4-8 posts with the same title (seems strange?)

My questions are:
1. What is the best way to conceptualize the process so it’s relatively scalable (i.e. 20-1,000 writers)
2. How would I get JSON api plugin to insert the data into a wordpress post code?

1 Answer
1

I’ve worked on JSON API for a iPhone app development for a WordPress site to post photos but not using JSON API Plugin,

But the basic procedure would be: As you need to be logged in to publish your post, the communication is going to be two way
1. Author writes a article as draft on site A.
2. There is some link or button to initiate a JSON request to login to site B, you get a token if log in was successful.
3. You send another JSON request if you get token, with token and post data to site B.
4. If all the data is valid , site B publishes the Post and return you the POST ID
5. You can store the post ids for the record.

This would require you to check proper security. Also site b can’t automatically pull a new post from site a as you said in your first point, because something needs to tell it when to pull the info.

There might be a better approach I don’t know but you’ll require to make a custom plugin for that.

Leave a Comment