Global variables during plugin activation , deactivation and uninstall operarations
-
10-12-2020 - |
Question
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?
Solution
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.
OTHER TIPS
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.