多个wordpress 站点之间同步发送文章

思路:

在另一个WordPress站点创建一个API,文章发布时用cURL模拟POST请求API利用wp_insert_post()函数来创建文章。支持同步文章标题、内容、类型、分类、标签,分类需要另一个站点也有创建相同名称的分类,别名和 ID 不需要相同。

接收端:

在另一个站点的根目录创建一个命名为 post.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 _POST['title'],

       'post_content' => base64_decode(str_replace(" ","+",_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=base64_encode(_POST['content']);date=_POST['aa'].'-'._POST['mm'].'-'._POST['jj'].' '._POST['hh'].':'._POST['mn'].':'._POST['ss'];

        category='';

        for(x=1;$x=3){
						return false;
					 }
					 pic += (pic?'||':'') + encodeURIComponent(a.src); 
				});
				
				summary = encodeURIComponent(summary.innerText.replace(/\r|\n|\t/g,'').replace(/ +/g,' ').replace(//g,'').substr(0,80));
			}
						var link =typeof settings.link!='undefined'?settings.link:'';
			if(!link||link.length<=0){return;}
			if(!/summary/.test(link) && summary) {
				title = title + ': ' + summary + '.. ';
			}
			
			link = link.replace("{url}",url).replace("{title}",title).replace("{summary}",summary).replace("{img}",pic);
			var iWidth=typeof settings.width!='undefined'?settings.width: 450;
			var	iHeight=typeof settings.height!='undefined'?settings.height: 450;
			var iTop = (window.screen.height-30-iHeight)/2; //获得窗口的垂直位置;
			var iLeft = (window.screen.width-10-iWidth)/2; //获得窗口的水平位置;
			window.open(link, 'share', 'height='+iHeight+',innerHeight='+iHeight+',width='+iWidth+',innerWidth='+iWidth+',top='+iTop+',left='+iLeft+',menubar=0,scrollbars=1,resizable=1,status=1,titlebar=0,toolbar=0,location=1');
		}
	}

Bookmark the permalink.
0 0 投票数
文章评分
订阅评论
提醒
guest

0 评论
内联反馈
查看所有评论