1

I have running a wordpress site on - lets say - "site.com" and another site "input.com" hosted in completely different locations. As the name suggests, "input.com" should be used as a input site with a very simple form (for example: title and content). The entered information should then be sent to "site.com" and posted as a blogpost, just like somebody entered the information using the wp-admin panel.

Usually I would use cURL for this, but since wp-admin panel is pretty complicated (login and stuff) I looked for other ways to do it. Unfortunatly the wordpress rest API seems to be limited to getting data from "site.com" but offers no option to send data to it. All other APIs I found did not work properly (or at all) because they were either outdated or badly documented.

Is there any simple way to do this without coding a cURL programm? The most promising thing I found so far is this: https://github.com/HarriBellThomas/Wordpress_PostController But I was not able to make it work yet. I would really appreciate your help!

Edit:

<?php

set_time_limit(0);
error_reporting(E_ALL);
ini_set('display_errors', 1);

include("./wp-includes/post.php");

// Create post object
$my_post = array(
  'post_title'    => "test",//wp_strip_all_tags( $_POST['post_title'] ),
  'post_content'  => "teeeeeeeessssssssssttttttttttt",// $_POST['post_content'],
  'post_status'   => 'publish',
  'post_author'   => 1,
  'post_category' => array( 1,2 )
);

// Insert the post into the database
wp_insert_post( $my_post );

?>
1

I finally managed to accomplish the task using XML-RPC as suggested by denis.stoyanov. If xmlrpc is installed on your Server simply use the wpPostXMLRPC function from stackoverflow. But keep in mind that the used xmlrpc_encode_request() is an experimental function you usually would try to avoid.

0

Setup a script on site.com to receive the data from input.com and use wp_insert_post() to create the post on site.com.

The title, content and other post details are sent using the following array structure:

$post = array(
  'ID'             => [ <post id> ] // Are you updating an existing post?
  'post_content'   => [ <string> ] // The full text of the post.
  'post_name'      => [ <string> ] // The name (slug) for your post
  'post_title'     => [ <string> ] // The title of your post.
  'post_status'    => [ 'draft' | 'publish' | 'pending'| 'future' | 'private' | custom registered status ] // Default 'draft'.
  'post_type'      => [ 'post' | 'page' | 'link' | 'nav_menu_item' | custom post type ] // Default 'post'.
  'post_author'    => [ <user ID> ] // The user ID number of the author. Default is the current user ID.
  'ping_status'    => [ 'closed' | 'open' ] // Pingbacks or trackbacks allowed. Default is the option 'default_ping_status'.
  'post_parent'    => [ <post ID> ] // Sets the parent of the new post, if any. Default 0.
  'menu_order'     => [ <order> ] // If new post is a page, sets the order in which it should appear in supported menus. Default 0.
  'to_ping'        => // Space or carriage return-separated list of URLs to ping. Default empty string.
  'pinged'         => // Space or carriage return-separated list of URLs that have been pinged. Default empty string.
  'post_password'  => [ <string> ] // Password for post, if any. Default empty string.
  'guid'           => // Skip this and let Wordpress handle it, usually.
  'post_content_filtered' => // Skip this and let Wordpress handle it, usually.
  'post_excerpt'   => [ <string> ] // For all your post excerpt needs.
  'post_date'      => [ Y-m-d H:i:s ] // The time post was made.
  'post_date_gmt'  => [ Y-m-d H:i:s ] // The time post was made, in GMT.
  'comment_status' => [ 'closed' | 'open' ] // Default is the option 'default_comment_status', or 'closed'.
  'post_category'  => [ array(<category id>, ...) ] // Default empty.
  'tags_input'     => [ '<tag>, <tag>, ...' | array ] // Default empty.
  'tax_input'      => [ array( <taxonomy> => <array | string>, <taxonomy_other> => <array | string> ) ] // For custom taxonomies. Default empty.
  'page_template'  => [ <string> ] // Requires name of template file, eg template.php. Default empty.
);  

Leave the ID blank to create a new post.

Detailed documentation on the WP Codex: https://codex.wordpress.org/Function_Reference/wp_insert_post

  • Hi, I am getting this error: Fatal error: Call to undefined function get_current_user_id() in wp-includes/post.php on line 2897 How to avoid that? – Julian Freyberg Jan 22 '16 at 17:08
  • Can you edit your question and post the code? I'll be on the road soon but I can try to take a look or another user might join in. – jdm2112 Jan 22 '16 at 17:22
  • Sure! I'm sorry it is not marked as code properly, but the way it is described above did not work or me. – Julian Freyberg Jan 22 '16 at 18:05
  • Looks like you are trying to use WP core functionality outside of WP which won't work. Try a copy of page.php, for example page-receive.php, and create an empty page with the slug 'receive'. Loading that page will use the code in that page template. You will be inside the WP context at that point. There is likely another or better way to accomplish your project as well. You might get better input with some of that info? – jdm2112 Jan 22 '16 at 19:05
  • how can i create a new post using Gallery format ? and upload images remotely ? thanks – AMB Jun 16 '17 at 5:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.