0

I'm trying to merely output the value from one of the fields in the Customizer.

This is the code for creating the setting and its control (it's in functions.php for the current theme):

<?php
add_action('customize_register', 'h2c_customize_register');
function h2c_customize_register($wp_customize) {
    $wp_customize->add_setting(
        'h2c[logo_width_px]', array (
            'default'           => '',
            'capability'        => 'edit_theme_options',
            'type'              => 'option',
            'transport'         => 'postMessage',
        )
    );

    $wp_customize->add_control(
        'logo_width_px', array(
            'label'             => __('Logo Width', 'obf_text'),
            'section'           => 'h2c_default_things',
            'settings'          => 'h2c[logo_width_px]',
        )
    );
}

And this is what I'm using to get the value of that field:

<?php
    echo get_theme_mod( 'h2c[logo_width_px]' );
?>

But nothing.

The field is visible in the Customizer itself and it indeed saves the value I put in it. So I know it does contain data. After putting the output code into a var_dump() instead, I get bool (false), so I'm guessing it doesn't even find the setting for some reason?

Have I done something wrong? I've looked through the code dozens of times now and I can't see where the problem might be.

I'm new at using the Customizer btw, so I'm sure I have done something wrong, I just can't find what that might be.

Edit: I think I solved it.

Edit2: Nope, I didn't. I thought that changing the type from option to theme_mod solved it, but it didn't.

Edit3: sigh. Turns out, it was a combination of two things: The type should be theme_mod and the name could not contain any [] characters.

1 Answer 1

1

You're registering the option in the customizer as an option and then trying to get it as a theme_mod. If you simply change the type from option to theme_mod when you're registering the setting it's gonna work fine. So try using this code:

add_action('customize_register', 'h2c_customize_register');
function h2c_customize_register($wp_customize) {
    $wp_customize->add_setting(
        'logo_width_px', array (
            'default'           => '',
            'capability'        => 'edit_theme_options',
            'type'              => 'option',
            'transport'         => 'postMessage',
        )
    );

    $wp_customize->add_control(
        'logo_width_px', array(
            'label'             => __('Logo Width', 'obf_text'),
            'section'           => 'h2c_default_things',
            'settings'          => 'logo_width_px',
        )
    );
}

and then to get the option this:

echo get_theme_mod( 'logo_width_px' );

Please note in the code above I used logo_width_px instead of h2c[logo_width_px] That's because theme_mods are serialized options anyway, you don't need to do anything more.

1
  • Yep, this is what I figured out as well after a bit of trial and error :). I'll set the checkmark on your answer though. Commented May 4, 2016 at 6:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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