• Resolved maxlefou

    (@maxlefou)


    Hi. I’m really sorry if it’s a double post but i haven’t found anything relelvant in the search or even on searches outside wordpress.

    I’m recoding a theme to make it comply with wordpress standards and also to get rid of options-framework which is outdated, non developed anymore and kinda useless now.
    I could make sections and settings appear in the customizer… When I change the settings, I can see the changes are applied in wordpress database, in wp-options, in “themename”… But nothing happens on the theme itself, either in live preview or when saved. It does that only on custom settings of the theme. The basic settings (like changing title and stuff) are working fine. Also, no error is reported with the debug mode.

    I’m pretty sure there’s something missing, but the tutorials and documentation didn’t give me any clues. I’m kinda new in this.

    Here’s the codes used for one of them:

    functions.php:
    require( get_template_directory() . '/inc/customizer.php' );

    /inc/customizer.php:

    function my_theme_customize_register( $wp_customize ) {
    	$wp_customize->get_setting( 'blogname' )->transport         = 'postMessage';
    	$wp_customize->get_setting( 'blogdescription' )->transport  = 'postMessage';
    	$wp_customize->get_setting( 'header_textcolor' )->transport = 'postMessage';
    
    $wp_customize->add_section( 'my_theme_header', array(
    		'title' => __( 'General', 'my-theme' ),
    		'priority' => 200
    	));
    
    	/* Search Box */
    	$options['g_search_box_id'] = array( "name" => __( "Display search box?", "my-theme" ),
    						"desc"    => __( "Display search box in the header?", "my-theme" ),
    						"id"      => "g_search_box_id",
    						"type"    => "checkbox",
    						"std"     => 1);
    	$wp_customize->add_setting( 'my_theme[g_search_box_id]', array(
    			'default' => 1,
    			'type' => 'option'
    	) );
    	$wp_customize->add_control( 'my_theme_g_search_box_id', array(
    			'label' => $options['g_search_box_id']['name'],
    			'section' => 'my_theme_header',
    			'settings' => 'my_theme[g_search_box_id]',
    			'type' => $options['g_search_box_id']['type'],
    			'priority' => 11
    	) );
    // ....
    add_action( 'customize_register', 'my_theme_customize_register' );

    the mattering part in header.php:

    <?php if ( get_theme_mod('g_search_box_id', '1') === '1') { ?>
    	          <div id="top-search">
    	            <form method="get" action="<?php echo home_url(); ?>/">
    	              <input type="text" name="s"  class="input-search" /><input type="submit" value="" id="submit">
    	            </form>
    	          </div>
    	        <?php } ?>

    In case you need, the full source code of the theme is here as it is.
    Thanks for your help

Viewing 5 replies - 1 through 5 (of 5 total)
  • You have defined the setting as an array, and of type option. But you are using get_theme_mod to retrieve it and the subscript name instead of the full setting name.

    Thread Starter maxlefou

    (@maxlefou)

    Well I’ve tried the following:

    get_theme_mod('my_theme_g_search_box_id', '1')
    get_theme_mod('g_search_box_id', '1')
    get_theme_mod('my_theme[g_search_box_id]', '1')
    get_option('my_theme_g_search_box_id', '1')
    get_option('g_search_box_id', '1')
    get_option('my_theme[g_search_box_id]', '1')

    But none of them has worked. The div is still visible no matter the option set and no errors reported.
    Is there one I missed or something else?

    Thanks

    • This reply was modified 5 years, 3 months ago by maxlefou.
    Thread Starter maxlefou

    (@maxlefou)

    Also tried the same by replacing 'type' => 'option' by 'type' => 'theme_mod' but still the same.

    Since this is a theme, I suggest you use theme_mod, and it’s the default anyway.
    Put the same name in for setting and control.
    If you use an array for the setting name, it will be saved as an array for you, but you still need to retrieve it by the array name, not with the subscript.
    So if you use
    add_setting( 'google_fonts[body]',
    add_control( 'google_fonts[body]',
    you need to retrieve it with
    array_merge( $defaults, get_theme_mod( 'google_fonts', $defaults ) )
    where your default value is the entire array. Using an array might not be the best choice because of this. The theme_mods are already stored as an array, so you can just use each one as if it is scalar and it’s easier that way.

    Thread Starter maxlefou

    (@maxlefou)

    I got rid of arrays (was planning to anyway, to get a better code structure) and it worked! For some reason I suspected that the use of arrays would be the cause. I just wasn’t sure of it.

    Thank you so much!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘custom theme settings not working’ is closed to new replies.