admin管理员组文章数量:1291135
I'm developing a WordPress theme in which I'm 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 instead of the "Clear" 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' );
And here's what the resulting color picker looks like. (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):
I'm developing a WordPress theme in which I'm 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 instead of the "Clear" 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' );
And here's what the resulting color picker looks like. (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):
Share Improve this question asked Jun 1, 2021 at 14:49 crispycrispy 214 bronze badges1 Answer
Reset to default 1I was actually able to get this to work with the help of kind developers on the WordPress forum, and I will post the answer to my question here to benefit anyone who has the same problem.
It is best to use the WP core functionality if it exists, which is the case for color controls, and to create an instance of the core WP_Customize_Color_Control class.
Here's the corrected code:
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(
new WP_Customize_Color_Control(
$wp_customize,
'footer_color',
array(
'label' => __( 'Footer Color', 'textdomain' ),
'section' => 'colors',
'capability' => 'edit_theme_options',
)
)
);
}
add_action( 'customize_register', 'mytheme_customize_register' );
本文标签: How to set a default color for the WordPress Customizer’s color picker
版权声明:本文标题:How to set a default color for the WordPress Customizer’s color picker? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741526338a2383490.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论