在另一个站点的根目录创建一个命名为 post.php 的 php 文件,代码如下:
//以下为代码正文…<?php//文章接收define('wp_use_themes', false);require_once("wp-load.php");$key='123456'; //设置启动api的密钥if($_post['key']==$key){ $categorys=explode(',',$_post['category']); $category=array(); for($x=1;$x<count($categorys);$x++) { $category[$x-1]=get_cat_id($categorys[$x]); } $info = array( 'post_title' => $_post['title'], 'post_content' => $_post['content'], 'post_status' => 'publish', 'post_author' => 1, //发布文章的作者id,1 为管理员 'post_date' => $_post['date'], 'tags_input' => $_post['tags'], 'post_category' => $category, 'post_type' => $_post['type'] ); wp_insert_post( $info ); }
然后在主题的 functions.php 文件的最后一个?>前加入已下代码,并设置 key,修改 api 地址
//文章推送add_action('publish_post', 'fanly_sync_post'); //钩子,在文章发布时执行 function fanly_sync_post($post_id) { $key='www.exiang2.com'; //输入你设置的密钥 $url='http://www.domain.com/post.php';//api地址,就是接受数据的那个站点 $post_info = get_post($post_id); if ( $post_info->post_status == 'publish' && $_post['original_post_status'] != 'publish' ) { $title=$_post['post_title']; $content=$_post['content']; $date=$_post['aa'].'-'.$_post['mm'].'-'.$_post['jj'].' '.$_post['hh'].':'.$_post['mn'].':'.$_post['ss']; $category=''; for($x=1;$x<count($_post['post_category']);$x++) { $category.=','.get_cat_name($_post['post_category'][$x]); } $type=$_post['post_type']; $tags=str_replace('、',',',$_post['tax_input']['post_tag']); if($_post['newtag']['post_tag']){ $tags.=','.str_replace('、',',',$_post['newtag']['post_tag']); } $data = 'key='.$key.'&title='.$title.'&content='.$content.'&date='.$date.'&category='.$category.'&type='.$type.'&tags='.$tags; $ch = curl_init (); //curl模拟post curl_setopt ( $ch, curlopt_returntransfer, true ); curl_setopt ( $ch, curlopt_post, true ); curl_setopt ( $ch, curlopt_postfields, $data ); curl_setopt ( $ch, curlopt_url, $url ); curl_setopt ( $ch, curlopt_ssl_verifypeer, false); $ret = curl_exec ( $ch ); curl_close ( $ch ); return $ret; } }
更多wordpress相关技术文章,请访问wordpress教程栏目进行学习!
以上就是wordpress多站点设置同步文章的详细内容。
