2

I'm currently working on my first in-depth custom template for Wordpress, utilizing the functionality of the customizer.

I am able to create options and also save them but somehow i am unable to push them to the page itself. The code i'm using:

//Do we want our template to be boxed or wide?
function cbTheme_customize_register($wp_customize){
    //  =============================
    //  = Add section               =
    //  =============================
    $wp_customize->add_section('cbTheme_general_settings', array(
        'title'    => __('General Settings', 'cbTheme'),
        'description' => 'Determines wether your site is boxed, or has 100% width',
        'priority' => 10,
    ));
    //  =============================
    //  = Add setting               =
    //  =============================
    $wp_customize->add_setting('cbTheme_site_settings[sitewidth]', array(
        'default'        => 'value1',
        'capability'     => 'edit_theme_options',
        'type'           => 'option',
    ));
    $wp_customize->add_control('cbTheme_site_width', array(
        'label'      => __('Site width', 'cbTheme'),
        'section'    => 'cbTheme_general_settings',
        'settings'   => 'cbTheme_site_settings[sitewidth]',
        'type'       => 'radio',
        'choices'    => array(
                'value1' => 'Wide',
                'value2' => 'Boxed',
        ),
    ));
}
add_action( 'customize_register', 'cbTheme_customize_register' );

I am using this in my header.php(first lines) to try and retrieve the options (as they do save).

//GET ALL SETTINGS FROM CUSTOMIZER
function cbTheme_customize_css($wp_customize){
    $sitewidth = get_theme_mod('cbTheme_site_settings[sitewidth]');
}
add_action( 'wp_head', 'cbTheme_customize_css');

Any advice/feedback on this? I assume the faulty code is the last part. I tried following the codex, but i am lost at the data retrieval process.

SliQz
  • 41
  • 1
  • 3

2 Answers2

2

Okay, i have been in search of an answer for forever now, and found a workaround for now. If anyone else has a better answer please provide me/other users with it. The workaround i used is to be applied in the second part of the code, where you retrieve the theme options.

To fix the issue, replace this

$sitewidth = get_theme_mod('cbTheme_site_settings[sitewidth]');

with this

extract(get_option('cbTheme_site_settings'));

This will provide you with variables that have the array-key as variable name, straight from the options-table.

SliQz
  • 41
  • 1
  • 3
0
//Do we want our template to be boxed or wide?
function cbTheme_customize_register($wp_customize){
    //  =============================
    //  = Add section               =
    //  =============================
    $wp_customize->add_section('cbTheme_general_settings', array(
        'title'    => __('General Settings Test', 'cbTheme'),
        'description' => 'Determines wether your site is boxed, or has 100% width',
        'priority' => 10,
    ));
    //  =============================
    //  = Add setting               =
    //  =============================
    $wp_customize->add_setting('cbTheme_site_settings', array(
        'default'        => 'value1',
        'capability'     => 'edit_theme_options',
    ));
    $wp_customize->add_control('cbTheme_site_width', array(
        'label'      => __('Site width', 'cbTheme'),
        'section'    => 'cbTheme_general_settings',
        'settings'   => 'cbTheme_site_settings',
        'type'       => 'radio',
        'choices'    => array(
                'value1' => 'Wide',
                'value2' => 'Boxed',
        ),
    ));
}
add_action( 'customize_register', 'cbTheme_customize_register' );

And in your index.php file or whether you want to use the settings value simply follow the below code::

$wideClass = get_theme_mod('cbTheme_site_settings', 'value1') == 'value1' ? 'wide' : 'boxed';

echo $wideClass;