最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

oop - WordPress plugin activation, deactivation and uninstall hook not being triggered

programmeradmin1浏览0评论

I am developing a WordPress plugin which requires certain rules to be added to the .htaccess file of the WordPress installation upon activation of the plugin.

Initially, before I switched my code to OOP (with classes and functions) the hooks and this functionality was working. But after I switched it, it doesn't work.

register_activation_hook( __FILE__, array( 'Optimizo', 'activation' ) );

The above code is how I am trying to call the function 'activation' from the class 'Optimizo' which resides in the same file as where I am writing the above activation hook. The 'activation' function has the necessary classes in it which should be called upon activation. Below is the content of the function.

protected function activation() {

    $this->activate();
}

I am just wondering if I wrote the code wrong of is it because that the function is having the protected access specifier or if I am doing something entirely wrong.

PS the function 'activate' resides in another class which is being extended to the 'Optimizo' class.

I am developing a WordPress plugin which requires certain rules to be added to the .htaccess file of the WordPress installation upon activation of the plugin.

Initially, before I switched my code to OOP (with classes and functions) the hooks and this functionality was working. But after I switched it, it doesn't work.

register_activation_hook( __FILE__, array( 'Optimizo', 'activation' ) );

The above code is how I am trying to call the function 'activation' from the class 'Optimizo' which resides in the same file as where I am writing the above activation hook. The 'activation' function has the necessary classes in it which should be called upon activation. Below is the content of the function.

protected function activation() {

    $this->activate();
}

I am just wondering if I wrote the code wrong of is it because that the function is having the protected access specifier or if I am doing something entirely wrong.

PS the function 'activate' resides in another class which is being extended to the 'Optimizo' class.

Share Improve this question asked May 14, 2019 at 13:43 MinhazMinhaz 11 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 1

The function needs to be public, but it can also be static.

For example, from the Akismet plugin:

In the plugin file:

register_activation_hook( __FILE__, array( 'Akismet', 'plugin_activation' ) );

In the Akismet class file:

public static function plugin_activation() {
    ...
}
发布评论

评论列表(0)

  1. 暂无评论