1

I'm trying to figure out how to remove wordpress widgets added by plugins. I know there are a couple of plugins that do this, like https://wordpress.org/plugins/wp-remove-widgets/ and https://wordpress.org/plugins/post-lists-view-custom/, however I'd rather not use a seperate plugin for something that seems like such a small task.

Specifically, I'm trying to unregister the wordpress engine powered by widget that http://wpengine.com/ loads on the widgets page. I searched the source code and found the id of the widget is wpe_widget_powered_by

However, this does not work...

function my_widgets_init() {
    unregister_widget( 'wpe_widget_powered_by' );
}
add_action('widgets_init', 'my_widgets_init');  

So far the only thing I've been able to do effectively is use a combination of css/js to hide then remove the widget. I use css to hide it on page load and then remove it with jquery so there's not a blank gap where the widget is suppose to go.

function remove_wpe_powered_by_widget() {
?>
<style type="text/css">
    [id*="wpe_widget_powered_by"] {
display: none;
}
</style>
<script type="text/javascript">
    jQuery(document).ready(function($) {
    $("div[id$='wpe_widget_powered_by']").remove();
    });
</script>
<?php
}
add_action( 'admin_head', 'remove_wpe_powered_by_widget' );

While the above works, I know it's not the correct way to do things and I'm wondering why the first function doesn't work. The codex says to do the following.

<?php unregister_widget( $widget_class ) ?>

Maybe I'm not understanding what exactly $widget_class is. Is there a way to find the widget class without searching through the plugins code? Or do I already have the correct class and it's not working for another reason (perhaps a late hook or something).

I looked all through stackexchange and the internet and could only find ways to remove default widgets, but no one tells you how to remove widgets added by plugins. Sorry if I'm missing something and this is an obvious question.

1
  • I've contacted Wordpress Engine and they refused to provide an option to remove the widget. Jun 4, 2014 at 22:07

2 Answers 2

3

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.

2

Without being able to see the code used by WPEngine to do this, this is partly guess work.

The code you have to remove a widget is correct, however you can only unregister it after it's been registered. So try pushing your callback further back in the queue:

function wpse147602_deregister_widget() {
    unregister_widget( 'wpe_widget_powered_by' );
}
add_action( 'widgets_init', 'wpse147602_deregister_widget', 99999 );  

Is there a way to find the widget class without searching through the plugins code?

No.

... do I already have the correct class ...?

Maybe. Widgets are are a php child class of WP_Widget. The argument passed to unregister_widget() should be the name of that child class. E.g. [Widget Class] below

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

It is the same arugment that is passed to register_widget().

1
  • Thanks for your explanation. Helped me figure out the problem (indirectly). Jun 6, 2014 at 3:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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