钩子文档

upgrader_process_complete

💡 云策文档标注

概述

upgrader_process_complete 是一个 WordPress 动作钩子,在升级器(如插件、主题、核心或语言包)的下载过程完成时触发。它允许开发者在升级操作后执行自定义代码,但需注意在插件自身升级时可能运行旧版本。

关键要点

  • 触发时机:当 WP_Upgrader、Theme_Upgrader、Plugin_Upgrader、Core_Upgrader 或 Language_Pack_Upgrader 实例的下载过程结束时触发。
  • 参数:接受两个参数:$upgrader(WP_Upgrader 实例)和 $hook_extra(包含更新数据的数组,如 action、type、bulk、plugins、themes、translations 等)。
  • 注意事项:如果插件使用此钩子且自身正在升级,钩子将运行插件的旧版本代码,可能导致意外行为。
  • 相关类:与 Language_Pack_Upgrader::bulk_upgrade()、Core_Upgrader::upgrade()、Plugin_Upgrader::bulk_upgrade()、Theme_Upgrader::bulk_upgrade() 和 WP_Upgrader::run() 等方法关联。
  • 版本历史:自 WordPress 3.6.0 引入,4.6.0 添加了 $translations 参数。

代码示例

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

function my_upgrade_function( $upgrader_object, $options ) {
    $current_plugin_path_name = plugin_basename( __FILE__ );

    if ( $options['action'] == 'update' && $options['type'] == 'plugin' ) {
        foreach ( $options['plugins'] as $each_plugin ) {
            if ( $each_plugin == $current_plugin_path_name ) {
                // 自定义代码
            }
        }
    }
}

注意事项

  • 在插件升级时使用此钩子需谨慎,因为可能执行旧版本代码,建议通过瞬态(transient)或其他机制处理升级后逻辑。
  • 参数 $hook_extra 的结构随更新类型变化,例如插件更新时包含 plugins 数组,主题更新时包含 themes 数组。

📄 原文内容

Fires when the upgrader process is complete.

Description

See also ‘upgrader_package_options’.

Parameters

$upgraderWP_Upgrader
WP_Upgrader instance. In other contexts this might be a Theme_Upgrader, Plugin_Upgrader, Core_Upgrade, or Language_Pack_Upgrader instance.
$hook_extraarray
Array of bulk item update data.

  • action string
    Type of action. Default 'update'.
  • type string
    Type of update process. Accepts 'plugin', 'theme', 'translation', or 'core'.
  • bulk bool
    Whether the update process is a bulk update. Default true.
  • plugins array
    Array of the basename paths of the plugins’ main files.
  • themes array
    The theme slugs.
  • translations array
    Array of translations update data.

    • language string
      The locale the translation is for.
    • type string
      Type of translation. Accepts 'plugin', 'theme', or 'core'.
    • slug string
      Text domain the translation is for. The slug of a theme/plugin or 'default' for core translations.
    • version string
      The version of a theme, plugin, or core.

More Information

The upgrader_process_complete action hook is run when the download process for a plugin install or update finishes.

Use with caution: When you use the upgrader_process_complete action hook in your plugin and your plugin is the one which under upgrade, then this action will run the old version of your plugin.

Source

do_action( 'upgrader_process_complete', $this, $options['hook_extra'] );

Changelog

Version Description
4.6.0 $translations was added as a possible argument to $hook_extra.
3.7.0 Added to WP_Upgrader::run().
3.6.0 Introduced.

User Contributed Notes

  1. Skip to note 5 content

    Example migrated from Codex:

    Do stuff if the current plug-in is being updated.

    add_action( 'upgrader_process_complete', 'my_upgrade_function',10, 2);
    
    function my_upgrade_function( $upgrader_object, $options ) {
        $current_plugin_path_name = plugin_basename( __FILE__ );
    
        if ($options['action'] == 'update' && $options['type'] == 'plugin' ) {
           foreach($options['plugins'] as $each_plugin) {
              if ($each_plugin==$current_plugin_path_name) {
                 // .......................... YOUR CODES .............
    
              }
           }
        }
    }

  2. Skip to note 6 content

    Example migrated from Codex:

    This short plugin demonstrates how to display a notice to the user when they update the plugin. It displays a different notice when they first install the plugin:

    https://catapultthemes.com/wordpress-plugin-update-hook-upgrader_process_complete/</a>
    Description: Just an example of using upgrader_process_complete
    Version: 1.0.0
    Author: Catapult Themes
    Author URI: <a href="https://catapultthemes.com/" rel="nofollow ugc">https://catapultthemes.com/</a>
    Text Domain: wp-upe
    Domain Path: /languages*/
    
    // Exit if accessed directly
    if ( ! defined( 'ABSPATH' ) ) {
     exit;
    }
    
    /**
     * 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 ) {
        // Set a transient to record that our plugin has just been updated
        set_transient( 'wp_upe_updated', 1 );
       }
      }
     }
    }
    add_action( 'upgrader_process_complete', 'wp_upe_upgrade_completed', 10, 2 );
    
    /**
     * Show a notice to anyone who has just updated this plugin
     * This notice shouldn't display to anyone who has just installed the plugin for the first time
     */
    function wp_upe_display_update_notice() {
     // Check the transient to see if we've just updated the plugin
     if( get_transient( 'wp_upe_updated' ) ) {
      echo '<div class="notice notice-success">' . __( 'Thanks for updating', 'wp-upe' ) . '</div>';
      delete_transient( 'wp_upe_updated' );
     }
    }
    add_action( 'admin_notices', 'wp_upe_display_update_notice' );
    
    /**
     * Show a notice to anyone who has just installed the plugin for the first time
     * This notice shouldn't display to anyone who has just updated this plugin
     */
    function wp_upe_display_install_notice() {
     // Check the transient to see if we've just activated the plugin
     if( get_transient( 'wp_upe_activated' ) ) {
      echo '<div class="notice notice-success">' . __( 'Thanks for installing', 'wp-upe' ) . '</div>';
      // Delete the transient so we don't keep displaying the activation message
     delete_transient( 'wp_upe_activated' );
     }
    }
    add_action( 'admin_notices', 'wp_upe_display_install_notice' );
    
    /**
     * Run this on activation
     * Set a transient so that we know we've just activated the plugin
     */
    function wp_upe_activate() {
     set_transient( 'wp_upe_activated', 1 );
    }
    register_activation_hook( __FILE__, 'wp_upe_activate' );

You must log in before being able to contribute a note or feedback.