我需要在不同的网站(网站2)中插入帖子,同时在网站1中插入帖子。我使用的是xmlrcp api。 xmlrcp正在运行。
但是当我尝试使用wp_insert_post动作挂钩在远程服务器中插入帖子时,帖子插入了4,5次。
我在远程服务器中插入相同帖子时遇到问题。谢谢大家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' );
?>