-
Notifications
You must be signed in to change notification settings - Fork 0
[Addons] Basics
An add-on is an encapsulated module with additional functionality that a plugin/theme may or may not create under certain conditions. In other words, these are additional functions that are needed only under certain conditions. Usually in plugins/themes this is due to functionality that is activated only with certain settings. A plugin/theme can have any number of add-ons.
Each add-on is a simplified version of the plugin, while it retains the connection with the plugin that creates it and can use a certain set of functionality of its plugin.
As with the plugin, you need to follow three simple steps to create the add-on:
- Create a new class for your add-on.
- Provide the add-on configuration.
- Register add-on in your plugin/theme.
Create a new class inheriting the class zukit_Addon and override the config method in which to return an array with some keys. Необходимые ключи это name and options. The array under the options key will be used as default values for add-on options. If this array is not empty, then the add-on options will be integrated into the plugin options under the key '<name> _options'. To work with the add-on options, it is recommended to use your add-on class methods. These methods allow you to get and set options and the methods themselves take care of updating plugin options as needed.
class my_AdminColors extends zukit_Addon {
protected function config() {
return [
'name' => 'colors',
'options' => [
'option1' => true,
'option2' => false,
],
];
}
}
class my_Plugin extends zukit_Plugin {
public function init() {
if($this->is_option('admin_colors')) {
// create instance of 'my_AdminColors' class and register add-on
$this->register_addon(new my_AdminColors());
}
}
}❌ Attention! You should not define a class constructor __construct in a new add-on class.
If you need to do something in the class constructor, you should override the construct_more method.
protected function construct_more() {
add_action('pre_get_posts', [$this, 'tableview_select_folder']);
add_action('restrict_manage_posts', [$this, 'tableview_category_filter']);
}If the add-on requires scripts or styles to work, then you need to override the enqueue or admin_enqueue method depending on where the scripts and styles are needed, respectively (front-end or admin pages). The admin methods receive a $hook argument and you can check if the scripts need to be loaded. You can use standard WordPress functions for enqueue scripts/styles, but you can use the same helpers that exist for the plugin (see more in the Scripts & Styles Params).
public function admin_enqueue($hook) {
if(in_array($hook, ['upload.php', 'post.php', 'post-new.php'])) {
$this->admin_enqueue_script('colors', [
'data' => $this->collect_script_data(),
'bottom' => false,
'deps' => [
'plupload',
'lodash',
'jquery-ui-draggable',
'jquery-ui-droppable',
'jquery-ui-dialog',
],
]);
// plugin prefix will be added to script filename automatically
$this->admin_enqueue_style('colors');
}
}❗ Please note that the plugin/theme prefix will be automatically added to the filename (which is passed as the first argument for all scripts/styles methods). So if you call a method with argument 'colors' and your plugin prefix is 'myplugin', then the framework will try to load the file 'myplugin-colors'. If you want to change this behavior, then pass the 'add_prefix' set to false as one of the params.
public function enqueue() {
// prefix won't be added to filename
$this->enqueue_style('frontend-colors', ['add_prefix' => false]);
}Zukit - The Developer Framework for WordPress