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'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?

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

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

share|improve this answer
add comment

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')
?>
share|improve this answer
add comment

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.