跳到主要内容

wordpress

两段代码使你的wordpress后台速度提升20倍!

总体思路:WP 访问后台时会请求更新和很多杂七杂八的 api,这些服务的服务器都在国外,这些功能在大陆一点作用也没有(用它官方的还不如你装个文派叶子),禁用掉可以极大提升站点速度

很多插件的禁用功能禁用的不彻底(文派叶子也是)用这两段代码可以完全禁用,并且不会对功能产生影响

第一段(加到主题的 function.php 中)

// Turn off updates completely
add_filter('automatic_updater_disabled', '__return_true');
// Disable the WordPress core auto update
add_filter('pre_site_transient_update_core', '__return_null');
add_filter('pre_site_transient_update_plugins', '__return_null');
add_filter('pre_site_transient_update_themes', '__return_null');
// disable sending alert updates email
add_filter( 'auto_core_update_send_email', '__return_false' );
// disable auto upadte translation
add_filter( 'auto_update_translation', '__return_false' );
// remove auto check wp/theme/plugins update
remove_action('init', 'wp_schedule_update_checks');
wp_clear_scheduled_hook('wp_version_check');
wp_clear_scheduled_hook('wp_update_plugins');
wp_clear_scheduled_hook('wp_update_themes');
// remove the timed checking for upadtes task
remove_action('admin_init', '_maybe_update_core');
remove_action('admin_init', '_maybe_update_plugins');
remove_action('admin_init', '_maybe_update_themes');

这个代码片段会有效地禁用 WordPress 中的各种自动更新,包括核心更新、主题和插件更新、翻译更新以及相关的电子邮件提醒。它还会移除定期检查更新的计划任务。

(代码解释来自 GPT4o)

这会禁用 wp 的自动更新和定期检查,避免时不时请求在国外的服务器进行更新,极大提高了后台速度

如果要更新手动去后台 “更新” 那一栏手动更新就好,也更自由

第二段(加到站点根目录中的 wp-config.php 中)

/*Disable the external connection*/
define('WP_HTTP_BLOCK_EXTERNAL', true);
/*allow the localhost connection*/
define('WP_ACCESSIBLE_HOSTS', 'localhost');
// Disable auto update
define( 'WP_AUTO_UPDATE_CORE', false );
// Disable auto insatll theme or plugins
define('DISALLOW_FILE_MODS', true);
//(注意!!这一条会禁用wp后台的文件主题编辑器)

这段代码禁用全部 wp 对外部的 HTTP 链接(防止请求 api)第二行放行了 localhost 本地的连接(外部数据库慎用)

三四行分别禁用了内核和插件的定期检查与自动更新

WordPress无需插件实现同步文章到其他WordPress站点

多个 WordPress 站点想要文章相同,可以使用本文的方法实现自动同步。

思路

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

实现

在负博客站点的根目录创建一个文件,命名为 xxxxxxxx-post.php,并设置用于启动 API 的 key,代码如下:

<?php
/*
文章发表后同步到另一个站点(接收)
*/
define('WP_USE_THEMES', false);
require_once("wp-load.php");
$key='xxxxxxxxxx'; //设置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 地址:

copy

/*

文章发表后同步到另一个站点(发送)

*/

add_action('publish_post', 'E_sync_post'); //钩子,在文章发布时执行

function E_sync_post($post_ID) {

$key='xxxxxxxxxxxxx'; //输入你设置的密钥

$url='http://xxxxxx/xxxxxxxxxxxx-post.php';//API地址(接受同步文章博客地址,例:xx表示为发布文章主博客,那填写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 代码,这会造成麻烦。 所以有如下解决办法,代码如下:

copy

<?php

//文章推送

add_action('publish_post', 'fanly_sync_post'); //钩子,在文章发布时执行

function fanly_sync_post($post_ID)

{

$key = 'xxxx'; //输入你设置的密钥

$url = 'http://xxxx/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;
}
}
?>

复制上面的代码,最好是用 Notepad ++ 等工具另存为 php 文件,打包成 zip 文档,在 wordpress 插件安装后台上传,安装并启用。 这样就是一个插件形式存在了,主题升级后不再有影响。

问题 2:

有些主题编辑器是支持密码可见付费可见等短代码的,但短代码在编辑模式跟输出模式是不一样的,到了镜像站的内容会是输出模式,有可能会输出异常。

我的解决办法也是采用小插件的办法,对这些代码进行一个自动修改。代码如下:

copy

<?php
//内容文字替换
function wpdaxue_replace_text($text)
{
$replace = array(
// '原始文字' => '替换为这些'
);
$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'); //评论
?>

本文來自 WordPress 无需插件实现同步文章到其他 WordPress 站点