admin管理员组文章数量:1316823
I need to know how to write function for registering activation_hook
and deactivation_hook
following OOP fashion with best practices.
I know there are some tasks we can do inside plugin activation/deactivation which is really essential in some cases like to create professional plugins.
Thanks.
I need to know how to write function for registering activation_hook
and deactivation_hook
following OOP fashion with best practices.
I know there are some tasks we can do inside plugin activation/deactivation which is really essential in some cases like to create professional plugins.
Thanks.
Share Improve this question asked Jun 3, 2017 at 11:56 magento_psychomagento_psycho 452 silver badges5 bronze badges 6- 1 Best practice is not to use "OOP" in wordpress plugins. Use namespaces instead. OOP means that there might be more than one object, each with its own activation handling.... scenario that do not make much sense. – Mark Kaplun Commented Jun 3, 2017 at 16:37
- 1 Good Point about namespace and OOP. Thanks! Just a quick thought, what if I use static keyword before the methods so that the static methods would be callable without an instance of the object. While I still found many places of codex.wordpress and developer.wordpress where OOP is being used in plugins too. – magento_psycho Commented Jun 3, 2017 at 17:30
- :) you can write code however you want and it will even work and will be stable and secure etc, it will just not be OOP ;). Wordpress is mostly functional, especially the hook system, and it just do not translate in any trivial way into OOP style of development. The biggest problem with using "OOP" is that many times, when the hook is not a a static function, it is hard or just impossible to remove the hook when needed, and too many themes and plugins do that. – Mark Kaplun Commented Jun 3, 2017 at 17:38
- 1 @MarkKaplun Wow, Excellent Point you stated. but I wonder why does wordpress recommend sample boiler plate of plugin which is OOP oriented!! :) – perfectionist1 Commented Jun 3, 2017 at 17:51
- 1 because of history, (and again it uses classes which is not the same as being OOP ;) ). PHP got namespaces only with version 5.3, and officially wordpress core still supports PHP 5.2. Now before 5.3 the only way to avoid collisions between the same function name in different plugins and themes was by either prefixing it with some string, usually based on the plugin name, or author, and the other way was to group the functions in a class. Obviously grouping them in a class seems to be more structured and requires less mental thinking about prefixes, and less typing. – Mark Kaplun Commented Jun 3, 2017 at 18:07
2 Answers
Reset to default 3It can be demonstrated like this way:
Step-1: Create a folder named includes
(or any name you like) inside the plugin root folder. Create two new files named class-responsive-slider-activator.php
and class-responsive-slider-deactivator.php
Now, in class-responsive-slider-activator.php create a class -
class Responsive_Slider_Activator {
public static function activate() {
//do your codes to execute upon activation
}
}
and in class-responsive-slider-deactivator.php create another class-
class Responsive_Slider_Deactivator {
public static function deactivate() {
//do your codes to execute upon deactivation
}
}
Step-2: In main plugin file create functions and register the two hooks -
// this code runs during plugin activation
function activate_responsive_slider() {
require_once plugin_dir_path( __FILE__ ) . 'includes/class-responsive-slider-activator.php';
Responsive_Slider_Activator::activate();
}
register_activation_hook( __FILE__, 'activate_responsive_slider' );
// this code runs during plugin deactivation
function deactivate_responsive_slider() {
require_once plugin_dir_path( __FILE__ ) . 'includes/class-responsive-slider-deactivator.php';
Responsive_Slider_Deactivator::activate();
}
register_deactivation_hook( __FILE__, 'deactivate_responsive_slider' );
That's it.
N.B. - As per your interest, I would like to denote that there are some essential tasks which can be accomplished through activation and deactivation hooks like:
- Validating other dependent plugin on activation.
- Creating custom database tables on activation to store data and removing tables on deactivation.
- Creating custom options for plugins in activation and reset in deactivation.
- Any other necessary task need to execute in activation. etc
Well there I got the below ways better-
// in the main plugin file
define( 'PLUGIN_BASE_FILE', __FILE__ );
class MainClass {
public static function init() {
register_activation_hook( PLUGIN_BASE_FILE, array( 'MainClass', 'install' ) );
}
public static function install() {
// Do what ever you like here.
}
}
MainClass::init();
Another way would be defining a class and on instantiating the class through __construct
method create a property with the value of __FILE__
. The call the register_activation_hook
in the init function. Like below-
class MainClass {
public $file;
public function __construct( $file ) {
$this->file = $file;
}
public function init() {
register_activation_hook( $this->file, array( $this, 'install' ) );
}
public function install() {
// Do what ever you like here.
}
}
// This part must have to be done in main plugin file.
$class = new MainClass(__FILE__);
$class->init();
But, this instantiation must be done in the plugin main file.
Hope that helps.
本文标签: How to register activation and deactivation hook in plugin using OOP pattern
版权声明:本文标题:How to register activation and deactivation hook in plugin using OOP pattern 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742013788a2413385.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论