The MU forums have moved to WordPress.org

register_activation_hook for mu-plugins (3 posts)

  1. delayedinsanity
    Member
    Posted 15 years ago #

    I'm wondering how other people have chosen to deal with the following scenario -- when a plugin is activated, we have the option to register hooks with register_activation_hook and register_deactivation_hook. Simple enough, and useful if you need to perform various tasks, such as creating or destroying database tables for your particular plugin.

    What about mu-plugins though? The above functions are rendered void, since an mu-plugin is automatically enabled site wide, and never gets physically 'activated'.

    Now there's a few obvious solutions to this situation, but all have their pitfalls I think;

    1) You could manually create the tables and populate them with any default data you require. The obvious downfall to this, if you plan on distributing your plugin, is that it makes the installation process a little more painful on less web savvy types, and the documentation process harder on the developer.

    2) You could run a check from within the mu plugins main file, which looks for the existence of said table and creates it if it isn't found, ala $wpdb->get_row("SELECT table_name FROM information_schema.tables WHERE table_schema='databasename' AND table_name='tablename'", ARRAY_N). The pitfall to this is that you're adding an extraneous query to every page load, which hopefully only actually needed to be run once ever.

    3) If you have an administrative panel for your function, you could add an option to create the tables and populate them with your data. The pitfall to this is that unless you remember to add a number of checks (which you should, but we all know some developers are busy and forget to use such things as function_exists() and strip out their short tags! grr), you leave a potentially damaging option available to anybody who forgets they've already activated the plugin.

    Any options I haven't thought of, alternate ways of dealing with such a situation? Thoughts on those listed above?

  2. DeannaS
    Member
    Posted 15 years ago #

    I've used the option of setting a site_option. If it's not set, or in the case of an upgrade, not the right version, then I run the initialization script.

  3. delayedinsanity
    Member
    Posted 15 years ago #

    @DeannaS, that's what I love about simplicity, it's almost always the best option. I think I'm going to start implementing that idea immediately! :)

About this Topic

  • Started 15 years ago by delayedinsanity
  • Latest reply from delayedinsanity