Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress WordPress Customizer API Custom WordPress Customizer Settings Custom Footer Text in the Theme Customizer

Justin Estrada
Justin Estrada
34,995 Points

Attempting to add another field with WP_Customizer_Control

Hello, I'm trying to use: $wp_customize->add_control( new WP_Customize_Control, ...) to create two fields under my general_settings panel > contact info section, but the adding the subsequent object methods add_setting() add_control overrides the previous ones.

Anybody have any idea what the best course of action is to get two fields in there. Please ask me questions, Thanks so much and here is a portion of my code from my functions.php:

// Add Contact Info Settings
$wp_customize->add_section( 'contact_info' , array(
'title'      => __('Change Contact Info','myprofilethemecustomizer'), 
'panel'      => 'general_settings',
'priority'   => 900    
) );  
$wp_customize->add_setting(
  'myprofile_phone_num',
  array(
      'default'           => __( '1-949-555-5555', 'myprofilethemecustomizer' ),
      'transport'         => 'postMessage',
      'sanitize_callback' => 'sanitize_text'          
  )
);
$wp_customize->add_control(
    new WP_Customize_Control(
        $wp_customize,
        'contact_info',
        array(
            'label'          => __( 'Phone Number (ex: 1-949-555-5555)', 'myprofilethemecustomizer' ),
            'section'        => 'contact_info',
            'settings'       => 'myprofile_phone_num',
            'type'           => 'text'
        )
    )
);
$wp_customize->add_setting(
  'myprofile_email',
  array(
      'default'           => __( 'example@example.com', 'myprofilethemecustomizer' ),
      'transport'         => 'postMessage',
      'sanitize_callback' => 'sanitize_text'          
  )
);
$wp_customize->add_control(
    new WP_Customize_Control(
        $wp_customize,
        'contact_info',
        array(
            'label'          => __( 'Email (example@example.com)', 'myprofilethemecustomizer' ),
            'section'        => 'contact_info',
            'settings'       => 'myprofile_email',
            'type'           => 'text'
        )
    )
);
Justin Estrada
Justin Estrada
34,995 Points

I think I know the answer now my second parameter in the instance of the object WP_Customize_Control is the same in both instances. ima test and get back to this forum.

1 Answer

Justin Estrada
Justin Estrada
34,995 Points

Hello I fixed it. :)

I changed this:

$wp_customize->add_control(
    new WP_Customize_Control(
        $wp_customize,
        'phone_info',

and this:

$wp_customize->add_control(
    new WP_Customize_Control(
        $wp_customize,
        'email_info',

I believe these are the setting identifiers. I had to make them different they were the same, which gave me issues, obvi :)