Skip to content

Instantly share code, notes, and snippets.

@keithics
Last active December 17, 2015 05:19
Show Gist options
  • Save keithics/5557439 to your computer and use it in GitHub Desktop.
Save keithics/5557439 to your computer and use it in GitHub Desktop.
Simple Theme Customizer
<?php
function wpcebu_customize_register( $wp_customize ){
// Full Name
$wp_customize->add_section( 'content',
array(
'title' => __( 'Full Name', 'wpcebu2' ), //Visible title of section
)
);
$wp_customize->add_setting('theme_mods_wpcebu[fulll_name]',
array(
'default' => 'default text',
'type' => 'option',
'capability' => 'edit_theme_options',
'transport' => 'refresh',
)
);
$wp_customize->add_control(new WP_Customize_Control($wp_customize, 'fulll_name', array(
'label' => 'My Full Name',
'type'=>'text', // can be text,checkbox,radio,select and dropdown-pages
'section' => 'content',
'default' => 'default text',
'settings' => 'theme_mods_wpcebu[fulll_name]',
)));
//1. Define a new section (if desired) to the Theme Customizer
$wp_customize->add_section( 'theme_mods_wpcebu',
array(
'title' => __( 'WPCEBU Options', 'wpcebu' ), //Visible title of section
'priority' => 35, //Determines what order this appears in
'capability' => 'edit_theme_options', //Capability needed to tweak
'description' => __('Allows you to customize some example settings for WPCEBU.', 'wpcebu'), //Descriptive tooltip
)
);
//2. Register new settings to the WP database...
$wp_customize->add_setting( 'theme_mods_wpcebu[h1_textcolor]', //Give it a SERIALIZED name (so all theme settings can live under one db record)
array(
'default' => '#2BA6CB', //Default setting/value to save
'type' => 'option', //Is this an 'option' or a 'theme_mod'?
'capability' => 'edit_theme_options', //Optional. Special permissions for accessing this setting.
'transport' => 'refresh', //What triggers a refresh of the setting? 'refresh' or 'postMessage' (instant)?
)
);
//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_Color_Control( //Instantiate the color control class
$wp_customize, //Pass the $wp_customize object (required)
'wpcebu_link_textcolor', //Set a unique ID for the control
array(
'label' => __( 'h1 Color', 'wpcebu' ), //Admin-visible name of the control
'section' => 'colors', //ID of the section this control should render in (can be one of yours, or a WordPress default section)
'settings' => 'theme_mods_wpcebu[h1_textcolor]', //Which setting to load and manipulate (serialized is okay)
'priority' => 10, //Determines the order this control appears in for the specified section
)
) );
// NOW create your own!!
}
add_action( 'customize_register', 'wpcebu_customize_register' );
function wpcebu_customize_css()
{
// p(get_theme_mod());
?>
<style type="text/css">
h1 { color:<?php echo get_theme_mod('h1_textcolor'); ?>; }
</style>
<?php
}
add_action( 'wp_head', 'wpcebu_customize_css');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment