0

Is it possible to register a widget area and sidebars when an option has been selected and saved in a theme options panel?

I am using a framework called redux but am not too sure if this can be accomplished as I am unsure on whether there is a hook that would be available to register the widget area / menu.

0

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);
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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