0

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!

0

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.

http://pastebin.com/NM4Xk4BR

https://codex.wordpress.org/Function_Reference/add_option https://codex.wordpress.org/Function_Reference/update_option

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.