-1

NOTE: Please note that, it's not the Widget Visibility issue — I'm taking about front-end.

To supply a project to my customer, I need to hide/delete the Jetpack Extra Sidebar widgets from the admin panel - basically I need a control to hide them specifically or can activate them again, if necessary.

As I can hide (Source^) the default widgets with code like:

// unregister default widgets
 function unregister_default_widgets() {
     unregister_widget('WP_Widget_Pages');
     unregister_widget('WP_Widget_Calendar');
     unregister_widget('WP_Widget_Archives');
 }
 add_action('widgets_init', 'unregister_default_widgets', 11);

I'm using JetPack for many purposes, but don't want to avail all of the JetPack Extra Sidebar Widgets to the Editor account.

How can I hide/delete them from the back-end?

1 Answer 1

2

This is not an alteration to the Plugin itself, it's just a way to revert what is already done. So, to Activate a plugin there's a WordPress function:

register_widget('Class_of_the_Widget');

As we can unregister the default plugins using the unregister_widget() function, we can use the same function for JetPack plugin's widgets too.

Get into the /wp-content/plugins/jetpack/modules/widgets, you will get most of the Jetpack plugins are registered from there, and fortunately they are sorted with their name (as of Version 2.9.3). Suppose, facebook-likebox.php file contains the widget information of the 'Jetpack Facebook Like Box' widget.

So, the rest is the same as the default widget-hiding, call the same thing:

 // unregister jetpack widgets
 function wpse140512_unregister_jetpack_widgets() {
     //hide from the 'Editor' only
     if ( is_admin() && current_user_can('editor') ) {
         unregister_widget('WPCOM_Widget_Facebook_LikeBox'); //Facebook Likebox
         unregister_widget('Jetpack_Gravatar_Profile_Widget'); //Gravatar Profile
     }
 }
 add_action('widgets_init', 'wpse140512_unregister_jetpack_widgets', 11);

...and so on. But how could I get those 'Widget Classes'?

Open all those widget files and get the name from the register_widget() function. As the widget is registered with a Class, you can unregister them with the same Class.

The better way to find them all in an instance is to use a Smart Text Editor's Find in Folder/Files feature. Suppose:

  • In Sublime Text: Use the Menu bar's Find » Find in Files... (Ctrl+Shift+F), then find register_widget and browse the jetpack folder under /plugins in your /wp-content, and then find them all at once.

But...

This process is worthy for almost all the cases, but some widgets are not registered using the register_widget() function, in that case, such widgets are registered using the wp_register_sidebar_widget() function. For those, you have to unregister them using the wp_unregister_sidebar_widget() function, but this time you have pass the Widget ID inside the function rather than the Widget Class.

So, let's travel! :)

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