• Resolved geowb

    (@geowb)


    I’ve working on a relatively complex plugin but an not at all clear on which bits of it should be in activiate & which in init. As of now virtually all of it is in add_action('init', 'rmaInit'). In outline, rmaInit does the following:

    • Use a Plugin class for creating the plugin
    • Define 10 plugin settings
    • Use a SettingsPage class to create the settings page
    • Define 4 page titles, contents, creation class & function
    • Define 2 templates
    • Use a PageTemplater class to create templates
    • Call a class to create a table, contingent on a plugin setting

    Yet to be added is a daily WP_Cron job to update the table created above if it’s created.

    When I called the rmaInit function with the activation hook, the plugin was shown as created but its settings were not available. So I’m led to ask what of any of the current functions done by the rmaInit function should be in an activation hook, or …?

    Code available on request, but the rmaInit is currently ~100 lines.

    PS: is this a support question or not? I’m not clear on what is implied by the checkbox re: support questions.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    There’s not an easy answer. As you probably know, the activation hook only runs upon activation and the init hook runs on every single request. An outside observer would think it should be obvious what goes where, but it’s not. A number of things in WP are re-created on every request that our outsider would think of as permanent and only needing to be done once. Things like post types, admin menu items, taxonomies, are all generated on every request.

    Other things like roles and capabilities, scheduled tasks are stored in the DB and persist without any code regenerating on every request. So if something is saved in the DB, it probably can be done on activation. If not in the DB, it probably has to be regenerated on every request. There may be exceptions, though I cannot offer any examples.

    All topics in these forums should be support questions. Yours certainly is. I’m unsure why the check box is still there. I think it is an artifact from a different support forum concept. It’s from before my time here. As it is, checking the box is pretty much a “delete me” flag since non-support questions are subject to deletion. Sorry for any confusion it may have caused.

    Thread Starter geowb

    (@geowb)

    Thanks for your reply. I suppose it really answers my question if it can be read as, “If you can make it work without throwing errors or bogging the system down you’re about as good as you can be.” Test, test, then retest. And I’ll ignore the support question checkbox in the future, provided I truly think I’ve exhausted all other remedies.

    Moderator bcworkz

    (@bcworkz)

    Basically, yes. You’ll eventually get a sense of what’s what. Until then, I would first try using the activation hook since that occurs less often. If doing so causes problems, then “init” may be a better candidate. Then there are several things that must be done during a particular action, not init or activation. Those cases should all be documented in the Code Reference.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Activate and/or init plugin’ is closed to new replies.