Below is the code you need. Your issue could be that you’re not first setting up the variable $display_options to contain your options, or that you not asking if the option is set.
add_action('widgets_init', 'arphabet_widgets_init');
function arphabet_widgets_init() {
$options = get_option('muffin_options');
if(isset($display_options['header_layout']) && $display_options['header_layout'] == '1') {
register_widget('arphabet_widget_1');
}
if(isset($display_options['header_layout']) && $display_options['header_layout'] == '1') {
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>',
) );
}
}
Also, it’s worth noting that you can set up default settings for your option. This avoids the need to ask if a an option setting is set each time you use it – so you wouldn’t need to use isset($display_options['show_my_menu'])
.
To do this, adapt the following code – the whole thing is included for clarity.
add_action( 'admin_init', 'arphabet_options_init' );
function arphabet_options_init() {
register_setting( 'arphabet_options', 'arphabet_options' ); // Register your setting
arphabet_options_defaults(); // Set up defaults in the function named 'arphabet_options_defaults'
}
function arphabet_options_defaults() {
$update_options= get_option('arphabet_options'); // Store options array data in a variable
$update_options['show_my_widget'] = true; // Add this key/value to the $update_options array: 'show_my_widget' => true
$update_options['show_my_menu'] = true; // Same as above, but for show_my_menu
add_option('arphabet_options', $display_options); // Add these settings to your option, if they don't already exists (use update_option to replace settings)
}
The part worth noting is add_option. This merges the key/value pairs from $display_options with the array data already stored in the arphabet_options option – basically, it adds settings to the database. If won’t replace existing settings though; use update_option for this.
If you’d like to know more about using setting/updating options, check out the links below – although they don’t really cover using arrays with options. So, I created a Pastebin showing a few useful snippets I use often.
https://codex.wordpress.org/Function_Reference/add_option
https://codex.wordpress.org/Function_Reference/update_option