Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Building themes using the WP Theme Customizer

Building themes using the WP Theme Customizer

Avatar for Dave Green

Dave Green

June 09, 2015

Other Decks in Programming

Transcript

  1. “We have made a new requirement regarding theme options. We

    are going to be requiring use of the customizer for all options from now. This applies to all new themes (submitted after this post is published). All existing themes have 6 months from today before we enforce this.” -- WordPress Theme Review Team
  2. A brief summary of the Theme Customizer API • Introduced

    in WordPress 3.4, was which released on June 13th 2012. By no means a new feature! • Integrates with the WordPress Settings API • Developers can add theme options using WordPress core features instead of using an external framework • Provides the end user with instant feedback when they make a change, whether it be text, colours, image uploads etc
  3. How to build customizer options into your theme • Create

    a subfolder in your theme called ‘inc’ if you don’t already have one e.g. wp-content/your-theme/inc • Create a file called customizer.php in that folder • Add the following line near the top of your functions.php // Child Theme require get_stylesheet_directory() . '/inc/customizer.php'; // Parent Theme require get_template_directory() . '/inc/customizer.php';
  4. function wpsheff_customize( $wp_customize ) { // Define settings, sections and

    controls } add_action( 'customize_register', 'wpsheff_customize' ); The customize_register action hook
  5. function wpsheff_customize( $wp_customize ) { $wp_customize->add_setting( 'phone_number' , array( 'default'

    => '07890 123 456', 'capability' => 'manage_options', 'transport' => 'postMessage' 'sanitize_callback' => 'wpsheff_validate', ) ); } add_action( 'customize_register', 'wpsheff_customize' ); Defining a setting
  6. function wpsheff_validate( $data ) { // Return only numbers $sanitized

    = preg_replace( '/[^0-9 ]+/' , '' , $data ); return $sanitized; } Validating setting data
  7. function wpsheff_customize( $wp_customize ) { $wp_customize->add_section('wpsheff_section', array( 'title' => 'WP

    Sheff Section', 'description' => 'Settings created at WP Sheff.', 'priority' => '20' ) ); } add_action( 'customize_register', 'wpsheff_customize' ); Defining a section
  8. function wpsheff_customize( $wp_customize ) { $wp_customize->add_control( new WP_Customize_Control( $wp_customize, 'phone_number',

    // control name array( 'label' => 'Phone Number', 'section' => 'wpsheff_section', 'settings' => 'phone_number', 'type' => 'text', // 'priority' => 10, ) ) ); } add_action( 'customize_register', 'wpsheff_customize' ); Defining a control
  9. function wpsheff_customize( $wp_customize ) { // SETTING $wp_customize->add_setting('phone_number', array( 'default'

    => '07890 123 456', 'capability' => 'manage_options', 'transport' => 'postMessage' ) ); // SECTION $wp_customize->add_section('wpsheff_section', array( 'title' => 'WP Sheff Section', 'description' => 'Settings created at WP Sheff.', 'priority' => '20' ) ); // CONTROL $wp_customize->add_control( new WP_Customize_Control( $wp_customize, 'phone_number', array( 'label' => 'Phone Number', 'section' => 'wpsheff_section', 'settings' => 'phone_number', 'type' => 'text', ) ) ); } add_action( 'customize_register', 'wpsheff_customize' );
  10. function wpsheff_customize( $wp_customize ) { $wp_customize->add_setting('title_colour', array( 'default' => '#000',

    'capability' => 'manage_options', 'transport' => 'postMessage' ) ); } add_action( 'customize_register', 'wpsheff_customize' ); Adding a colour picker to change the title colour
  11. function wpsheff_customize( $wp_customize ) { // Setting… // Section… $wp_customize->add_control(

    new WP_Customize_Color_Control( $wp_customize, 'title_colour', // control name array( 'label' => 'Title Colour', 'section' => 'wpsheff_section', 'settings' => 'title_colour' ) ) ); } add_action( 'customize_register', 'wpsheff_customize' );
  12. Outputting CSS to override the default styles function wpsheff_customize_css() {

    ?> <style type="text/css"> .site-title a { color: <?php echo get_theme_mod('title_colour'); ?>; } </style> <?php } add_action( 'wp_head', 'wpsheff_customize_css' );
  13. Configuring live preview: postMessage is the key function wpsheff_customize( $wp_customize

    ) { $wp_customize->add_setting( 'phone_number' , array( 'default' => '07890 123 456', 'capability' => 'manage_options', 'transport' => 'postMessage' // BINGO! ) ); } add_action( 'customize_register', 'wpsheff_customize' );
  14. Configuring live preview: add theme-customizer.js ( function( $ ) {

    // Update the phone number wp.customize( 'phone_number', function( value ) { value.bind( function( newval ) { $( '.phone-number' ).html( newval ); } ); } ); } )( jQuery );
  15. Configuring live preview: add theme-customizer.js ( function( $ ) {

    // Phone number code… // Update the site title colour wp.customize( 'title_colour', function( value ) { value.bind( function( newval ) { $( '.site-title a' ).css( 'color' , newval ); } ); } ); } )( jQuery );
  16. ( function( $ ) { // Update the phone number

    wp.customize( 'phone_number', function( value ) { value.bind( function( newval ) { $( '.phone-number' ).html( newval ); } ); } ); // Update the site title colour wp.customize( 'title_colour', function( value ) { value.bind( function( newval ) { $( '.site-title a' ).css( 'color' , newval ); } ); } ); } )( jQuery );
  17. Configuring live preview: enqueue the Javascript function wpsheff_live_preview() { wp_enqueue_script(

    'wpsheff-customizer', // script ID get_stylesheet_directory_uri().'/js/theme-customizer.js', array( 'jquery','customize-preview' ), '', true // in the footer ); } add_action('customize_preview_init', 'wpsheff_live_preview');
  18. Modifying existing settings, sections and controls function wpsheff_modifying( $wp_customize )

    { $wp_customize->get_setting( 'phone_number' )->default = '999'; $wp_customize->get_section( 'wpsheff_section' )->title = 'WP Sheffield'; $wp_customize->get_control( 'phone_number' )->label = 'Telephone Number'; } add_action( 'customize_register' , 'wpsheff_modifying', 50 );
  19. Removing existing sections and controls function wpsheff_removing( $wp_customize ) {

    $wp_customize->remove_section( 'header_image' ); $wp_customize->remove_section( 'background_image' ); $wp_customize->remove_control( 'background_color' ); $wp_customize->remove_control( 'header_background_color' ); $wp_customize->remove_control( 'sidebar_textcolor' ); } add_action( 'customize_register' , 'wpsheff_removing' , 50 );
  20. Adding controls to existing sections function wpsheff_adding( $wp_customize ) {

    $wp_customize->add_control( new WP_Customize__Control( $wp_customize, 'phone_number', array( 'label' => 'Telephone Number', 'section' => 'title_tagline', // BINGO! 'settings' => 'phone_number' ) ) ); } add_action( 'customize_register' , 'wpsheff_adding' , 50 );
  21. • Relatively simple to build into themes or use to

    modify existing theme options • No external dependencies for adding options using common elements • Immediate feedback when the user makes changes delivers a better user experience Pros of using the Theme Customizer API
  22. • User interface isn’t everyone’s cup of tea: it’s quite

    cramped, and isn’t great on mobile • The core controls are currently very limited, requiring third party libraries Cons of using the Theme Customizer API