5

It seems by default WordPress provides fields for site-title and tagline.

I understand how to add (for example) the option to chose a logo, adding add_theme_support( 'custom-logo' ); to the functions.php file. However, how do I add fields for 'company name' and 'company division' or for any text field that is not already part of the WordPress theme support?

9

You'll have to add your own customizer controls to achieve that.

So for example, if you want to add Company Name, you can use this code:

function my_register_additional_customizer_settings( $wp_customize ) {
    $wp_customize->add_setting(
        'my_company_name',
        array(
            'default' => '',
            'type' => 'option', // you can also use 'theme_mod'
            'capability' => 'edit_theme_options'
        ),
    );

    $wp_customize->add_control( new WP_Customize_Control(
        $wp_customize,
        'my_company_name',
        array(
            'label'      => __( 'Company name', 'textdomain' ),
            'description' => __( 'Description for your field', 'textdomain' ),
            'settings'   => 'my_company_name',
            'priority'   => 10,
            'section'    => 'title_tagline',
            'type'       => 'text',
        )
    ) );
}
add_action( 'customize_register', 'my_register_additional_customizer_settings' );

PS. Here you can find more docs regarding this topic: Theme Customization API

| improve this answer | |
4

This is all part of the Customizer API; you'll create a section, add a setting to the section, then add a controller to the setting. Once complete you'll show it on your theme using get_theme_mod()

For your example, you would add the 'company name' to the Customizer like so:

function mytheme_customize_register( $wp_customize ) {

    $wp_customize->add_section( 'mytheme_company_section' , array(
        'title'      => __( 'Additional Company Info', 'mytheme' ),
        'priority'   => 30,
    ));

    $wp_customize->add_setting( 'mytheme_company-name', array());
    $wp_customize->add_control( new WP_Customize_Control(
        $wp_customize,
        'mytheme_company_control',
            array(
                'label'      => __( 'Company Name', 'mytheme' ),
                'section'    => 'mytheme_company_section',
                'settings'   => 'mytheme_company-name',
                'priority'   => 1
            )
        )
    );

    // ..repeat ->add_setting() and ->add_control() for mytheme_company-division
}
add_action( 'customize_register', 'mytheme_customize_register' );

Then to show this dynamic value in the theme, you'd go into a file like header.php or wherever you want it, and use:

echo get_theme_mod( "mytheme_company-name" );
| improve this answer | |
  • Hmm... Wouldn't option be a better choice for settings like company name? – Krzysiek Dróżdż Jul 30 '18 at 19:02
  • I've assumed he's working on a theme, not a plugin. A very good point made though. – David Sword Jul 30 '18 at 19:06
  • Well, even if it is a theme, then you don't need to save it as a theme - Company name won't change very often. And since Site name is stored as an option... That's my way of reasoning in this case... :) But it's a detail, I guess :) – Krzysiek Dróżdż Jul 30 '18 at 19:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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