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).
<?php
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.
Latest posts by WordPrax Ltd. (see all)
- WordPress 4.9 Update Emphasizes Ease-of-Workflow - July 5, 2022
- What are the Steps to Convert a PSD file into a WordPress Theme? - September 12, 2017
- Reasons for Needing a Professional Genesis Coder for Converting PSD Design to WordPress - July 18, 2017