Skip to content

plugin_deleted hook is not firing in WordPress when wp plugin uninstall is called in wp cli #437

@dkoston

Description

@dkoston

Bug Report

Describe the current, buggy behavior

When calling wp plugin uninstall $plugin_name the deleted_plugin event does not fire in WordPress

Context, we have a plugin which tracks plugin related events.

When calling wp plugin deactivate, we see this hook fire:

add_action('deactivated_plugin', array(&$this, 'deactivated_plugin_log'));

When calling wp plugin uninstall, this hook does not fire:

add_action('deleted_plugin', array($this, 'deleted_plugin_log'));

However, when deleting the plugin in WP Admin, the hook fires.

Describe how other contributors can replicate this bug

  • Install any plugin in WordPress
  • Add a plugin with the above hooks
  • click in WP Admin to delete the plugin and see the hook has fired (you'll need some logic to log, send http request, etc in your hook function...let me know if you want me to provide that logic
  • install any plugin
  • call wp plugin uninstall $plugin and see that the hook has not fired

NOTE: also tried wp plugin delete but since that technically doesn't do the uninstall / deactivate, I wouldn't expect that to call the hook. wp plugin deactivate does fire the deactivated_plugin hook so it appears that wp plugin uninstall is using a non-standard way to delete a plugin?

Describe what you would expect as the correct outcome

WP CLI should use the same functions in WP Core that WP-Admin does so that the same hooks are activated

Let us know what environment you are running this on

OS:	Linux 4.18.0-513.11.1.lve.el8.x86_64 #1 SMP Thu Jan 18 16:21:02 UTC 2024 x86_64
Shell:	/usr/local/cpanel/bin/noshell
PHP binary:	/opt/alt/php80/usr/bin/php
PHP version:	8.0.30
php.ini used:	/opt/alt/php80/etc/php.ini
MySQL binary:	/bin/mysql
MySQL version:	mysql  Ver 8.0.37 for Linux on x86_64 (MySQL Community Server - GPL)
SQL modes:	
WP-CLI root dir:	phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir:	phar://wp-cli.phar/vendor
WP_CLI phar path:	/usr/local/cpanel/base
WP-CLI packages dir:	
WP-CLI cache dir:	/home/username/.wp-cli/cache
WP-CLI global config:	
WP-CLI project config:	
WP-CLI version:	2.9.0

Provide a possible solution

If you happen to have a suggestion on how to fix this bug, please tell us in here.

Just leave this section out if you don't know how to fix it.

Provide additional context/Screenshots

Add any other context about the problem here.

Screenshot 2024-12-09 at 09 51 10
Screenshot 2024-12-09 at 09 52 05

^^ would expect those 2 things to perform the same actions in WP Core

for reference, the wp-admin call is:

POST https://$domain/wp-admin/admin-ajax.php?plugin=elementor%2Felementor.php&slug=elementor&action=delete-plugin&_ajax_nonce=f105e819ec&_fs_nonce=&username=&password=&connection_type=&public_key=&private_key=

I see this call to uninstall_plugin: https://github.com/wp-cli/extension-command/blob/main/src/Plugin_Command.php#L1122

Which appears to call: https://github.com/WordPress/WordPress/blob/e8b5d1a702e9fbf6a1f225df6501ea2e517938ec/wp-admin/includes/plugin.php#L1289

Why is this used over the one that wp-admin uses delete_plugins()?

https://github.com/WordPress/WordPress/blob/e8b5d1a702e9fbf6a1f225df6501ea2e517938ec/wp-admin/includes/plugin.php#L904

That has additional logic to call hooks, etc:

https://github.com/WordPress/WordPress/blob/e8b5d1a702e9fbf6a1f225df6501ea2e517938ec/wp-admin/includes/plugin.php#L980

I'll submit a PR associated with this but not sure if people will consider it a breaking change since the logic technically changes

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions