wordpress

How to use upgrader_process_complete hook in WordPress to check for spcific plugin update


Is it possible to use upgrader_process_complete hook to check if only a specific plugin is updated?

I want to run a function every time a certain plugin is updated, for example warn the user about the changes or updating the database.


Solution

  • Yes, for it you can use this code:

    /**
     * This function runs when WordPress completes its upgrade process
     * It iterates through each plugin updated to see if ours is included
     * @param $upgrader_object Array
     * @param $options Array
     */
    function wp_upe_upgrade_completed( $upgrader_object, $options ) {
     // The path to our plugin's main file
     $our_plugin = plugin_basename( __FILE__ );
     // If an update has taken place and the updated type is plugins and the plugins element exists
     if( $options['action'] == 'update' && $options['type'] == 'plugin' && isset( $options['plugins'] ) ) {
      // Iterate through the plugins being updated and check if ours is there
      foreach( $options['plugins'] as $plugin ) {
       if( $plugin == $our_plugin ) {
        // Your action if it is your plugin
    
       }
      }
     }
    }
    add_action( 'upgrader_process_complete', 'wp_upe_upgrade_completed', 10, 2 );