0

I've created two custom fields within the customiser, a text field and a dropdown-pages field. The dropdown-pages field (button_1_link) works fine but the text field (button_1_text) doesn't display any value. I've checked the db entry and there is a value stored for that field. Can anyone explain what the issue is please?

function header_customizer( $wp_customize )
{
    $wp_customize->add_section( 'header_buttons', array(
        'title' => __('Header Buttons', 'test_theme'),
        'priority' => 99,
        'description' => __('You can change the header buttons here') 
    ));

    $wp_customize->add_setting( 'button_1_text_settings', array(
        'type' => 'theme_mod',
        'default' => __('Primary Button Text', 'test_theme'),
        'transport' => 'postMessage'
    ));

    $wp_customize->add_setting( 'button_1_link_settings', array(
        'default' => __('Primary Button Link', 'test_theme'),
        'transport' => 'postMessage'
    ));

    $wp_customize->add_control( 'button_1_text', array(
        'label'    => __( 'Primary Button', 'test_theme' ),
        'section'  => 'header_buttons',
        'settings' => 'button_1_text_settings',
        'type'     => 'text'
    ));

    $wp_customize->add_control( 'button_1_link', array(
        'label'    => __( 'Primary Button Link', 'test_theme' ),
        'section'  => 'header_buttons',
        'settings' => 'button_1_link_settings',
        'type'     => 'dropdown-pages'
    ));
}

In header.php to display:

$button_1 = get_theme_mod( 'button_1_text' );
$page_id_1 = get_theme_mod( 'button_1_link' );              
echo '<a href="'. get_permalink($page_id_1) . '">' . $button_1 . '</a>';

1 Answer 1

0

The settings attribute you're using for add_control() is expecting an array of all settings tied to the control while you're passing a string. The attribute you want is the singular setting which is expecting the string value of the primary setting for the control.

So try this:

$wp_customize->add_control( 'button_1_text', array(
    'label'    => __( 'Primary Button', 'test_theme' ),
    'section'  => 'header_buttons',
    'setting' => 'button_1_text_settings',
    'type'     => 'text'
));

It's also not clear to me if it's best practice, but I tend to use the same string as my setting ID for my control ID, then I can just omit the setting attribute since it defaults to the ID, like this:

$wp_customize->add_setting( 'button_1_text', array(
    'type' => 'theme_mod',
    'default' => __('Primary Button Text', 'test_theme'),
    'transport' => 'postMessage'
));
$wp_customize->add_control( 'button_1_text', array(
    'label'    => __( 'Primary Button', 'test_theme' ),
    'section'  => 'header_buttons',
    'type'     => 'text'
));
2
  • 1
    That makes sense! Thanks so much for your help, that change has worked.
    – FlyingFox
    Oct 6, 2022 at 17:44
  • Glad you got it sorted!
    – AuRise
    Oct 9, 2022 at 14:28

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.