Custom function to add a secondary logo

admin

Administrator
Staff member
I'm looking to create a custom function in order to be able to change/load a new image from the "customize" panel (this because I won't be the one who manage the website, so I'm trying to made things as easy as possible).

I used by default the adamos theme (<a href="https://wordpress.org/themes/adamos" rel="nofollow">link</a>) and then I started customizing it for my purposes.
In <em>functions.php</em> I added the following code:

Code:
function gb_theme_customizer( $wp_customize ) {

   $wp_customize-&gt;add_section( 'gb_logo_section' , array(
    'title'       =&gt; __( 'Gb\'s Logo', 'gb_logo' ),
    'priority'    =&gt; 31,
    'description' =&gt; 'Upload a new logo for Gb to be displayed above the left menu',
) );

   $wp_customize-&gt;add_setting( 
        'gb_logo',
        array(
            'sanitize_callback' =&gt; 'gb_sanitize_upload',
        )
    );

$wp_customize-&gt;add_control( new WP_Customize_Image_Control( $wp_customize, 'gb_logo', array(
    'label'    =&gt; __( 'Gb\'s Logo', 'gb_logo' ),
    'section'  =&gt; 'gb_logo_section',
    'settings' =&gt; 'gb_logo',
) ) );


}
add_action('customize_register', 'gb_theme_customizer');

This was made copying the theme's default function <em>adamos_theme_customizer</em>, that is still above this one (I need both the functions).

To call the function I used the following code:

Code:
&lt;!--TEST--&gt;
&lt;?php if ( get_theme_mod('gb_logo') ) : ?&gt;
&lt;div class="test"&gt;
&lt;img src="&lt;?php echo get_theme_mod( 'gb_logo' ); ?&gt;" alt="&lt;?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?&gt;"&gt;
&lt;/div&gt;
&lt;?php endif; ?&gt;
&lt;br /&gt;

This function actually doesn't work and I don't really know why, because if I try to load images it works (they're loaded, but neither this way are shown). Trying some debugging (see the code below) I found that with <em>get_theme_mod</em> WordPress thinks that gb_logo is a boolean variable.

Code:
&lt;div id="test" style="border: 2px solid black"&gt;&lt;?php $mods = get_option('gb_logo');
              var_dump($mods);?&gt;&lt;/div&gt;

Any idea about how to fix this?