0

I'd like to be able to automatically uninstall certain plugins if they're detected (specifically Akismet and Hello Dolly), either by writing another plugin to do so or via my theme's functions.php file. Is that possible?

2 Answers 2

1

Sure, just call the delete_plugins() function, found in wp-admin/includes/plugin.php - you have to manually require() it.

1

This'll deactivate and uninstall plugins you specify in the the $to_deactivate and $to_delete arrays.

NOTE: Due to the funky way WordPress activates plugins, we can't use an activation hook to do our work. Instead we check for the existence of an option, so as to ensure it only runs once per activation!

<?php

/**
 * Plugin Name: My Remove Plugins
 * Version: 0.1
 */

/**
 * Deactivate and/or delete plugins.
 */
function my_remove_plugins()
{
    $to_deactivate = array( 'Anti-Spam Email' );
    $to_delete     = array( 'Akismet' );

    $deactivate_stack =
    $delete_stack     = array();

    foreach ( get_plugins() as $plugin => $data ) {
        if ( in_array( $data['Name'], $to_deactivate ) )
            $deactivate_stack[] = WP_PLUGIN_DIR . "/$plugin";

        elseif ( in_array( $data['Name'], $to_delete ) )
            $delete_stack[] = WP_PLUGIN_DIR . "/$plugin";
    }

    if ( !empty($deactivate_stack) || !empty($delete_stack) )
        deactivate_plugins( array_merge( $deactivate_stack, $delete_stack ), true );

    if ( !empty($delete_stack) )
        $result = delete_plugins( array_map( 'plugin_basename', $delete_stack ) );

    update_option( 'my_remove_plugins', 1 );

    // refresh, otherwise deleted plugins will still show on first load
    wp_safe_redirect( $_SERVER['REQUEST_URI'] );
    exit;
}

if ( is_admin() && !get_option( 'my_remove_plugins' ) )
    add_action( 'admin_init', 'my_remove_plugins' );

/**
 * Clear out our option on deactivation.
 */
function my_remove_plugins_deactivate()
{
    delete_option( 'my_remove_plugins' );
}
register_deactivation_hook( __FILE__, 'my_remove_plugins_deactivate')
?>

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.