admin管理员组

文章数量:1312889

I'm building my first plugin and it's very basic. I created my init class that is used for enqueue styles.. etc. It also has the register_activation_hook and register_deactivation_hook hooks but I don't know what to do with those!

My question is this.. Put yourself in my position, that you want to create a basic plugin that adds small features like related posts, maybe some small widgets... etc, and you don't want to do anything on activation/deactivation or uninstall.. Would you..

  • Remove those hooks?
  • Keep the hooks and leave the callback functions empty or return null...?
  • Do something else that any plugin should do when activate or deactivate or uninstall?

Thank you!

I'm building my first plugin and it's very basic. I created my init class that is used for enqueue styles.. etc. It also has the register_activation_hook and register_deactivation_hook hooks but I don't know what to do with those!

My question is this.. Put yourself in my position, that you want to create a basic plugin that adds small features like related posts, maybe some small widgets... etc, and you don't want to do anything on activation/deactivation or uninstall.. Would you..

  • Remove those hooks?
  • Keep the hooks and leave the callback functions empty or return null...?
  • Do something else that any plugin should do when activate or deactivate or uninstall?

Thank you!

Share Improve this question asked Nov 19, 2017 at 10:39 mrKC.988mrKC.988 1072 silver badges14 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

You don't have to use the register_activation_hook or register_deactivation_hook hook, they are optional. As their name suggests, they are hooks planned to run a task on plugin's activation/deactivation, such as updating the status of all users for some purpose.

So, if your plugin doesn't require such tasks, then simply don't use them. If you want to register new post types and taxonomies, you can use the init hook instead.

As another respondent has already noted, they are both optional. Still, I believe they are underutilized, especially register_activation_hook. If a block of code only needs to run once (e.g., to register a new post type), it would be obviously more performant to run it only on plugin activation instead of every time a page is loaded!

Keep in mind, however, that activation hooks are called before either the plugins_loaded hook or the init hook[1], so there's no point in trying to register an activation hook from a function added to either of those commonly used hooks.

register_deactivation_hook is useful for removing actions or filters created by your plugin, among other things.

本文标签: plugin developmentIs using registeractivationhook required