Design & Development
Published April 19th, 2017 by

How to Deactivate WordPress Widgets using Code

While creating or getting a fresh WordPress theme, you might encounter some situations when you want to build a certain functionality that gets activated whenever the theme has been activated on your WordPress powered website. If you ever had closely examined the WordPress CMS, then you would have noticed some certain qualities that are typical of WP templates and will surely help you speed up the process of customization and so on.

To gain a better understanding of this concept, imagine yourself in a situation in which you are working on custom WordPress theme development process which contains a specific place where you wish to put different widgets. So, while activating the template, you would definitely want your web layout to look, clean and impressive, without getting disturbed by the undesirable elements.

In this article, you will come across simple ways through which you can quickly deactivate the widgets using the coding of your WordPress based site. Well, the best thing is that this process of deactivating the widget is extremely simple and easy to learn, even the amateurs can also use this for their project. So, without further ado, let’s discuss this procedure in detail.

How to Programmatically Deactivate WordPress Widgets

The World Wide Web is full of several tutorials, articles and blogs that would give you a clear understanding of the process how to unregister the widget in WordPress. Although, this unregistering and deactivation of the widget are completely different things and that’s the primary thing you need to understand.

This whole process of deactivation is quite easy to follow, especially when you are developing or editing the theme. All you need to is to simply write a certain set of code (you can also copy paste it if you are not in the mood of doing scripting).

add_action('after_setup_theme', 'theme_remove_widgets' );/***When the theme is activated, all of the active widgets are deactivated.*/
function theme_remove_widgets() {
if( ! get_option( 'bottom_right_widgets' ) ) {
update_option('top_right_widgets', array() );
update_option('bottom_right_widgets', true );

Once you are done with the coding part, it’s time to check if the top_right_widgets flag exists and its value is set to “True”. If not, you should probably start updating the options and if you have already done that, then don’t need to go further. In case there’s no flag existing, then set the bottom_right_widgets option equal to an array and then set the value of top_right_widgets to true.

It will take you hardly 15-20 minutes to finish this whole process. See, it wasn’t that hard to deactivate a widget on your theme. The activated widgets are usually put in serial order in the bottom_right_widgets option, that you will find in the Options Column. This way you would be able to clear up all things which you stored before. The primary reason you are going through all this is that the acme_cleared_widgets option is because you don’t have to clear widgets each and every time the after_setup_theme action is initiated.

This way, you would be able to focus more on clearing the widgets in one go. Additionally, as you know that it’s a destructive function, a setting can also be provided to the users which will trigger the function each time the user performs some sort of action in the dashboard and should be requiring a confirmation.

Summing Up

After reading the above mentioned content, you would have a complete understanding how you can deactivate the widgets using a set of code in the WordPress-based sites.

WordPrax Ltd.

WordPrax Ltd.

WordPrax Ltd is a leading WordPress web site development company across the world. If you're seeking to hire brilliant WordPress conversion companies, then WordPrax is the one stop solution for you. Our experience in handling complex projects in complex environment is a critical differentiator, which means you can depend on us.
WordPrax Ltd.

Our rankings are completely independent, transparent, and community driven; they are based on user reviews and client sentiment. These design & development companies had to earn their way up and didn't just pay their way up.

View Rankings of Best Design & Development Companies