2

I require to insert a post in different website (website 2) while insert a post in website 1. I am using the xmlrcp api. The xmlrcp is working.

But while i try to insert the post in remote server using the wp_insert_post action hook, the post is inserted 4,5 times.

I am having problem with inserting the same post in remote server. Thank you all xml-rcp wordpress hook

 <?php 
 function post_blog_to_another_site( $post_id ) {

include_once( ABSPATH . WPINC . '/class-IXR.php' );
include_once( ABSPATH . WPINC . '/class-wp-http-ixr-client.php' );

$usr = 'admin';
$pwd = 'password';
$xmlrpc = 'http://upper.dev/wordpressdemo/xmlrpc.php';
$client = new IXR_Client($xmlrpc);

$post_thumbnail_id = get_post_thumbnail_id( $post_id );
$fullsize_path = get_attached_file( $post_thumbnail_id ); 



////////////  IMAGE UPLOAD AND ATTACHMENT POST CREATION  ///////////


$img_attach = $fullsize_path; 
$img_attach_content = array(
        'name' => basename($img_attach),
        'type' => mime_content_type($img_attach),
        'bits' => new IXR_Base64(file_get_contents($img_attach)),
                );
$status = $client->query( 'wp.uploadFile','1',  $usr, $pwd, $img_attach_content );
$image_returnInfo = $client ->getResponse();

    ////////////  POST CREATION  ///////////


$post_content = array(
    'post_type' => 'post',
    'post_status' => 'draft', //for now
    'post_title' => $post_title,
    //'post_author' => 3,
    'post_name' => $post_title,
    'post_content' => 'Thats Used to Losing Love by Joey Yung. In one of the citys many karaoke centres, you will hear people crooning about love, anger, making money - as well as lots and lots and lots of heartbreak.',

    'post_thumbnail' => $image_returnInfo['id']

);

$res = $client -> query('wp.newPost',1, $usr, $pwd, $post_content);
$postID =  $client->getResponse();
if(!$res)
    echo 'Something went wrong....';
else {
        echo 'The Project Created Successfully('.$res.')<br>Post ID is '.$postID.'<br>';
}




    ////////////  Image Post Attachment Edit  ///////////
    $img_attach_content2 = array(
    'post_type'  => 'attachment',   
    'post_status' => 'inherit', 
    'post_title' => $postID, 
    'post_name' => $postID, 
    'post_parent'  => $postID,
    'guid'    => $image_returnInfo['url'],
    'post_content'   => '',
    'post_mime_type' => 'image/jpg'
     );

     $res2 = $client -> query('wp.editPost', 0, $usr, $pwd,    $image_returnInfo['id'], $img_attach_content2);

     $postIDimg =  $client->getResponse(); 




      ////////////   POST EDIT  ///////////


     $post_content2 = array(
       'post_status' => 'publish'  //publish
      );
     $media2= $client->query('wp.editPost',0, $usr, $pwd, $postID,  $post_content2);


  }
  add_action( 'wp_insert_post', 'post_blog_to_another_site'  );
  ?>
Rocky Prajapati
  • 187
  • 1
  • 15

0 Answers0