Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

WP Customizer - Image not showing

Hi,

I'm trying to setup a WordPress Customizer. I can bring slogan to display as well, but when I tried to add image background via Customizer, it is not showing and always return a blank value when viewing source:

<style>
    .main-content {
        background-image: url("");
    }
</style>

Here is my code:

header.php

<head>
    <?php if(is_post_type_archive('album')) { ?>
        <style>
            .main-content {
                background-image: url("<?php echo get_theme_mod( 'album_bg' ); ?>");
            }
        </style>
    <?php } ?>
</head>
<body>

inc/customizer.php

function mhs_sanitize_image( $image, $setting ) {
    /*
     * Array of valid image file types.
     *
     * The array includes image mime types that are included in wp_get_mime_types()
     */
    $mimes = array(
        'jpg|jpeg|jpe' => 'image/jpeg',
        'gif'          => 'image/gif',
        'png'          => 'image/png',
        'bmp'          => 'image/bmp',
        'tif|tiff'     => 'image/tiff',
        'ico'          => 'image/x-icon'
    );
    // Return an array with file extension and mime_type.
    $file = wp_check_filetype( $image, $mimes );
    // If $image has a valid mime_type, return it; otherwise, return the default.
    return ( $file['ext'] ? $image : $setting->default );
}

function mhs_customize_register( $wp_customize ) {

    if ( ! isset( $wp_customize ) ) {
        return;
    }

    // Adding default setting
    $wp_customize->add_setting("mhs_default_slogan", array(
        "transport" => "postMessage",
    ));

    // Remove default sections
    $wp_customize->remove_section( 'static_front_page' );

    // Adding section and setting
    $wp_customize->add_section( 'mhs_frontpage' , array(
        'title'    => __( 'Frontpage Setting', 'mhs' ),
        'priority' => 80
    ));
    $wp_customize->add_control('mhs_slogan', array(
            'label' => __( 'Slogan', 'mhs' ),
            'section' => 'mhs_frontpage',
            'settings' => 'mhs_default_slogan',
            'default' => 'This is a slogan',
            'type' => 'text',
    ));

    $wp_customize->add_section( 'mhs_background' , array(
        'title'    => __( 'Background Setting', 'mhs' ),
        'priority' => 90
    ));

    $wp_customize->add_setting( 'default_bg_image' , array(
        'default' => 'wp-content/uploads/11/4.jpg',
        'transport' => 'refresh',
    ));

    $wp_customize->add_control( new WP_Customize_Upload_Control( 
        $wp_customize, 
        'album_bg', 
        array(
            'label'      => __( 'Album Background', 'mhs' ),
            'section'    => 'mhs_background',
            'settings'   => 'default_bg_image',
                'priority'   => 1
        )
    ));

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

function mhs_customizer_live_preview()  {
    wp_enqueue_script("sitepoint-themecustomizer", get_template_directory_uri() . "/js/theme-customizer.js", array("jquery", "customize-preview"), '',  true);
}
add_action("customize_preview_init", "mhs_customizer_live_preview");