Support » Developing with WordPress » Hook to when a plugin is deleted

  • Resolved Storm Rockwell

    (@stormrockwell)


    I am trying to add a hook to when any plugin is deleted (not deactivate). Can’t seem to google it. Any help or guidance would be appreciated.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator bcworkz

    (@bcworkz)

    Gotta know the right terminology: “uninstall” 🙂
    https://developer.wordpress.org/plugins/the-basics/uninstall-methods/

    Unfortunately, it’s not foolproof. If someone were to uninstall by deleting via FTP none of this will run. It only runs when the delete action is picked from the plugins screen.

    Awesome, thanks a bunch.

    I know I marked it as resolved but is there any way to tell when any plugin is uninstalled? I’d rather not loop through all the plugins to create hooks for when they’re deleted.

    Moderator bcworkz

    (@bcworkz)

    You would not believe the length of some dialogs that have continued on after a topic has been resolved!

    I now see I initially misunderstood what you are asking. I get it now (I hope). The “delete_plugin” action (not uninstall?? 🙂 ) fires any time any particular plugin is uninstalled, just before the files are unlinked. The principle plugin filename is passed to your callback. If the bulk uninstall action is invoked, this action will fire once for every plugin selected. There is also the “deleted_plugin” action that fires after the “delete_plugin” action and after each plugin folder has been unlinked from the file system. In addition to the plugin file unlinked, in this case your callback is also passed a boolean indicating the success of the unlink command.

    The goal is to not pass a specific plugin file, it’s to detect if any plugin is deleted. Basically, I need to run a method in my script when any plugin is deleted (I don’t even care what plugin was deleted).

    So with the resources you gave me, it sounds like I need to loop through every installed plugin, get the file, then create a hook for it.

    I was hoping for something like “upgrader_process_complete” for deleting but if it doesn’t exist I’ll need to resort to what I stated above.
    https://codex.wordpress.org/Plugin_API/Action_Reference/upgrader_process_complete

    You also mentioned about it not detecting if a plugin is deleted manually so what I will probably do is compare previously stored plugins to currently cached plugins so if they don’t match, then I’ll run my action.

    Thanks for the help, I think I have my solution now.

    Moderator bcworkz

    (@bcworkz)

    Cool. FWIW, though it’s moot now, while you would need to loop through plugins and setup hooks using one the first mentioned methods, this is not required with the “delete_plugin” action. It fires no matter what plugin is deleted. It passes the plugin file name FYI, you do not need to use it if you don’t need it. This is the only hook to install for any plugin removal, no need to loop through plugins.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Hook to when a plugin is deleted’ is closed to new replies.