Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

How to execute a function not only on first activation? WordPress

  • SOLVED

Within my plugin [[LINK href="http://mapsmarker.com"]]mapsmarker.com[[/LINK]] I created the function lmm_install_and_updates() which gets fired every time the plugin is called. It creates the tables needed at first activation and then checks on each call the version number if it should execute code or not, like

if (get_option('leafletmapsmarker_version') == '1.0' ) {
$save_defaults_for_new_options = new Leafletmapsmarker_options();
$save_defaults_for_new_options->save_defaults_for_new_options();
update_option('leafletmapsmarker_version', '1.1');
}
if (get_option('leafletmapsmarker_version') == '1.1' ) {
$save_defaults_for_new_options = new Leafletmapsmarker_options();
$save_defaults_for_new_options->save_defaults_for_new_options();
update_option('leafletmapsmarker_version', '1.2');
}
if (get_option('leafletmapsmarker_version') == '1.2' ) {
update_option('leafletmapsmarker_version', '1.2.1');
}

...
This ensures that users dont have to save new options to initialize them for example.

First I wanted to use register_activation_hook() for this but as I read from docs, this function only gets fired when the user first activates the plugin and not on plugin updates?
According to [[LINK href="http://codex.wordpress.org/Function_Reference/register_activation_hook"]]http://codex.wordpress.org/Function_Reference/register_activation_hook[[/LINK]] there were some discussions about a register_update_hook(), but this proposal was rejected.

I would like to fire my function only on activation and on plugin updates (bulk or single) and not each time the plugin files are called. But as far as I have searched the Codex, I couldnt find a function/hook for this. Am I wrong? Whats the best way to solve this?
Thanks for any tips!

Answers (2)

2012-06-13

Luis Abarca answers:

Add an option to store your plugin version, when you perform an update you can compare version installed vs your update

register_activation_hook(__FILE__, 'myplugin_onactivate');

function myplugin_onactivate()
{
// TODO:
}


Robert Harm comments:

the plugin version already gets stored in the option lmm_version and my lmm_install_and_updates() function checks each time the plugin is called via
add_action('admin_init', array(&$this, 'lmm_install_and_updates'),2);
if the plugin is up to date.

To my understanding the register_activation_hook doesnt fire on updates (single or bulk) or am I wrong?


Luis Abarca comments:

Nop, dont fire update hooks, but you can check with other hooks, like plugins_loaded or init, etc (a cron job maybe works too).

Just check your version and when are versions doesn't match run your update routine.

2012-06-13

Kailey Lampert answers:

This is completely untested, just an idea...

[[LINK href="http://phpxref.ftwr.co.uk/wordpress/wp-admin/includes/class-wp-upgrader.php.source.html#l419"]]upgrade code[[/LINK]] fires this near completion:
delete_site_transient('update_plugins');

in delete_site_transient(), there's this
do_action( 'delete_site_transient_' . $transient, $transient );

So perhaps...

add_action('delete_site_transient_'.'update_plugins', function() {
//do on update
});


Edit: This probably would fire on every plugin's update, maybe that's ok? or maybe there's some way to detect which plugin is being updated...


Robert Harm comments:

thanks - could work on plugin updates but I guess not on first activation...


Kailey Lampert comments:

I suspect that you'll still need to hook in twice, once on activation (using register_activation_hook perhaps) and once during upgrade.


Robert Harm comments:

that´s what I also just thought about :-) Too bad that there is no update hook :-/