Home  >  Article  >  CMS Tutorial  >  Methods and precautions for establishing a wordpress content backup mirror site

Methods and precautions for establishing a wordpress content backup mirror site

藏色散人
藏色散人forward
2019-09-26 09:43:112218browse

The following column WordPress Tutorial will introduce to you the methods and precautions for establishing a WordPress content backup mirror site. I hope it will be helpful to friends in need!

Methods and precautions for establishing a wordpress content backup mirror site

As a shrimp-level webmaster, I am actually a rookie who doesn’t understand code. Since my site is a small water pipe host, and the stability is difficult to guarantee, in many visitors Under the suggestion, I also want to build a content mirror site so that when the host of the main site is maintained, there will be a backup site for visitors to visit.

At first I wanted to have a shared database that could be used by both sites, but after Baidu checked the information, I found that this seemed not applicable to virtual host website building.

Until you find the following code, you can realize that when the source site publishes an article, it will also be automatically published on the mirror site.

The first step is to create a php file named post.php in the root directory of the mirror site. Code content:

//The following is the code text...

<?php
//文章接收
define(&#39;WP_USE_THEMES&#39;, false);
require_once("wp-load.php");
$key=&#39;123456&#39;; //设置启动 API 的密钥
if($_POST[&#39;key&#39;]==$key){
$categorys=explode(&#39;,&#39;,$_POST[&#39;category&#39;]);
$category=array();
for($x=1;$x<count($categorys);$x++) {
$category[$x-1]=get_cat_ID($categorys[$x]);
}
$info = array(
&#39;post_title&#39; => $_POST[&#39;title&#39;],
&#39;post_content&#39; => $_POST[&#39;content&#39;],
&#39;post_status&#39; => &#39;publish&#39;,
&#39;post_author&#39; => 1, //发布文章的作者 ID,1 为管理员
&#39;post_date&#39; => $_POST[&#39;date&#39;],
&#39;tags_input&#39; => $_POST[&#39;tags&#39;],
&#39;post_category&#39; => $category,
&#39;post_type&#39; => $_POST[&#39;type&#39;]
);
wp_insert_post( $info );
}

The second step is to add the downloaded code before the last ?> in the functions.php file of the main website theme, set the key, and modify the API address.

//文章推送
add_action(&#39;publish_post&#39;, &#39;fanly_sync_post&#39;); //钩子,在文章发布时执行
function fanly_sync_post($post_ID) {
$key=&#39;123456&#39;; //输入你设置的密钥
$url=&#39;http://3838521.com/post.php&#39;;//API地址,就是接受数据的那个站点,修改为自己站点
$post_info = get_post($post_ID);
if ( $post_info->post_status == &#39;publish&#39; && $_POST[&#39;original_post_status&#39;] != &#39;publish&#39; ) {
$title=$_POST[&#39;post_title&#39;];
$content=$_POST[&#39;content&#39;];
$date=$_POST[&#39;aa&#39;].&#39;-&#39;.$_POST[&#39;mm&#39;].&#39;-&#39;.$_POST[&#39;jj&#39;].&#39; &#39;.$_POST[&#39;hh&#39;].&#39;:&#39;.$_POST[&#39;mn&#39;].&#39;:&#39;.$_POST[&#39;ss&#39;];
$category=&#39;&#39;;
for($x=1;$x<count($_POST[&#39;post_category&#39;]);$x++) {
$category.=&#39;,&#39;.get_cat_name($_POST[&#39;post_category&#39;][$x]);
}
$type=$_POST[&#39;post_type&#39;];
$tags=str_replace(&#39;、&#39;,&#39;,&#39;,$_POST[&#39;tax_input&#39;][&#39;post_tag&#39;]);
if($_POST[&#39;newtag&#39;][&#39;post_tag&#39;]){
$tags.=&#39;,&#39;.str_replace(&#39;、&#39;,&#39;,&#39;,$_POST[&#39;newtag&#39;][&#39;post_tag&#39;]);
}
$data = &#39;key=&#39;.$key.&#39;&title=&#39;.$title.&#39;&content=&#39;.$content.&#39;&date=&#39;.$date.&#39;&category=&#39;.$category.&#39;&type=&#39;.$type.&#39;&tags=&#39;.$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;
}
}

In this way, after an article is published on the main site, the mirror site will also publish an article, but there will also be some unexpected situations, such as not publishing immediately, but showing the plan , it will normally be published after a few minutes, but there may also be publication failures. You need to select the article that failed to be published in the background article management, change the status to published, and update.

Solution to some unexpected situations:

Problem 1. After the theme is upgraded, the functions.php code will be replaced. Content mirroring implemented using the above method requires modifying the functions.php code every time the theme is upgraded, which will cause trouble.

So there is the following solution, the code is as follows:

Copy the above code, it is best to use Notepad and other tools to save it as a php file, package it into a zip document, and upload it in the WordPress plug-in installation background , install and enable.

This way it exists in the form of a plug-in, and it will no longer have any impact after the theme is upgraded.

Question 2. Some theme editors support short codes such as password visibility, payment visibility, etc., but the short code is different in the editing mode and the output mode. The content on the mirror site will be in the output mode, which is possible. An exception will be output.

My solution is also to use a small plug-in to automatically modify these codes. The code is as follows:

<?php
/*
Plugin Name: 小插件
Description: 给主题添加点小功能
Author: 云落
*/
//内容文字替换
function wpdaxue_replace_text($text){
$replace = array(
// &#39;原始文字&#39; => &#39;替换为这些&#39;
&#39;\"20\"]&#39; => &#39;"20"]&#39;,
&#39;\"10\"]&#39; => &#39;"10"]&#39;,
&#39;\"50\"]&#39; => &#39;"50"]&#39;
);
$text = str_replace(array_keys($replace), $replace, $text);
return $text;
}
add_filter(&#39;the_content&#39;, &#39;wpdaxue_replace_text&#39;); //正文
add_filter(&#39;the_excerpt&#39;, &#39;wpdaxue_replace_text&#39;); //摘要
add_filter(&#39;comment_text&#39;, &#39;wpdaxue_replace_text&#39;); //评论
?>

In this article, the codes used are all provided by omnipotent bloggers. I just made an effective integration of them to achieve my needs. Thank you for sharing. The hard work of the bloggers!

The above is the detailed content of Methods and precautions for establishing a wordpress content backup mirror site. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:aliyun.com. If there is any infringement, please contact admin@php.cn delete