1

For activation hooks codex states that you have to be explicit on globals:

A note on variables during activation

But that seems not to be working on uninstall.php

If i define a global in my main plugin file like this:

global $plugin_options_name;

$plugin_options_name = 'xxxxxxxxx';

Activation hook can use it via global statement , whereas uninstall.php cannot.

I have to redeclare the variable inside unistall.php

If this is the case, if i switch to uninstallation hook this will change?

2

You should prefer the uninstall hook over the file if your main plugin doesn't have side effects (outputs html or writes to file/DB automatically when loaded). IMO there is too much risk (i.e. non zero) of doing the uninstall.php code wrong and open the file to direct execution from the outside. This also help in having all the relevant code in one place.

uninstall.php, when exists, is being executed without loading the plugin code (that is the whole point of it) and therefor whatever is declared in the plugin code will not be available to uninstall.php.

0

I define any constants and globals in their own file, and load that both in the main file of the plugin and in uninstall.php. That way all of my globals are organized in a single place. And then I don't have to worry about accidentally performing side-effects on uninstallation, as might happen if using the hook.

  • What kind of side effects? – e4rthdog Nov 2 '15 at 7:03
  • 1
    @e4rthdog the things that @Mark mentioned in his post: HTML output or file/DB writes. Most plugins shouldn't be automatically doing those things when they are loaded anyway; any functions that do those things should be hooked to actions and filters. But if your plugin did happen to modify something automatically during uninstall, it could have unintended consequences. Using the uninstall.php file means that you don't have to load the whole plugin, just the parts that you need, which makes unintended side-effects less likely. – J.D. Nov 2 '15 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.