In functions.php
write this code
function my_optionally_widgets()
{
$option = get_options('wantwidget');
if($option == 'yes')
{
register_widget('mywidget');
/* add other widgets for registration here */
}
}
add_action('widgets_init', 'my_optionally_widgets');
function my_optionally_sidebars()
{
$option = get_options('wantsidebar');
if($option == 'yes')
{
register_sidebar($args);
/* add other sidebars for registration here */
}
}
add_action('init', 'my_optionally_sidebars');
Remember that Widget Areas
and Sidebars
are the exact same thing. But the code above shows you how to enable and disable widgets and sidebars from appearing in the dashboard.
And this is how you display them on the front-end:
$option = get_options('wantsidebar');
if($option == 'yes')
{
dynamic_sidebar($index);
}