简介
WordPress可以利用自建API在不同站点间自动同步发布文章,支持同步主站的文章标题、内容、类型、分类、标签等到备站,分类需要在备站也有创建相同名称的分类。
操作
- 在镜像站根目录下创建
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<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='123456'; //输入你设置的密钥
$url='http://www.ensu.cc/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;
}
}
这样一来,在主站发表一篇文章后,镜像站点也就会发表出来一篇文章了,但也会有一些意外情况,比如不是马上发表出来,而是显示计划中,正常隔几分钟后会发表好,但也会有发表失败,需要在后台文章管理中,选择该发表失败文章,状态修改为已发布,更新即可。
**一些意外情况的解决:
问题 1:由于主题升级后,functions.php 代码会被置换。**
用以上方法实现的内容镜像每次在主题升级后都需要修改 functions.php 代码,这会造成麻烦。 所以有如下解决办法,复制下面的代码,最好是用 Notepad ++ 等工具另存为 php 文件,打包成 zip 文档,在 wordpress 插件安装后台上传,安装并启用。代码如下:
<?php
/*
Plugin Name: 小插件
Description: 给主题添加点小功能
Author: 云落
*/
//文章推送
add_action('publish_post', 'fanly_sync_post'); //钩子,在文章发布时执行
function fanly_sync_post($post_ID) {
$key='123456'; //输入你设置的密钥
$url='http://www.ensu.cc/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;
}
}
?>
这样就是一个插件形式存在了,主题升级后不再有影响。
问题 2:有些主题编辑器是支持密码可见付费可见等短代码的,但短代码在编辑模式跟输出模式是不一样的,到了镜像站的内容会是输出模式,有可能会输出异常。
复制下面的代码,最好是用 Notepad ++ 等工具另存为 php 文件,打包成 zip 文档,在 wordpress 插件安装后台上传,安装并启用。代码如下:
<?php
/*
Plugin Name: 小插件
Description: 给主题添加点小功能
Author: 云落
*/
//内容文字替换
function wpdaxue_replace_text($text){
$replace = array(
// '原始文字' => '替换为这些'
'\"20\"]' => '"20"]',
'\"10\"]' => '"10"]',
'\"50\"]' => '"50"]'
);
$text = str_replace(array_keys($replace), $replace, $text);
return $text;
}
add_filter('the_content', 'wpdaxue_replace_text'); //正文
add_filter('the_excerpt', 'wpdaxue_replace_text'); //摘要
add_filter('comment_text', 'wpdaxue_replace_text'); //评论
?>