前段时间我发布了《WordPress非官方主题/插件添加升级功能》此篇文章,使用自建网站服务器的方式给 WordPress 主题或插件提供升级点。当然,缺点在于如果服务器不够稳定,极有可能出现意想不到的后果。本文来自 DeveWork ,将与大家一同实现借助 Github ,为 WordPress 插件添加自动更新的功能,只要稍加修改,相信国内高人一定能整合出适合主题的版本。
WordPress-GitHub-Plugin-Updater 简介
WordPress-GitHub-Plugin-Updater 是一个WordPress 的第三方类。如果你的WordPress 插件是托管到Github 上的,借助这个类可以实现类似WordPress 官方的自动更新升级机制。难得可贵的是,这个类不仅仅支持公共仓库(public repositories),也支持私有仓库的项目(private repositories)——当然前提你是GitHub 的付费用户。支持私有仓库意味着你可以将你的收费插件托管到Github 上而不必担心文件泄露的问题。
WordPress-GitHub-Plugin-Updater 使用方法
Github 主页:https://github.com/jkudish/WordPress-GitHub-Plugin-Updater
下载zip 压缩包解压后安装WordPress后台上,项目本身就是一个插件,你可以安装来查看是如何的运作的。
要集成到你的插件的话,将updater.php 放到你的插件目录下,然后在插件主文件写入下面的代码,注意相关文件名称要修改为你的插件的:
add_action( 'init', 'github_plugin_updater_test_init' ); function github_plugin_updater_test_init() { include_once 'updater.php'; define( 'WP_GITHUB_FORCE_UPDATE', true ); if (is_admin()) { // note the use of is_admin() to double check that this is happening in the admin $config = array( 'slug' => plugin_basename(__FILE__), // this is the slug of your plugin 'proper_folder_name' => 'plugin-name', // this is the name of the folder your plugin lives in 'api_url' => 'https://api.github.com/repos/username/repository-name', // the github API url of your github repo 'raw_url' => 'https://raw.github.com/username/repository-name/master', // the github raw url of your github repo 'github_url' => 'https://github.com/username/repository-name', // the github url of your github repo 'zip_url' => 'https://github.com/username/repository-name/zipball/master', // the zip url of the github repo 'sslverify' => true // wether WP should check the validity of the SSL cert when getting an update, see https://github.com/jkudish/WordPress-GitHub-Plugin-Updater/issues/2 and https://github.com/jkudish/WordPress-GitHub-Plugin-Updater/issues/4 for details 'requires' => '3.0', // which version of WordPress does your plugin require? 'tested' => '3.3', // which version of WordPress is your plugin tested up to? 'readme' => 'README.md', // which file to use as the readme for the version number 'access_token' => '', // Access private repositories by authorizing under Appearance > Github Updates when this example plugin is installed ); new WP_GitHub_Updater($config); } }
access_token 这个参数是私有仓库才需要,如果是公开项目不必填写。私有项目必须按照下面的步骤去获取 Github 应用的 Client ID 与 Client Secret 激活之。
如果填写正确,那么后台会适时提示更新,如下图:
感觉还是手动的比较好。。。
辛苦啦~~~~~~~~
自动更新貌似不安全 。
@郑永 这个还真没研究过……
终于找到使用办法了。。搞了个插件不知道怎么用。谢谢博主
自动更细没问题,但是文章源从哪来呢
我的那个主题也支持自定更新,只是不是github,是OSC
谢谢啊!
学习下