I have theme options/settings page and options/settings fields set as outlined here: http://code.tutsplus.com/tutorials/the-complete-guide-to-the-wordpress-settings-api-part-2-sections-fields-and-settings--wp-24619
What I would like to know, is how to register both 1) widgets and 2) menus based on specific theme option/setting field. Note: I want to 'register' the menu/widget, not merely display it. The logic being that in the back-end, a user may be confused by having superfluous widgets/menus.
To be concrete, I'm using theme settings to offer admin users the ability to set different header types, let's call them '1,' '2,' and '3.'
The code in my header.php file is (I'm pulling template parts via 'get_template_part'):
<?php if($display_options['header_layout'] == '1'): ?>
<?php get_template_part( 'partials/header', 'left' ); ?>
<?php endif; ?>
<?php if($display_options['header_layout'] == '2'): ?>
<?php get_template_part( 'partials/header', 'center' ); ?>
<?php endif; ?>
<?php if($display_options['header_layout'] == '2'): ?>
<?php get_template_part( 'partials/header', 'right' ); ?>
<?php endif; ?>
As mentioned, the above code, [header_layout] utilises one of three options from a settings field. Based on this field, I'm loading different template parts, each of which would - ideally - have different menus / widgets.
Where I'm getting hung up is here by trying to conditionally register menus/widgets, examples (this is in a separate widgets.php file):
function arphabet_widgets_init() {
if($display_options['header_layout'] == '2') {
register_sidebar( array(
'name' => 'Header Widget 2',
'id' => 'widget_header_2',
'before_widget' => '<div class="nav widget">',
'after_widget' => '</div>',
'before_title' => '<h2 class="widgettitle">',
'after_title' => '</h2>',
) );
};
}
add_action( 'widgets_init', 'arphabet_widgets_init' );
I'm thinking that somehow the way I'm passing the variables from $display_options is wrong. More than happy to clarify if need be and thanks in advance for any ideas!