Remove/Unregister or hide a widget added by a plugin

Thanks Stephen for helping me wrap my head around this today. Even though it’s something so small, I absolutely hate when develpers bloat plugins with crap you don’t want. I was very frustrated after WPengine, telling me it can’t be removed and that if I really want to remove it than I need to find the function in the MU-Plugins folder and delete it. Kind of sad hearing from a “WordPress” hosting company that their solution is to delete the code, when that is the opposite of how WordPress has been built to operate.

Anyway, I managed to make a debugging function that prints the registered widgets. Here it is:

function show_widget_classes() {
    global $wp_registered_widgets; 
    $widgets = array(); 
    if(is_array($wp_registered_widgets)){ 
        foreach($wp_registered_widgets as $widg){ 
            if(!empty($widg['callback'])){
                if(!empty($widg['callback'][0])){  
                    $class = get_class($widg['callback'][0]);
                    if(!array_key_exists($class, $widgets)){
                        $widgets[$class] = $widg['callback'][0]->name;
                    }
                }
            }
        }
    }
foreach($widgets as $widget_class  => $widget_title ){
        echo '<pre>'; print_r( $widget_class ); echo '</pre>';
    }
}
add_action( 'admin_notices', 'show_widget_classes' ); 

Ironically wpengine_powered_by managed to be the only one that didn’t show up. At first I thought it was a conflict with the wpengine-widget loading too early in the mu-plugins folder. However, after re-reading your response about the class:

class [Widget Class] extends WP_Widget{
 ...
}

I did a quick grep search through the wpengine-common plugins folder for extends WP_Widget:

grep -nr 'extends WP_Widget' /wpengine-common  

And Nothing! So to get the the point I eventually just searched the plugin and realized they were using an older function that registers a widget that only words in one instance. Using this method, a class is not used.

http://codex.wordpress.org/Function_Reference/wp_register_sidebar_widget

So instead of this:

unregister_widget

You would use this:

wp_unregister_sidebar_widget

You can also use:

wp_unregister_widget_control for functions that add controls with the wp_register_widget_control

Here is the final function removing the widget for anyone else annoyed with wpengines “affiliate” widget.

<?php
/**
* Plugin Name: Remove WpEngine Bloat
* Plugin URI: http://wordpress.stackexchange.com/questions/147602/
* Description: Remove the affiliate links widget from the Admin Widgets Page.
* Version: 1.0
* Author: Bryan WIllis
* Author URI: 
* License:
*/

function remove_unwanted_wpe_bloat_affiliate_widget_wpse_147602() {
// unregister the widget and its control
   wp_unregister_sidebar_widget('wpe_widget_powered_by');
}
add_action('widgets_init', 'remove_unwanted_wpe_bloat_affiliate_widget_wpse_147602', 1);

I will also update the debugging function when I get a chance and make sure ALL the widgets are outputted, not just the ones that use class.

Alternatively, download https://wordpress.org/plugins/post-lists-view-custom/ which lets you remove widgets, however it had some flaws and also adds a whole lot of other stuff if you’re just trying to remove widgets.