While developing or getting a new WordPress theme, you probably have encountered situations when you wanted to have a certain kind of functionality get activated whenever the theme has been activated on the website. If you’ve closely examined WordPress , then you would have observed that there are certain qualities that are typical of WordPress themes and will help speed up the process of customization and so on. For a better understanding of the concept, let’s say you are working on niche themes, which contain specific sets of widgetized areas.
So, while activating the theme, you would want your layout to look clean and impressive, without getting disturbed by undesirable elements. In this article, we are going to discuss about some simple ways through which you can easily deactivate the widgets programmatically in your WordPress powered website. The process is extremely simple and easy to learn. So, without further ado, let’s discuss it in detail. How to Programmatically Deactivate WordPress Widgets The Internet is provided with a lot of articles that give a clear understanding of the process of unregistering a widget in WordPress.
Although, the process unregistering and deactivating a widget is entirely different and that’s what we need to understand. The process of deactivating the widget is relatively easy to follow (especially when it comes to the theme deactivation). All we need to do is simply right a certain set of code add_action(‘after_setup_theme’, ‘acme_remove_default_widgets’ );***When the theme is activated, all of the active widgets are deactived.**@since . . *functionacme_remove_default_widgets() if( ! get_option( ‘acme_cleared_widgets’ ) ) update_option(‘sidebars_widgets’, array() ); update_option(‘acme_cleared_widgets’, true ); Now, check if the acme_cleared_widget flag exists and it is set to the value ‘True’.
If not, start updating the options; if they are already updated then no need to go further. If there is no flag existing, then set the sidebars_widgets option equal to an array and then set the value of acme_cleared_widget to true. See, it wasn’t that hard. Right The activated widgets are put in a serial order in the sidebars_widgets option, which you’ll find the options column. https://infonetic.wordpress.com/ , you’ll be able to clear everything which has been stored before. The reason why we are going through this is that they are guided by conditions and has the acme_cleared_widgets option so that there is no need for us to clear widgets each and every time the after_setup_theme action is being activated.