Support » Everything else WordPress » Plugins Handbook Functions Q

  • Resolved sprowt



    I’m reading the WP Plugins Handbook, starts w register_activation_hook and register_deactivation_hook, but i’ve noticed not all plugins use these.

Why do some plugins (Lightweight Social Icons by GeneratePress) only use ‘plugins_loaded’ hook and neither of the register hooks above and under what conditions would I use one or the other or both?
I’m guessing the social icon plugin doesn’t need additional functionality that comes with the register hooks. Whereas, a more complicated plugin, like CoBlocks by GoDaddy needs the added functionality.

 Am i on the right track by surmising that if a plugin provides CPT functionality, for example, it would require using the activation hooks to assist w the rewrite rules, etc.?
    Thank you!!

    • This topic was modified 2 years, 9 months ago by Jan Dembowski. Reason: Moved to Everything else WordPress, this is not an Developing with WordPress topic
Viewing 3 replies - 1 through 3 (of 3 total)
  • Hello,

    You’re right generally in saying that plugins will only use those hooks if they need to. Those hooks are only fired when a plugin is being activated or deactivated, and so a plugin will only use those if they need to run code at those times.

    A common example for these is registering and de-registering WP Cron hooks, see the examples here:

    Since you only need to add these hooks once and remove them when deactivated, and this is because those are stored in the site options in the database.

    Another use case for the deactivation hook could be deleting data that is no longer required when the plugin is deactivated or removed (though I think there’s another hook for that…)

    For your point 2, these wouldn’t be the place to register Custom Post Types, since these need to be declared every time the site is compiled, since these are not stored in the database (like, the CPTs themselves are, but their definitions are not).

    Generally CPTs would be registered on the init hook making them available for any code that is run thereafter:

    Hope that helps.

    • This reply was modified 2 years, 9 months ago by dominic_ks. Reason: corrected typo
    Thread Starter sprowt


    Yes, thank you.. very helpful!

    I appreciate the WP Cron hooks link which I’ll put on the list to investigate, as well.

    I asked about the CPT example because it’s specifically mentioned in the Plugins Handbook and, to your point, it’s hooked with ‘init’.

    It’s really great to hear I’m on the right track and I’m so GRATEFUL for the opportunity to confirm, clarify, and learn in these forums.

    I’m excited for the opportunity to pay that forward one day (hopefully sooner than later 🙂

    Thanks again and be well!

    I hear you there, it’s the best thing about working with open source, I have largely learned to become a developer by asking questions and getting help on this forum and others like it, and so here I am, helping out where I can.

    Take care.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Plugins Handbook Functions Q’ is closed to new replies.