Home >CMS Tutorial >WordPress >How to develop a feature that automatically updates a WordPress plugin
How to develop a feature that automatically updates WordPress plugins
WordPress is a very popular open source content management system (CMS) with a rich plugin market to extend its functionality . To ensure that plugins are always up to date and secure, developers need to implement automatic updates. In this article, we’ll walk you through how to develop an auto-updating WordPress plugin and provide code examples to help you get started quickly.
Before starting development, you need to prepare the following key steps:
Next, we will write the plug-in code to implement the automatic update function.
First, create a main plugin file in the plugin directory, for example plugin-name.php
. In this file, you need to define a class to manage the automatic update process of the plugin. The following is a simple plug-in class example:
<?php class Plugin_Name { private $plugin_file; private $plugin_slug; private $version; public function __construct($plugin_file, $plugin_slug, $version) { $this->plugin_file = $plugin_file; $this->plugin_slug = $plugin_slug; $this->version = $version; add_action('init', array($this, 'check_for_update')); add_filter('pre_set_site_transient_update_plugins', array($this, 'set_update_transient')); } public function check_for_update() { $config_url = 'https://example.com/plugin-config.json'; // 替换为您的配置文件URL $config = wp_remote_get($config_url); if (!is_wp_error($config)) { $config = json_decode(wp_remote_retrieve_body($config), true); if (isset($config['version']) && version_compare($this->version, $config['version'], '<')) { $download_url = $config['download_url']; $package = wp_remote_get($download_url); if (!is_wp_error($package)) { $package_file = $this->plugin_file; WP_Filesystem(); global $wp_filesystem; $wp_filesystem->put_contents($package_file, wp_remote_retrieve_body($package)); // 更新插件版本号 $plugin_data = get_plugin_data($this->plugin_file); $plugin_data['Version'] = $config['version']; $plugin_data['RequiresWP'] = $config['requires_wp']; $plugin_data['RequiresPHP'] = $config['requires_php']; $plugin_data['TestedWP'] = $config['tested_wp']; $all_plugins = get_plugins(); $all_plugins[$this->plugin_slug] = array_merge($all_plugins[$this->plugin_slug], $plugin_data); update_option('active_plugins', array_keys($all_plugins)); delete_transient('update_plugins'); // 清除插件更新缓存 } } } } public function set_update_transient($transient) { if (empty($transient->checked)) { return $transient; } $config_url = 'https://example.com/plugin-config.json'; // 替换为您的配置文件URL $config = wp_remote_get($config_url); if (!is_wp_error($config)) { $config = json_decode(wp_remote_retrieve_body($config), true); if (isset($config['version']) && version_compare($this->version, $config['version'], '<')) { $transient->response[$this->plugin_slug] = array( 'new_version' => $config['version'], 'package' => $config['download_url'], 'slug' => $this->plugin_slug ); } } return $transient; } } // 实例化插件类 new Plugin_Name(__FILE__, 'plugin-folder/plugin-name.php', '1.0.0'); ?>
In the above code example, we pass the plug-in file name __FILE__
, plug-in slug and plug-in version number to the plug-in class in the constructor . Then, we use add_action
and add_filter
to bind the check_for_update
method and set_update_transient
method to the corresponding WordPress hooks to implement automatic checking and updated features.
check_for_update
The method first obtains the latest version number and download link of the plug-in from the remote configuration file. Then, download the latest version of the plug-in package through the wp_remote_get
function. Next, we use the WP_Filesystem
class and global $wp_filesystem
to update the plugin file and update the plugin’s version information. Finally, we use the delete_transient
function to clear the plugin's update cache so that we get the latest version of the plugin the next time we check.
set_update_transient
The method is called when WordPress checks for plugin updates and is used to set the update information of the plugin. First, get the latest version number and download link of the plugin from the remote configuration file. The update information is then stored in the $transient
variable, allowing WordPress to discover updates to the plugin.
The above is an implementation example of a basic automatic update WordPress plug-in. Depending on your needs, you can further optimize the code and add features such as error handling and logging.
Finally, you need to configure the plugin’s remote repository and configuration files. You can use a version control tool such as Git to manage the plug-in's code and deploy the plug-in repository to a web server. Then, create a configuration file in JSON format that contains the plug-in information and version number. Store the configuration file on your server and reference its URL in the plugin code.
The following is an example of a configuration file:
{ "version": "1.0.1", "requires_wp": "5.2", "requires_php": "7.2", "tested_wp": "5.4", "download_url": "https://example.com/plugin-package.zip" }
In the configuration file, you can specify the latest version number of the plugin, the minimum version requirement of WordPress, the minimum version requirement of PHP, and the plugin package. Download link.
By following the above steps and code examples, you can easily develop an auto-updating WordPress plugin. The automatic update feature helps you ensure your plugins are always up to date and secure, providing a better user experience.
During development, please make sure to use the latest WordPress development standards and best practices. Also, remember to back up your plugin files before updating in case anything unexpected happens.
I wish you success in your development!
The above is the detailed content of How to develop a feature that automatically updates a WordPress plugin. For more information, please follow other related articles on the PHP Chinese website!