2

For some reason I cannot output the color. Everything works, but text_color just doesn't want to output its value.

What is going wrong?

Back end code (functions.php):

$wp_customize->add_setting('text_color', array(
    'default'           => '#fff',
    'sanitize_callback' => 'sanitize_hex_color',
    'type'           => 'option',
));

$wp_customize->add_control( new WP_Customize_Color_Control($wp_customize, 'text_color', array(
    'label'    => __('Text color', 'pc'),
    'section'  => 'colors',
    'settings' => 'text_color',
)));

Front end code:

if(!empty(get_theme_mod( 'text_color' ))) {
?>
h1, h2, h3, h4, h5, h6 {
    color:<?php echo get_theme_mod( 'text_color' ); ?>
}
<?php
}
  • Looks fine to me, except that there's a missing semicolon before the closing bracket of the css statement. Could you be a bit more specific about where the problem occurs? – cjbj Jun 7 '16 at 15:54
  • I had a similar problem - try removing 'type' => 'option' - see wordpress.stackexchange.com/questions/222245/… – mistertaylor Jun 7 '16 at 16:06
2

The 'type'=>'option' parameter is not required for the colour picker, instead use:

$wp_customize->add_setting('text_color', array(
    'default'           => '#fff',
    'sanitize_callback' => 'sanitize_hex_color',
));
0

With

type => option

use

get_option( 'text_color' )

and

type => theme_mod (default)

use

get_theme_mod( 'text_color' )

More here: https://codex.wordpress.org/Class_Reference/WP_Customize_Manager/add_setting

-1

if you are using this

add_setting('text_color'...

instead of

add_setting('themename_theme['text_color'] ....

you should retrieve data by

get_option('text_color');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.