8

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?

3 Answers 3

14

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

7

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" );
3
  • Hmm... Wouldn't option be a better choice for settings like company name? Commented Jul 30, 2018 at 19:02
  • I've assumed he's working on a theme, not a plugin. A very good point made though. Commented Jul 30, 2018 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 :) Commented Jul 30, 2018 at 19:46
0

Here's my take on a custom text menu setting to put in a footer, for example, using a default value as well:

function theme_footer_copyright( $wp_customize ) {
    $wp_customize->add_setting( 'footer_copyright_setting', array(
        'default' => 'With ❤ from Prague', //default value of setting
    ));
    $wp_customize->add_section( 'footer_copyright_section', array(
        'title' => __( 'Footer Copyright', 'textdomain' ), //title of customizer menu section
        'priority' => 20, //order of display
    ));
    $wp_customize->add_control( new WP_Customize_Control( $wp_customize, 'footer_copyright_control', array(
        'label' => __(' Copyright Text', 'textdomain' ), //label of the setting itself
        'section' => 'footer_copyright_section',
        'settings' => 'footer_copyright_setting',
    )));
}
add_action( 'customize_register', 'theme_footer_copyright' );

Then to show it in theme (and the default value) use:

<?php echo get_theme_mod('footer_copyright', 'With ❤ from Prague'); ?>

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.