这将停用并卸载您在$to_deactivate
和$to_delete
阵列。
注意:由于WordPress激活插件的方式很古怪,我们不能使用激活挂钩来完成我们的工作。相反,我们检查选项是否存在,以确保每次激活只运行一次!
<?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\')
?>