Main Docs Page: AutoHosted Docs
1. Add Updater Class in your plugin.
Create a “includes” folder in your plugin, and add “plugin-updater.php” from example plugin “Super Mario”.
In this file, change all instance of Class name Super_Mario_Plugin_Updater
with your plugin prefix, for example My_Awesome_Plugin_Updater
. There is two instance of this class name, one is in class exist check, and the second is the actual class.
You can also change all ‘text-domain
‘ string with your plugin text domain if your plugin support language translation.
2. Include and activate
Now, you need to include and use the updater class from your plugin main file. You can see how to use it in Super Mario plugin example. Let’s create for dummy plugin: “awesome-plugin”
/* hook updater to init */
add_action( 'init', 'my_awesome_plugin_updater_init' );
/**
* Load and Activate Plugin Updater Class.
*/
function my_awesome_plugin_updater_init() {
/* Load Plugin Updater */
require_once( trailingslashit( plugin_dir_path( __FILE__ ) ) . 'includes/plugin-updater.php' );
/* Updater Config */
$config = array(
'base' => plugin_basename( __FILE__ ), //required
'dashboard' => false,
'username' => false,
'key' => '',
'repo_uri' => 'http://yoursite.com/',
'repo_slug' => 'awesome-plugin',
);
/* Load Updater Class */
new My_Awesome_Plugin_Updater( $config );
}
First you need to create a function and hook it to init
hook. In your function:
Load Plugin Updater
This is where we load the “plugin-updater.php” file using require_once
.
Updater Config
$config = array(
'base' => plugin_basename( __FILE__ ), //required
'dashboard' => true,
'username' => false,
'key' => '',
'repo_uri' => 'http://yoursite.com/', //required
'repo_slug' => 'awesome-plugin', //required
);
Updater config is an array with, this array is the configuration of your plugin updater. You can read more in Plugin Config Cheat Sheet Documentation to set various configuration in your plugin update.
base
This is required to get the proper folder name and file name of the plugin.
dashboard
If this set to true
, the updater class will register a dashboard meta box where your user can input activation key. If you don’t use update restriction by activation key feature in your plugin you can set it to false, or remove the line.
The input is saved in database with option name ahp_plugin-folder_activation_key
. The plugin-folder
part is your plugin folder.
if you create your own activation key input, for example in your plugin settings page, you also do not need this.
key
You only need this if you create your own activation key input, it’s not needed if you set dashboard
to true
and you only need this if you choose to use update restiction by activation key.
Example usage if you use your own input:
$config = array(
'base' => plugin_basename( __FILE__ ), //required
'key' => get_option( 'your_plugin_db' ),
'repo_uri' => 'http://yoursite.com/',
'repo_slug' => 'awesome-plugin',
);
If you use this, i assume you already familiar with creating settings page, using the database saved in your settings page, and how to use get_option
function.
username
Set this to true if you want to restrict update by members (role). If set to true and dashboard also set to true, there will be two input in dashboard meta box. Username and Email. Members of your site with role selected can activate automatic update feature after they input their username and email.
If you want to use your own settings and not using dashboard meta box, you can add the data in this key too.
$config = array(
'base' => plugin_basename( __FILE__ ), //required
'key' => get_option( 'your_plugin_db_for_email' ),
'username => get_option( 'your_plugin_db_for_username' ),
'repo_uri' => 'http://yoursite.com/',
'repo_slug' => 'awesome-plugin',
);
repo_uri
This is the home url of the site where you install Auto Hosted plugin. you can get it from Updater data meta box repo_uri
.
repo_slug
This is unique for each plugin, this is your plugin entry slug in your repository.
Load Updater Class
This is how we use the class based on your previous configuration.
/* Load Updater Class */
new My_Awesome_Plugin_Updater( $config );
The class My_Awesome_Plugin_Updater
name need to be the same with class you use in “plugin-updater.php”, the $config
part is the config we set earlier.
And that’s it. 🙂