0

I'm trying to add a color to my theme for the <h2> tag on my website. But now I've got the following problem:

The hashtag is missing before the color. So I get 000000 instead of #000000. It does show the color, I can change the color and it all works fine, until it is displayed on the site.

This is my code:

function cloudboxx_customize_register( $wp_customize ) {
    //All our sections, settings, and controls will be added here
    $wp_customize->add_setting( 'header_textcolor' , array(
        'default'     => "#000000",
        'transport'   => 'refresh',
    ) );

    $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'header_textcolor', array(
        'label'        => __( 'Header Color', 'cloudboxx' ),
        'section'    => 'colors',
    ) ) );
}
add_action( 'customize_register', 'cloudboxx_customize_register' );

function cloudboxx_customize_css()
{
    ?>
    <style type="text/css">
        h2 { color:<?php echo get_theme_mod('header_textcolor', "#000000"); ?>; }
    </style>
    <?php
}
add_action( 'wp_head', 'cloudboxx_customize_css');

Output on site:

<style type="text/css">
    h2 { color:dd9933; }
</style>
0

Already fixed it. When saving it to the database the hashtag is filtered out. So with echoing the color, I just put a hashtag before it, like this:

function cloudboxx_customize_register( $wp_customize ) {
    //All our sections, settings, and controls will be added here
    $wp_customize->add_setting( 'header_textcolor' , array(
        'default'     => "#000000",
        'transport'   => 'refresh',
    ) );

    $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'header_textcolor', array(
        'label'        => __( 'Header Color', 'cloudboxx' ),
        'section'    => 'colors',
    ) ) );
}
add_action( 'customize_register', 'cloudboxx_customize_register' );

function cloudboxx_customize_css()
{
    ?>
    <style type="text/css">
        h2 { color: #<?php echo get_theme_mod('header_textcolor', "#000000"); ?>; }
    </style>
    <?php
}
add_action( 'wp_head', 'cloudboxx_customize_css');

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.