The description for register_uninstall_hook
's $callback
parameter states:
Must be a static method or function.1
There is no such comment for register_activation_hook
or register_deactivation_hook
. However, in the Codex entry for register_activation_hook
there is an example that reads:
Or, because the activation hook requires a static function, if you're inside of a __construct():2
In a GitHub issue, "why is activate a static method?", a user states:
You can't define them as standard functions because they require an instance of the plugin to fire, but you can't instantiate the plugin prior to firing the activation function because the constructor will fire (even if it's empty), so functions need to be marked as static so that any preliminary work required to prepare the plugin for de/activation can be set.3
When using classes to (de)activate a plugin, are the functions required to be static? If so, is the explanation as to why correct?
The description for register_uninstall_hook
's $callback
parameter states:
Must be a static method or function.1
There is no such comment for register_activation_hook
or register_deactivation_hook
. However, in the Codex entry for register_activation_hook
there is an example that reads:
Or, because the activation hook requires a static function, if you're inside of a __construct():2
In a GitHub issue, "why is activate a static method?", a user states:
You can't define them as standard functions because they require an instance of the plugin to fire, but you can't instantiate the plugin prior to firing the activation function because the constructor will fire (even if it's empty), so functions need to be marked as static so that any preliminary work required to prepare the plugin for de/activation can be set.3
When using classes to (de)activate a plugin, are the functions required to be static? If so, is the explanation as to why correct?
Share Improve this question asked Nov 29, 2017 at 4:43 SpencerSpencer 2762 silver badges5 bronze badges2 Answers
Reset to default 4Depends the way you want to implement it. The static is used because you don't have to instantiate the class to use the functions of the class. It's up to you. I generally would do:
<?php
/*it's good practice that every class has its own file. So put it in for example 'classes/class-hooks.php' and require it properly in your plugin file. */
class Hooks{
static function activation(){
//the code you want to execute when the plugin activates
}
static function deactivation(){
//the code you want to execute when the plugin deactivates
}
static function uninstall(){
//the code you want to execute when the plugin uninstalled
}
}
...
// you should use this in your plugin file
register_activation_hook(__FILE__, 'Hooks::activation' );
register_deactivation_hook(__FILE__, 'Hooks::deactivation');
register_uninstall_hook(__FILE__, 'Hooks::uninstall');
This is the simple way I know to do it and the plugins I read the code generally does that way. Hope that helps!
In my plugin folder I have created a folder named includes who have two files php
#1 = my-plugin-activate.php
<?php
/**
* @package MyPlugin
*/
class MyPluginActivate {
public static function activate() {
flush_rewrite_rules();
}
}
#2 - my-plugin-desactivate.php
<?php
/**
* @package MyPlugin
*/
class MyPluginDeactivate {
public static function deactivate() {
flush_rewrite_rules();
}
}
In my principal file php : my-plugin.php I have required this two file in the bottom of the class MyPlugin
we must first instantiate the class and register it
$fm = new MyPlugin();
$fm->register();
there are two methode to require the file externe activate.php and deactivate.php
register_activation_hook( FILE, array( $fm, 'activate' ) );
or
require_once plugin_dir_path( FILE ) . 'includes/my-plugin-activate.php'; register_desactivation_hook( FILE, array( 'MyPluginActivate', 'activate' ) );
it's the same for Deactivate
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745112491a4611929.html
评论列表(0条)