6

How can I add a Drop down control to WordPress Theme Customizer and use its value in Theme source?
I want to use Drop down value for changing some CSS file names

Thank you very much

1 Answer 1

7

Add Section to Theme Customizer:

$wp_customize->add_section( 'parsmizban_options', 
 array(
    'title'       => __( 'Theme Options', 'parsmizban' ), //Visible title of section
    'priority'    => 20, //Determines what order this appears in
    'capability'  => 'edit_theme_options', //Capability needed to tweak
    'description' => __('Allows you to customize settings for Theme.', 'parsmizban'), //Descriptive tooltip
 ) 
);

Add new Setting:

$wp_customize->add_setting( 'bootstrap_theme_name', //No need to use a SERIALIZED name, as `theme_mod` settings already live under one db record
 array(
    'default'    => 'default', //Default setting/value to save
    'type'       => 'theme_mod', //Is this an 'option' or a 'theme_mod'?
    'capability' => 'edit_theme_options', //Optional. Special permissions for accessing this setting.
    //'transport'  => 'postMessage', //What triggers a refresh of the setting? 'refresh' or 'postMessage' (instant)?
 ) 
);

Add a new control:

/* Supports basic input types text, checkbox, textarea, radio, select and dropdown-pages. * Additional input types such as email, url, number, hidden and date are supported implicitly. */

//3. Finally, we define the control itself (which links a setting to a section and renders the HTML controls)...
$wp_customize->add_control( new WP_Customize_Control(
 $wp_customize, //Pass the $wp_customize object (required)
 'parsmizban_theme_name', //Set a unique ID for the control
 array(
    'label'      => __( 'Select Theme Name', 'parsmizban' ), //Admin-visible name of the control
    'description' => __( 'Using this option you can change the theme colors' ),
    'settings'   => 'bootstrap_theme_name', //Which setting to load and manipulate (serialized is okay)
    'priority'   => 10, //Determines the order this control appears in for the specified section
    'section'    => 'parsmizban_options', //ID of the section this control should render in (can be one of yours, or a WordPress default section)
    'type'    => 'select',
    'choices' => array(
        'default' => 'Default',
        'cerulean' => 'Cerulean',
        'cosmo' => 'Cosmo',
        'cyborg' => 'cyborg',
    )
)
) );

Use this control:

<?php esc_html_e( get_theme_mod( 'bootstrap_theme_name' ) ); ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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