Use default value of wp_customizer in theme_mod output?

Sadly not – all your customize controls are hooked onto customize_register, so they’ll only ever come into play when customising the theme for the first time.

get_theme_mod() takes a second argument for a “default” value – yes, it means two instances of data in your code, but it’s a half-solution.

I guess a more DRY approach would be a coupling of globals & helper functions:

$my_theme_defaults = array(
    'foo' => 'bar',
    'baz' => 'boo',
);

function my_theme_customize( $wp_customize ) {
    global $my_theme_defaults;

    $wp_customize->add_setting(
        'foo',
        array(
            'default' => $my_theme_defaults['foo'],
        )
    );  
}

function my_theme_mod( $name ) {
    global $my_theme_defaults;

    echo get_theme_mod( $name, $my_theme_defaults[ $name ] );
}

Leave a Comment