• add_theme_support( 'custom-background' );
    
    add_action( 'customize_register', 'custom_background_size' );
    function custom_background_size( $wp_customize ) {
    
        $wp_customize->add_setting( 'default-size', array(
                'default' => 'inherit',
            ) );
    
        $wp_customize->add_control( 'default-size', array(
                'label'      => 'Background Image Size',
                'section'    => 'background_image',
                'settings'   => 'default-size',
                'priority'   => 200,
                'type' => 'radio',
                'choices' => array(
                    'cover' => 'Cover',
                    'contain' => 'Contain',
                    'inherit' => 'Inherit',
                )
            ));
    }
    add_action( 'wp_head', 'custom_background_size_css', 999 );
    function custom_background_size_css() {
    
        $background_size = get_theme_mod( 'default-size', 'inherit' );
    
        echo '<style type="text/css"> body.custom-background { background-size:'.$background_size.'; } </style>';
    
    }

    This is code i’m using that add custom background image to customizer (from codex) and additional size attribute.

    Now i want it to hide along other controls when image is not specified but can’t find active_callback name for it.

    Also i would like to move whole custom background section to panel. Tried :

    add_theme_support( 'custom-background', $defaults );
    
    $defaults = array(
    	'panel' => 'theme_settings'
    );

    but that’s not working. Any idea how can i get these 2 things work ?
    Thanks

    @up
    One more thing. How can i add section icons like in first screenshot here ?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Checkout https://github.com/aubreypwd/wp-forums/blob/custom-background-13/custom-background-13/custom-background-13.php which is what I think you need.

    <?php
    
    add_theme_support( 'custom-background' );
    
    function custom_background_size( $wp_customize ) {
    
    	// Add the "panel" (Section).
    	// If this section already exists, comment the next 3 lines out.
    	$wp_customize->add_section( 'theme_settings', array(
    		'title' => __( 'Theme Settings' ),
    	) );
    
    	// If they haven't set the background image, don't show these controls.
    	if ( ! get_theme_mod( 'background_image' ) ) {
    		return;
    	}
    
    	// Add your setting.
    	$wp_customize->add_setting( 'default-size', array(
    		'default' => 'inherit',
    	) );
    
    	// Add your control box.
    	$wp_customize->add_control( 'default-size', array(
    		'label'      => __( 'Background Image Size' ),
    		'section'    => 'theme_settings',
    		'settings'   => 'default-size',
    		'priority'   => 200,
    		'type' => 'radio',
    		'choices' => array(
    			'cover' => __( 'Cover' ),
    			'contain' => __( 'Contain' ),
    			'inherit' => __( 'Inherit' ),
    		)
    	) );
    }
    
    add_action( 'customize_register', 'custom_background_size' );
    
    function custom_background_size_css() {
    	$background_size = get_theme_mod( 'default-size', 'inherit' );
    	echo '<style> body.custom-background { background-size: '.$background_size.'; } </style>';
    }
    
    add_action( 'wp_head', 'custom_background_size_css', 999 );

    The controls aren’t added, unless the background image is set, and I’ve set the control to be in the theme_settings panel (had to create it on my setup, but if it already exists, you can just comment that out).

    Any word on the solution above?

    BTW the link has been updated to: https://github.com/aubreypwd/wp-forums/tree/topic/custom-background-13

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Custom Background’ is closed to new replies.