Support » Developing with WordPress » How to set a default color for the Customizer’s color picker?

  • Resolved C. Perez

    (@crispybacon)


    Hello,

    I am developing a theme in which I am adding a color control to the Customizer’s “Colors” section, so that the user can customize the footer’s background color.

    I added a functioning color picker to the Customizer, but I can’t get the color picker to show a “Default” color and button.

    I assigned a hex color value to the ‘default’ parameter in the Customizer setting associated with my color control, assuming this would be the default color for this control, but it just doesn’t work.

    Instead of the “Default” button, my color picker shows a “Clear” button that clears the color input but doesn’t reset it to a default color.

    My question is: How can I get the Customizer’s color picker to show a “Default” button?

    Here’s the custom function I added to my theme’s customizer.php file:

    function mytheme_customize_register( $wp_customize ) {
    
    	$wp_customize->add_setting( 'footer_color',
       	  array(
                'default' => '#000000',
                'transport' => 'refresh', 
                'sanitize_callback' => 'sanitize_hex_color',
       	  )
    	);
    
    	$wp_customize->add_control( 'footer_color',
       	  array(
                'label' => __( 'Footer Color', 'textdomain' ),
                'section' => 'colors',
                'type' => 'color',
                'capability' => 'edit_theme_options',
    	  )
    	);
    }
    add_action( 'customize_register', 'mytheme_customize_register' );
Viewing 2 replies - 1 through 2 (of 2 total)
  • Joy

    (@joyously)

    The code for the control must pick up the default from the setting. If you used the built-in color control, you would get it automatically. It’s best to use the core functionality if it exists, which it does for colors.
    You code has some wrong parameters, and should be

    $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'footer_color',
       	  array(
                'label' => __( 'Footer Color', 'textdomain' ),
                'section' => 'colors',
                'capability' => 'edit_theme_options',
    	  )
    	);

    See https://developer.wordpress.org/reference/classes/wp_customize_manager/add_control/

    Thread Starter C. Perez

    (@crispybacon)

    Indeed, that solves my problem. Thanks a lot for your help, Joy! I appreciate it.

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.