上次发帖的时候,插件还没有在线更新功能,每次更新插件都得打包,挨个发给别人很麻烦,于是我就在最新的版本添加了在线更新的功能。也修了几个bug,添加了一些网友要的新功能。
![]()
![]()
![]()
这个插件其实很多功能子比主题都是自带的,比如说用户头像,文章缩略图,随机浏览量,上传重命名等。这个插件最早是给我博客写的,我的博客用的是老外的一个主题,那个主题写得奇奇怪怪,先不提后台做的乱七八糟,甚至为了支持woocommerce,在你没有安装woocommerce插件的时候就会加载woocommerce的样式表(真的是服了)
我的优化插件没有用任何框架,所有的样式都是纯手写的,原生的代码。插件现在的压缩包就100k大小,很是精简。
但是还是那句话,自己不了解或者没必要的优化没必要开,要不然反而会让网站卡顿或者直接报错!
为什么叫模块化的优化插件呢?因为我的插件所有的功能都是分模块写的,css和js分开,admin.php是插件的总设置模块,其他的php均为功能代码,用public static function init(){ 挂载设置选项。所有设置在admin.php有个“蓝图”的预设值,如果用户在插件里面点了保存,会自动和和蓝图对比,选项有修改就会保存,反之。这么写可能有些笨拙但是用的时候反而很稳妥。
![]()
插件里面所有优化选项我没有写的很死,优先级不高
Shiguang-Site-Optimizer
插件开源版本可以在我github下载,1.0和1.3功能很少但是日常也是够了。v3重构了后台和所有优化代码,差不多用了一个多月测试,支持php7.4-8.2(测试环境是8.2,再新就不知道了)
自动更新功能其实实现起来很简单,这里说一下我的做法:
这个过程分为两个核心部分:
- 更新服务器:一个存放更新信息的远程服务器。最简单的方式是使用一个公开的 JSON 文件。
- 插件端的更新器:一段 PHP 代码(独立
updater.php文件),它会定期请求更新服务器,获取信息,并告诉 WordPress 何时有新版本。
第1步:创建更新信息 JSON 文件
首先,您需要创建一个名为 info.json 的文件,并把它托管在一个公开可访问的地方,比如您的网站服务器、GitHub Gist 或者一个 GitHub 仓库中。这个文件就是您的“更新服务器”。
当您的插件检查更新时,它会来读取这个文件的内容。
info.json 文件内容示例:
{
"name": "时光优化",
"slug": "shiguang-optimizer",
"version": "3.5.2",
"author": "FuHua",
"homepage": "https://www.shiguang.ink/1851",
"download_url": "https://example.com/path/to/your/plugin-v3.5.2.zip",
"requires": "5.5",
"tested": "6.5",
"requires_php": "7.4",
"last_updated": "2025-10-07 15:30:00",
"sections": {
"description": "这是时光优化插件的详细描述。一个现代化、模块化的 WordPress 站点优化插件。",
"installation": "上传插件压缩包到您的WordPress网站,然后激活即可。",
"changelog": "<h4>版本 3.5.2</h4><ul><li>修复:解决了 Nginx 环境下的兼容性问题。</li><li>优化:重构了后台 ID 列功能,支持更多内容类型。</li></ul><h4>版本 3.5.1</h4><ul><li>初始发布版本。</li></ul>"
},
"banners": {
"low": "https://example.com/path/to/your/banner-772x250.jpg",
"high": "https://example.com/path/to/your/banner-1544x500.jpg"
}
}
字段说明:
version: 最重要的字段。当这里的版本号大于用户插件的当前版本号时,WordPress 才会提示更新。download_url: 最重要的字段。这是一键更新时下载的插件.zip压缩包的直接链接。slug: 插件的唯一标识符,需要和您updater.php文件中设置的一致。sections: 包含了“详情”弹窗中的内容,特别是changelog(更新日志) 非常重要。banners: “详情”弹窗顶部的横幅图片(可选)。
第2步:创建 updater.php 文件
现在,在您的插件根目录(和主插件文件平级)创建一个名为 updater.php 的新文件。将以下代码完整地复制进去。
updater.php 文件完整代码:
<?php
if ( ! defined( 'ABSPATH' ) ) exit;
class Shiguang_Updater {
private $plugin_file; // 插件主文件路径, e.g., /path/to/wp-content/plugins/shiguang-optimizer/shiguang.php
private $plugin_slug; // 插件的目录名, e.g., shiguang-optimizer
private $current_version; // 插件当前版本
private $update_url; // 远程 info.json 文件的URL
private $plugin_data; // 缓存插件头信息
/**
* 构造函数
* @param string $_plugin_file 插件主文件的 __FILE__ 常量
*/
public function __construct( $_plugin_file ) {
$this->plugin_file = $_plugin_file;
// 1. 设置更新服务器的URL
// !!!【【【请务必修改为您自己的 info.json 文件地址】】】!!!
$this->update_url = 'https://raw.githubusercontent.com/shiguang/Shiguang-Site-Optimizer/main/info.json';
// 2. 从插件头信息中获取数据
$this->plugin_data = get_plugin_data( $this->plugin_file );
$this->current_version = $this->plugin_data['Version'];
$this->plugin_slug = plugin_basename( dirname( $this->plugin_file ) );
// 3. 注册钩子
$this->define_hooks();
}
/**
* 注册所有必需的WordPress钩子
*/
private function define_hooks() {
// 核心钩子:拦截WordPress的插件更新检查
add_filter( 'pre_set_site_transient_update_plugins', [ $this, 'check_for_updates' ] );
// 拦截插件详情页弹窗的信息
add_filter( 'plugins_api', [ $this, 'plugin_api_info' ], 10, 3 );
}
/**
* 向远程服务器请求更新信息
* @return object|false 远程服务器返回的JSON对象,或者在失败时返回false
*/
private function request_info() {
// 检查缓存中是否已有数据
$remote_info = get_transient('shiguang_update_info');
if (false !== $remote_info) {
return $remote_info;
}
// 发起远程请求
$response = wp_remote_get( $this->update_url, [ 'timeout' => 10 ] );
// 检查请求是否成功
if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
return false;
}
$remote_info = json_decode( wp_remote_retrieve_body( $response ) );
if ( $remote_info ) {
// 缓存结果12小时
set_transient('shiguang_update_info', $remote_info, 12 * HOUR_IN_SECONDS);
return $remote_info;
}
return false;
}
/**
* 检查是否有新版本,并将其注入到WordPress的更新数据中
* @param object $transient WordPress的插件更新瞬态数据
* @return object 修改后的瞬态数据
*/
public function check_for_updates( $transient ) {
// 如果 $transient->response 为空,则提前返回
if ( empty( $transient->checked ) ) {
return $transient;
}
// 获取远程更新信息
$remote_info = $this->request_info();
// 检查版本是否需要更新
if ( $remote_info && version_compare( $this->current_version, $remote_info->version, '<' ) ) {
$plugin_update_data = new stdClass();
$plugin_update_data->slug = $this->plugin_slug;
$plugin_update_data->plugin = plugin_basename( $this->plugin_file );
$plugin_update_data->new_version = $remote_info->version;
$plugin_update_data->tested = $remote_info->tested;
$plugin_update_data->package = $remote_info->download_url;
$plugin_update_data->url = $remote_info->homepage;
// 将我们的插件更新信息添加到WordPress的响应对象中
$transient->response[ $plugin_update_data->plugin ] = $plugin_update_data;
}
return $transient;
}
/**
* 为插件详情弹窗提供信息 (如更新日志)
* @param object|false $result 默认的API响应
* @param string $action 请求的操作 (e.g., 'plugin_information')
* @param object $args 请求参数
* @return object 修改后的API响应
*/
public function plugin_api_info( $result, $action, $args ) {
// 检查是否是请求我们的插件信息
if ( 'plugin_information' !== $action || ! isset( $args->slug ) || $args->slug !== $this->plugin_slug ) {
return $result;
}
$remote_info = $this->request_info();
if ( $remote_info ) {
$result = new stdClass();
$result->name = $remote_info->name;
$result->slug = $this->plugin_slug;
$result->version = $remote_info->version;
$result->author = $remote_info->author;
$result->homepage = $remote_info->homepage;
$result->requires = $remote_info->requires;
$result->tested = $remote_info->tested;
$result->requires_php = $remote_info->requires_php;
$result->download_link = $remote_info->download_url;
$result->last_updated = $remote_info->last_updated;
// 处理 sections, 转换对象为数组
if (isset($remote_info->sections)) {
$result->sections = (array) $remote_info->sections;
}
// 处理 banners, 转换对象为数组
if (isset($remote_info->banners)) {
$result->banners = (array) $remote_info->banners;
}
return $result;
}
return $result;
}
}
重要: 请务必修改 updater.php 文件中 __construct 函数里的 $this->update_url 变量,将其替换为您自己 info.json 文件的实际URL。
第3步:在您的主插件文件中加载更新器
最后一步,打开您插件的主文件(比如 shiguang-optimizer.php),在文件开头部分,require 刚刚创建的 updater.php 文件,并实例化更新类。
假设您的主插件文件和 admin.php 等文件都在插件根目录,您可以这样添加:
在主插件文件(例如 shiguang-optimizer.php)中添加:
<?php
/**
* Plugin Name: 时光优化
* Plugin URI: https://www.shiguang.ink/1851
* Description: 一个现代化、模块化的 WordPress 站点优化插件。
* Version: 3.5.1 <-- 这里的版本号要和您实际版本对应
* Author: 你的名字
* Author URI: https://www.shiguang.ink
* License: GPLv2 or later
* Text Domain: shiguang
*/
if ( ! defined( 'ABSPATH' ) ) exit;
// 定义插件常量
define( 'SHIGUANG_VERSION', '3.5.1' );
// ... 其他常量定义
// 加载所有模块
require_once plugin_dir_path( __FILE__ ) . 'admin.php';
// ... 其他 require_once 语句
// ▼▼▼ 【【【在这里添加更新器代码】】】 ▼▼▼
// 1. 加载更新器文件
require_once plugin_dir_path( __FILE__ ) . 'updater.php';
// 2. 在后台初始化时启动更新器
function shiguang_init_updater() {
// 将 __FILE__ 传递给更新器类,让它知道是为哪个插件工作
new Shiguang_Updater( __FILE__ );
}
add_action( 'admin_init', 'shiguang_init_updater' );
// ▲▲▲ 【【【更新器代码添加结束】】】 ▲▲▲
// 初始化后台菜单等
add_action( 'admin_menu', ['Shiguang_Admin', 'add_menu'] );
// ... 插件的其他启动代码
总结与测试
完成以上三步后,您的插件自动更新功能就配置好了。
如何测试?
- 确保您当前安装的插件版本(主文件头部的
Version)低于您在info.json文件中设置的version。例如,插件是3.5.1,JSON 文件中是3.5.2。
- 登录 WordPress 后台,进入 仪表盘 > 更新。
- 点击“再次检查”按钮。WordPress 会清除缓存并立即检查更新。
- 稍等片刻,您应该就能在“插件”页面看到您的“时光优化”插件出现了更新提示。您还可以点击“查看详情”,确认更新日志等信息是否和您
info.json中写的一致。

6 条回复
排序