3

Is there a WordPress hook I can use to run a function anytime any plugin is updated?

I am trying to write to a log file with the logged in users IP every time something is updated so I can track activity.

1 Answer 1

7

Maybe upgrader_process_complete() action hook.

Fires when the bulk upgrader process is complete.

On line 761 of wp-admin/includes/class-wp-upgrader.php

From source:

/**
 * Fires when the upgrader process is complete.
 *
 * See also {@see 'upgrader_package_options'}.
 *
 * @since 3.6.0
 * @since 3.7.0 Added to WP_Upgrader::run().
 * @since 4.6.0 `$translations` was added as a possible argument to `$hook_extra`.
 *
 * @param WP_Upgrader $this WP_Upgrader instance. In other contexts, $this, might be a
 *                          Theme_Upgrader, Plugin_Upgrader, Core_Upgrade, or Language_Pack_Upgrader instance.
 * @param array       $hook_extra {
 *     Array of bulk item update data.
 *
 *     @type string $action       Type of action. Default 'update'.
 *     @type string $type         Type of update process. Accepts 'plugin', 'theme', 'translation', or 'core'.
 *     @type bool   $bulk         Whether the update process is a bulk update. Default true.
 *     @type array  $plugins      Array of the basename paths of the plugins' main files.
 *     @type array  $themes       The theme slugs.
 *     @type array  $translations {
 *         Array of translations update data.
 *
 *         @type string $language The locale the translation is for.
 *         @type string $type     Type of translation. Accepts 'plugin', 'theme', or 'core'.
 *         @type string $slug     Text domain the translation is for. The slug of a theme/plugin or
 *                                'default' for core translations.
 *         @type string $version  The version of a theme, plugin, or core.
 *     }
 * }
 */
do_action( 'upgrader_process_complete', $this, $options['hook_extra'] );

This below is just the example from codex (it is noted as untested there, btw), and I edited it to show a possible instance of all plugin updates:

add_action( 'upgrader_process_complete', 'my_upgrate_function',10, 2);

function my_upgrate_function( $upgrader_object, $options ) {


    if ($options['action'] == 'update' && $options['type'] == 'plugin' ) {

       foreach($options['packages'] as $each_plugin) {

             // do stuff

       }//end foreach
    }//end if
}//end my_upgrade_function() 
3
  • This answer is correct! thank you!! :) Here is tested code. gist.github.com/ve3/596bbd26b3664f4a006620d06b52f07c
    – vee
    Commented Jan 4, 2018 at 12:55
  • @vee this gist is no longer available, can you share your solution?
    – Rain Man
    Commented Apr 6, 2020 at 14:32
  • @RainMan Sorry, I don't have that code anymore but I'm sure that this answer by hwl work.
    – vee
    Commented Apr 6, 2020 at 21:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.