2

Any hook is available on before the plugin update action ? i searched in hooks library . What is the exact hook? . Doing some stuff before updating plugin/theme .

1 Answer 1

1

Here are two untested ideas:

Idea #1:

If you want to hook before an instance of the Plugin_Upgrader class is created with:

$upgrader = new Plugin_Upgrader( ... )

where the upgrades are activated with:

$upgrader->upgrade($plugin);          // for a single plugin upgrade
$upgrader->bulk_upgrade( $plugins );  // for bulk plugins upgrades

then you could try:

/**
 * Idea #1 - Prior to plugin (bulk or single) upgrades
 */

add_action( 'admin_action_upgrade-plugin', function() {
    add_action( 'check_admin_referer', 'wpse_referer', 10, 2 );
});

add_action( 'admin_action_bulk-update-plugins', function() {
    add_action( 'check_admin_referer', 'wpse_referer', 10, 2 );
});

function wpse_referer( $action, $result )
{
    remove_action( current_filter(), __FUNCTION__, 10 );

    if( $result )
    { 
        if( 'bulk-update-plugins' === $action )
        {
            // Do stuff before we run the bulk upgrade
        } 
        elseif( 'upgrade-plugin_' === substr( $action, 0, 15 )  )
        {
            // Do stuff before we run the single plugin upgrade
        }
    }
}

Idea #2:

If you want to hook deeper into the Plugin_Upgrader object, then you could try for example to hijack the upgrader_pre_install filter for the single upgrade case:

/**
 * Idea #2 - Prior to a single plugin upgrade
 */

add_action( 'admin_action_upgrade-plugin', function() {
    add_action( 'upgrader_pre_install', 'wpse_upgrader_pre_install', 99, 2 );
});

function wpse_upgrader_pre_install( $return, $plugin )
{
    if ( ! is_wp_error( $return ) ) 
    {
        // Do stuff before we run the single plugin upgrade
    }
    remove_action( current_filter(), __FUNCTION__, 99 );
    return $return;
}

Similarly you could hijack the upgrader_clear_destination filter for the bulk upgrades:

/**
 * Idea #2 - Prior to bulk plugins upgrade
 */

add_action( 'admin_action_bulk-update-plugins', function() {
    add_action( 'upgrader_clear_destination', 'wpse_upgrader_clear_destination', 99, 4 );
});

function wpse_upgrader_clear_destination( $removed, $local_destination, 
    $remote_destination, $plugin )
{
    if ( ! is_wp_error( $removed ) ) 
    {
        // Do stuff before we run the bulk plugins upgrades
    }
    remove_action( current_filter(), __FUNCTION__, 99 );
    return $removed;
}

Hopefully you can adjust this to your needs, if it works ;-)

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.