How WP_Customize_Background_Image_Control is supposed to work?

I’m trying to add a second background image function to my theme customizer with this code.

    // HTML Background Image
$wp_customize->add_setting(
    'html_background_image', 
    array(
        'capability'     => 'edit_theme_options',
));

$wp_customize->add_control( 
    new WP_Customize_Background_Image_Control(
        $wp_customize,
        'background_image', 
        array(
            'label'    => __('HTML Background Image', 'TEXTDOMAIN'),
            'section'  => 'background_image',
            'settings' => 'html_background_image',
)));

But it doesn’t work. Am I missing something here?

Related posts

Leave a Reply

1 comment

  1. There’s very little documentation on this class, but here below is the full example from WordPress core (here’s github). Note you’d be using the $wp_customize object you get as argument to your custom_register callback and not $this as does the core class. I’ve done a search and replace below and it works.

            /* CORE COPY PASTE */
            //https://github.com/WordPress/WordPress/blob/master/wp-includes/class-wp-customize-manager.php#L808
    
            /* Custom Background */
    
            $wp_customize->add_section( 'background_image', array(
                'title'          => __( 'Background Image' ),
                'theme_supports' => 'custom-background',
                'priority'       => 80,
            ) );
    
            $wp_customize->add_setting( 'background_image', array(
                'default'        => get_theme_support( 'custom-background', 'default-image' ),
                'theme_supports' => 'custom-background',
            ) );
    
            $wp_customize->add_setting( new WP_Customize_Background_Image_Setting( $wp_customize, 'background_image_thumb', array(
                'theme_supports' => 'custom-background',
            ) ) );
    
            $wp_customize->add_control( new WP_Customize_Background_Image_Control( $wp_customize ) );
    
            $wp_customize->add_setting( 'background_repeat', array(
                'default'        => 'repeat',
                'theme_supports' => 'custom-background',
            ) );
    
            $wp_customize->add_control( 'background_repeat', array(
                'label'      => __( 'Background Repeat' ),
                'section'    => 'background_image',
                'type'       => 'radio',
                'choices'    => array(
                    'no-repeat'  => __('No Repeat'),
                    'repeat'     => __('Tile'),
                    'repeat-x'   => __('Tile Horizontally'),
                    'repeat-y'   => __('Tile Vertically'),
                ),
            ) );
    
            $wp_customize->add_setting( 'background_position_x', array(
                'default'        => 'left',
                'theme_supports' => 'custom-background',
            ) );
    
            $wp_customize->add_control( 'background_position_x', array(
                'label'      => __( 'Background Position' ),
                'section'    => 'background_image',
                'type'       => 'radio',
                'choices'    => array(
                    'left'       => __('Left'),
                    'center'     => __('Center'),
                    'right'      => __('Right'),
                ),
            ) );
    
            $wp_customize->add_setting( 'background_attachment', array(
                'default'        => 'fixed',
                'theme_supports' => 'custom-background',
            ) );
    
            $wp_customize->add_control( 'background_attachment', array(
                'label'      => __( 'Background Attachment' ),
                'section'    => 'background_image',
                'type'       => 'radio',
                'choices'    => array(
                    'fixed'      => __('Fixed'),
                    'scroll'     => __('Scroll'),
                ),
            ) );
    

    It appears you cannot customize the e.g. section, and for evidence look to this code for the third argument to WP_Customize_Image_Control