9. Relationships within the Customizer
Control
Control
Control
Control
Section
Setting
Setting
Setting
Setting
DatabaseSection
Section
The Customizer Konstantin Obenland
Saturday, September 21, 13
10. Theme Mods & Options
Digression
The Customizer Konstantin Obenland
Saturday, September 21, 13
11. Options
“The Options API is a simple and standardized way of storing data in
the database.” — WordPress Codex
The Customizer Konstantin Obenland
Saturday, September 21, 13
12. Theme Modifications
• Options API for themes.
• Introduced in 2.1 (!)
The Customizer Konstantin Obenland
Saturday, September 21, 13
13. Theme Mods API
set_theme_mod( $name, $value );
get_theme_mod( $name, $default = false );
remove_theme_mod( $name );
get_theme_mods();
remove_theme_mods();
The Customizer Konstantin Obenland
Saturday, September 21, 13
14. Theme Mods API
// Function simplified for brevity and clarity.
function get_theme_mod( $name, $default = false ) {
$mods = get_option( 'theme_mods_' . get_option( 'stylesheet' ) );
if ( isset( $mods[ $name ] ) )
return $mods[ $name ];
return $default;
}
The Customizer Konstantin Obenland
Saturday, September 21, 13
15. The Customizer API
The Customizer Konstantin Obenland
Saturday, September 21, 13
16. Get Started
/**
* Registers the theme setting controls with the Customizer.
*
* @param WP_Customize_Manager $wp_customize
*/
function prefix_customize_register( $wp_customize ) {
// Code
}
add_action( 'customize_register', 'prefix_customize_register' );
The Customizer Konstantin Obenland
Saturday, September 21, 13
17. Adding a Section
$wp_customize->add_section( 'unique_section_id', array(
'title' => __( 'Title', 'textdomain' ),
'priority' => 10,
'description' => __( 'Description', 'textdomain' ),
'theme_supports' => '',
'capability' => 'edit_theme_options',
) );
The Customizer Konstantin Obenland
Saturday, September 21, 13
30. /**
* Add postMessage support for site title and description for the Customizer.
*
* @since Twenty Thirteen 1.0
*
* @param WP_Customize_Manager $wp_customize Customizer object.
* @return void
*/
function twentythirteen_customize_register( $wp_customize ) {
$wp_customize->get_setting( 'blogname' )->transport = 'postMessage';
$wp_customize->get_setting( 'blogdescription' )->transport = 'postMessage';
$wp_customize->get_setting( 'header_textcolor' )->transport = 'postMessage';
}
add_action( 'customize_register', 'twentythirteen_customize_register' );
In Twenty Thirteen
The Customizer Konstantin Obenland
Saturday, September 21, 13
31. /**
* Binds JavaScript handlers to make Customizer preview reload changes
* asynchronously.
*
* @since Twenty Thirteen 1.0
*/
function twentythirteen_customize_preview_js() {
wp_enqueue_script(
'twentythirteen-customizer',
get_template_directory_uri() . '/js/theme-customizer.js',
array( 'customize-preview' ),
'20130226',
true
);
}
add_action( 'customize_preview_init', 'twentythirteen_customize_preview_js' );
In Twenty Thirteen
The Customizer Konstantin Obenland
Saturday, September 21, 13
32. /**
* Theme Customizer enhancements for a better user experience.
* Contains handlers to make Theme Customizer preview reload changes asynchronously.
*/
( function( $ ) {
// Site title and description.
wp.customize( 'blogname', function( value ) {
value.bind( function( to ) {
$( '.site-title' ).text( to );
} );
} );
wp.customize( 'blogdescription', function( value ) {
value.bind( function( to ) {
$( '.site-description' ).text( to );
} );
} );
} )( jQuery );
In Twenty Thirteen
The Customizer Konstantin Obenland
Saturday, September 21, 13
33. wp.customize( 'header_textcolor', function( value ) {
value.bind( function( to ) {
if ( 'blank' == to ) {
if ( 'remove-header' == _wpCustomizeSettings.values.header_image )
$( '.home-link' ).css( 'min-height', '0' );
$( '.site-title, .site-description' ).css( {
'clip': 'rect(1px, 1px, 1px, 1px)',
'position': 'absolute'
} );
} else {
$( '.home-link' ).css( 'min-height', '230px' );
$( '.site-title, .site-description' ).css( {
'clip': 'auto',
'color': to,
'position': 'relative'
} );
}
} );
} );
In Twenty Thirteen
The Customizer Konstantin Obenland
Saturday, September 21, 13