Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I would like that every time any plugin previously activated, a function from my plugin be called, returning (at least) the name of the plugin, or something that allows to identify it (file name?).

I figured i could use the action hook "deactivated_plugin" but i'm wondering what information from the desactivated plugin is actually available?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

You can hook into the action 'deactivate_plugin' to get the plugin's base name and (as second parameter) if it was deactivated network wide.

See wp-admin/includes/plugin.php function deactivate_plugins().

There seems to be no hook for plugin deletion.

Sample logging code:

add_action( 'activated_plugin', 't5_plugin_logger', 10, 2 );
add_action( 'deactivated_plugin', 't5_plugin_logger', 10, 2 );

/**
 * Log plugin activations and deactivations.
 *
 * @param  string $plugin
 * @param  bool   $network_wide
 * @return void
 */
function t5_plugin_logger( $plugin, $network_wide )
{
    $log_size = 20;
    $log      = get_option( 't5_plugin_log', array () );

    // Remove the oldest entry.
    sizeof( $log ) > $log_size and array_shift( $log );

    $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin );
    $date_format = get_option( 'date_format' ) . ' · ' . get_option( 'time_format' );

    $log[] = array (
        'user'    => esc_html( wp_get_current_user()->display_name ),
        'plugin'  => $plugin_data['Name'],
        'network' => $network_wide ? '✔' : '',
        'time'    => date( $date_format, time() ),
        'action'  => 'deactivated_plugin' === current_filter() ? 'deactivated' : 'activated'
    );

    update_option( 't5_plugin_log', $log );
}

Now every plugin activation or deactivation will be added to the log option field. You can use this data anywhere. I made a small dashboard widget:

enter image description here

Download as plugin from GitHub: T5 Plugin Log

share|improve this answer
    
thanks for that. I think what i'm trying to do is impossible: i need my function to be called whenever any plugin is deactivated, not just mine. –  pixeline May 26 '12 at 23:48
    
@pixeline See my update, I have added some sample code. –  toscho May 27 '12 at 3:48
    
+1+ : bravisimo, maestro! olé! –  brasofilo May 27 '12 at 7:08
    
@toscho wow, amazing. Thank you for putting so much time into this. I'm building a plugin that will take care of plugins but also for themes and WPcore changes. If you are interested, maybe we partner up? –  pixeline May 27 '12 at 8:23
    
Yeah, would be cool. :) There is also an old plugin Last Edits which I wanted to refresh and extend for … years. Tracking system changes would be enough for a start, I'm also interested in changes to PHP, MySQL and Apache because web hoster do this usually rather silent. –  toscho May 27 '12 at 8:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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