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.