I am aware that WordPress provides Plugins a nice way to clean up the db if the Plugin is deleted by providing the uninstall.php
hook. You just have to place the cleaning code and it works.
But my question is, I have seen a couple of Plugins which is using functions defined in their Plugin file inside the uninstall.php
file. My understanding is that if the Plugin is already disabled and the user is deleting it after that, then these functions may not be accessible.
Is my assumption correct or is there any WordPress magic which I am not aware of?
That doesn’t work, if the
uninstall.php
calls one of the plugin’s functions, it will produce aFatal error: Call to undefined function
. Unless… (explained bellow).This, on the other hand, works:
We can see why in the function
uninstall_plugin()
. Ifunistall.php
exists, it is included, executed and end of story.If no
uninstall.php
file exists, and if we registered the uninstall hook, the main plugin file will be included:include WP_PLUGIN_DIR . '/' . $file;
, so as to make the callback available -and thus making the other function available too.We could make some
include
s in ouruninstall.php
file, but is the uninstall process so complex as to need this?A quote from a related wp-hackers thread (my emphasis):