0

Looking at wp codex it says:

The register_activation_hook function registers a plugin function to be run when the plugin is activated.

I am finding this to be not entirely correct, for example i can activate a plugin that adds an extra field to WP registration form (using the add extra field example in codex) which does not require the register_activation_hook function (nor the register_deactivation_hook).

What codex does not tell you is when and when not to use the register_activation_hook, now i am going to guess that the hook is only required when including files for install or creating db tables on plugin activation, i would be grateful if someone who has more knowledge of WP and the correct use of the register/deregister hooks could clarify its correct usage.

regards

1 Answer 1

2

The register_activation_hook() is actually just a wrapper for add_action('activate_' . $file, $function); and is only executed when a plugin is activated.

This action/function is meant to be used by anything that should only execute once when the plugin is activated, things which would include stuff like

  • adding custom terms to taxonomies

  • create new database tables or rows

  • running one time db processes which might include updating certain tables, columns or rows

  • flush rewrite rules

On the opposite, register_deactivation_hook() only executes when a plugin is deactivated. This hook/function/action is meant for running processes to clean up after the plugin, which may include

  • restoring db tables, row or columns which was altered

  • removing custom db tables which was added by the plugin

  • removing any custom options which was added by the plugin that would not be necessary anymore

1
  • Thank you Pieter, a concise answer which has cleared my confusion on the proper use of these hooks.
    – MartinJJ
    Feb 22, 2016 at 12:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.