WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] register_activation_hook vs. add_action (5 posts)

  1. vsandvold
    Member
    Posted 6 years ago #

    I'm a bit mystified by the difference between register_activation_hook() and a simple add_action().

    If I place a simple add_action() in the main body of the plugin code, is this function called only upon activation, or will it be called other times?

    I'm about to add some scheduled event, and read from wp_schedule_event about register_activation_hook(), which I must use to avoid ending up with a lot of scheduled events, apparently.

    It this is the case, why aren't add_action hooks added multiple times?

    Let me know if you know:-)

  2. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    Because they are different things... sorta.

    add_action creates an event that hooks to some specific action, which could occur multiple times and probably will. An "action event" is something like "WordPress is displaying the post content", or "the plugins have just loaded" or whatever. It happens every time the page is displayed, or at least, a lot.

    register_activation_hook creates an event that only happens one time, when your plugin is first activated. This actually uses a hidden action which is "this plugin X just got activated", but that's beside the point.

    Although both of these function calls happen every time the page is loaded, the actual code executed by your function is deferred until later.

    See, anything in the "main body" of your plugin code runs *every single time the page is loaded*. add_action and register_activation_hook both defer the execution of some function until a given event occurs. That's what they do, that's how actions work.

    The reason you use register_activation_hook to schedule wp-cron events, is because the wp-cron event is persistent. It reschedules itself (if you use a cron event that's not "single"). So you only want to schedule it once, not have it add a new schedule every time the plugin is loaded (which is every time the page is loaded).

    Note that for any normal plugin, you'll want to register_deactivation_hook too, which will remove the wp-cron event (via wp_clear_scheduled_hook, most likely).

    You might want to read up on actions in general:
    http://codex.wordpress.org/Plugin_API#Actions

  3. vsandvold
    Member
    Posted 6 years ago #

    Thanks for the thorough explanation, Otto42!

    So if I understand this right, add_action will hook a function to an action every time a page is loaded. The hooks then disappear when the page is done loading, because they aren't persistent, and must be re-created for the next page load.

    Seems to me this may add some runtime overhead for page requests, especially if my plugin grows and the site gets a lot of traffic. If I want to optimize my plugin, should I try to detect the context (view vs. admin), in order to execute only the code that is required? Or is WP already so optimized that this will have little effect?

    Thanks again for spelling it out :-)

    BTW, what do you call the part of PHP script not contained within a function, class or a conditional block?

  4. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    So if I understand this right, add_action will hook a function to an action every time a page is loaded. The hooks then disappear when the page is done loading, because they aren't persistent, and must be re-created for the next page load.

    More or less, yes. add_action will hook the action to the function. Then, when the do_action for that action name gets run, the function gets called. You're right about the persistent stuff.

    Seems to me this may add some runtime overhead for page requests, especially if my plugin grows and the site gets a lot of traffic. If I want to optimize my plugin, should I try to detect the context (view vs. admin), in order to execute only the code that is required? Or is WP already so optimized that this will have little effect?

    Meh. It could have some impact, but probably won't. The add_action code basically just sets part of a global variable. So it's pretty minimal. It's a bit of a catch-22 situation, you don't want to add the action unnecessarily, but sometimes just checking for the condition where the action will be needed is actually more expensive than simply adding the action. Setting two fixed strings to a variable is probably cheaper than actually doing a string comparison to figure out what page is being displayed.

    This can be worked around in many cases though. Especially for the admin pages, there's an "admin_head" action that you can use (along with a few others). This only happens on admin pages, so you could hook admin_head and then have it's function add other actions that are needed. There's similar approaches for other pages and such. These can help optimize the speed, if you have several required actions.

    BTW, what do you call the part of PHP script not contained within a function, class or a conditional block?

    Well... "main body" is good. Seems to be the preferred choice.

  5. vsandvold
    Member
    Posted 6 years ago #

    Thank you, Otto42!

Topic Closed

This topic has been closed to new replies.

About this Topic