


Methods and precautions for establishing a wordpress content backup mirror site
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!
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('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 ); }
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('publish_post', 'fanly_sync_post'); //钩子,在文章发布时执行 function fanly_sync_post($post_ID) { $key='123456'; //输入你设置的密钥 $url='http://3838521.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; } }
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( // '原始文字' => '替换为这些' '\"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'); //评论 ?>
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!

Enable comments on your WordPress website to provide visitors with a platform to participate in discussions and share feedback. To do this, follow these steps: Enable Comments: In the dashboard, navigate to Settings > Discussions, and select the Allow Comments check box. Create a comment form: In the editor, click Add Block and search for the Comments block to add it to the content. Custom Comment Form: Customize comment blocks by setting titles, labels, placeholders, and button text. Save changes: Click Update to save the comment box and add it to the page or article.

How to copy WordPress subsites? Steps: Create a sub-site in the main site. Cloning the sub-site in the main site. Import the clone into the target location. Update the domain name (optional). Separate plugins and themes.

The steps to create a custom header in WordPress are as follows: Edit the theme file "header.php". Add your website name and description. Create a navigation menu. Add a search bar. Save changes and view your custom header.

Enable comments in WordPress website: 1. Log in to the admin panel, go to "Settings" - "Discussions", and check "Allow comments"; 2. Select a location to display comments; 3. Customize comments; 4. Manage comments, approve, reject or delete; 5. Use <?php comments_template(); ?> tags to display comments; 6. Enable nested comments; 7. Adjust comment shape; 8. Use plugins and verification codes to prevent spam comments; 9. Encourage users to use Gravatar avatar; 10. Create comments to refer to

You can install the FTP plug-in through WordPress, configure the FTP connection, and then upload the source code using the file manager. The steps include: installing the FTP plug-in, configuring the connection, browsing the upload location, uploading files, and checking that the upload is successful.

How to copy WordPress code? Copy from the admin interface: Log in to the WordPress website, navigate to the destination, select the code and press Ctrl C (Windows)/Command C (Mac) to copy the code. Copy from a file: Connect to the server using SSH or FTP, navigate to the theme or plug-in file, select the code and press Ctrl C (Windows)/Command C (Mac) to copy the code.

WordPress Error Resolution Guide: 500 Internal Server Error: Disable the plug-in or check the server error log. 404 Page not found: Check permalink and make sure the page link is correct. White Screen of Death: Increase the server PHP memory limit. Database connection error: Check the database server status and WordPress configuration. Other tips: enable debug mode, check error logs, and seek support. Prevent errors: regularly update WordPress, install only necessary plugins, regularly back up your website, and optimize website performance.

How to turn off a comment in WordPress? Specific article or page: Uncheck Allow comments under Discussion in the editor. Whole website: Uncheck "Allow comments" in "Settings" -> "Discussion". Using plug-ins: Install plug-ins such as Disable Comments to disable comments. Edit the topic file: Remove the comment form by editing the comments.php file. Custom code: Use the add_filter() function to disable comments.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version
Recommended: Win version, supports code prompts!