• I’m sure that this has been answered before, but I cant seem to find the solution having searched here and on Google.

    I want to know what hooks I can use for when a plugin is installed/deleted? This is different from activated and deactivated.

    What I want to happen:

    Installed: Nothing
    Activated: Create database tables, add capability to role
    Deactivated Remove capability from role
    Activated: Add capability to role
    Deactivated Remove capability from role
    Deleted: Delete database table

    Basically, I’d like the capability added and removed when the plugin is activated and deactivated, but I want the database table and contents persistent until the plugin is deleted. (I’m already using register_activation_hook() and register_deactivation_hook()).

    Any help would be much appreciated. In the mean time I’ll look for a plugin that does this and take it apart 😉

Viewing 5 replies - 1 through 5 (of 5 total)
  • You can do all that with the register_activation/deactivation hooks EXCEPT, you can’t do the delete part if I’m not wrong.

    Since deletion can simply be a case of the blog owner deleting your plugin files, there’s nothing of your code left to do the deletion.

    IIRC, the recommendation was to add an option to set whether the database table is deleted or not, then your code should check for the setting when deactivating.

    Also bear in mind that in some cases, your activation/deactivation can fail, but WordPress thinks it completed and marks the plugin as activated/deactivated. So you might end up with an inconsistent state.

    I initially joined this forum because I was trying to find a way to tell the user that the activation/deactivation didn’t succeed 100%, e.g. table creation failed or insertion of default data didn’t work, despite WordPress’ listing it as activated.

    Thread Starter huggie

    (@huggie)

    Thanks Xephan, I guess I could add a setting for it.

    My only issue is that if they edit the settings when they activate the plugin and say ‘When deactivating the plugin don’t delete the database’, they may have forgotten about it when they actually want to delete it.

    I hate viewing my blog database and seeing tables for plugins that I installed to test and then didn’t like, or plugins that I deleted months earlier.

    Cheers
    Rich

    I remember reading that the deactivation hook can be used to be nasty and NOT allow the user to deactivate the plugin, which is of course not recommended 😀

    But in your case, it should theoretically be possible use the deactivation hook to ask the user if they want to delete the database tables.

    Thread Starter huggie

    (@huggie)

    OK, I’ll look into that then. I’m not sure how I’d present that to the user, but I’m sure I’ll find a way.

    I think I’ll still look for some plugins that have similar functionality.

    Thread Starter huggie

    (@huggie)

    Xephan, I found a way to do it by looking at some other plugins that I have installed.

    I can be done using the register_uninstall_hook() function, but also via a simple method of creating an uninstall.php in the root of the plugin directory and checking for a constant called WP_UNINSTALL_PLUGIN before executing it.

    The details are in the provided link.

    Rich

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Keep a plugin tidy’ is closed to new replies.