0

I'm currently working on my first theme and am trying to allow the user to edit some text in the customizer.

Basically, I've got as far as getting the text the user writes in the customizer to show on the frontend, but the default text isn't showing and I'm not sure how to get it to show.

Below is the code I've put on the customizer.php file

$wp_customize->add_section('home_header', array(
        'title'    => __('Home Header', 'sincere'),
        'description' => '',
        'priority' => 70,
));

$wp_customize->add_setting( 'home_header_title', array(
        'capability' => 'edit_theme_options',
        'default' => __('Clean. Simple. Sincere', 'sincere'),
        'sanitize_callback' => 'sanitize_text_field',
) );

$wp_customize->add_control( 'home_header_title', array(
        'type' => 'text',
        'section' => 'home_header', // Add a default or your own section
        'label' => __( 'Header Text' ),
) );

This is the code I've put on my front-page.php file to try and display the text.

<section class="feature-image feature-image-home feature-image-default-alt" 
data-type="background" data-speed="2">
    <h1 class="entry-title"><?php echo get_theme_mod('home_header_title'); ?
    ></h1>
</section>

Thanks in advance for any help!

2

Setting the default when registering the setting doesn't make it the default on output, it just makes it the default value when the customiser is opened. To provide a default before the customiser is opened/saved you pass it as a second argument to get_theme_mod():

<?php echo get_theme_mod('home_header_title', __('Clean. Simple. Sincere', 'sincere') ); ?>

This can be annoying, since you have to have the defaults written down twice. So one thing you can do is create a function the returns defaults, and use it in both places.

The function would look like this:

function sincere_get_theme_default( $setting ) {
    $defaults = array(
        'home_header_title' => __( 'Clean. Simple. Sincere', 'sincere' ),
    );

    return $defaults[$setting];
}

Then you'd call it as the default value when registering your setting:

$wp_customize->add_setting( 'home_header_title', array(
    'capability' => 'edit_theme_options',
    'default' => sincere_get_theme_default( 'home_header_title' ),
    'sanitize_callback' => 'sanitize_text_field',
) );

And also as the default argument for get_theme_mod():

<?php echo get_theme_mod( 'home_header_title', sincere_get_theme_default( 'home_header_title' ) ); ?>
  • Thanks for the reply. I've added the function to functions.php and replaced the code in the customizer.php and the front-page.php files with your code, but no default text is being shown. I'm not sure what I'm doing wrong. – Tim Davis Aug 26 '17 at 1:02
0

At my end your code working fine. if default text is not being shown in your text customizer section then try to change setting id or once simple try this code and hard referesh brower(ctrl+F5).Don't forget to try change the setting id.

add_action( 'customize_register', 'mytheme_text_customizer' );
function mytheme_text_customizer($wp_customize){
    $wp_customize->add_section('home_header', array(
            'title'    => __('Home Header', 'sincere'),
            'description' => '',
            'priority' => 70,
    ));

    $wp_customize->add_setting( 'home_header_title', array(
            'capability' => 'edit_theme_options',
            'default' => __('Clean. Simple. Sincere', 'sincere'),
            'sanitize_callback' => 'sanitize_text_field',
    ) );

    $wp_customize->add_control( 'home_header_title', array(
            'type' => 'text',
            'section' => 'home_header', // Add a default or your own section
            'label' => __( 'Header Text' )
    ) );
}

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.