Top Banner
Discovering the capabilities of the Theme Customizer API Tomasz Dziuda WordCamp Praha 2015
113
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: WordCamp Praga 2015

Discovering the capabilities of the Theme Customizer API

Tomasz Dziuda

WordCamp Praha 2015

Page 2: WordCamp Praga 2015

Ahoj!

Page 3: WordCamp Praga 2015

Tomasz Dziuda

Lead Developer @

Co-organizer of WordCamp Poland 2014

Twitter: @dziudek

http://dziudek.pl -> https://www.gavick.com/blog

Mail: [email protected]

Page 4: WordCamp Praga 2015
Page 5: WordCamp Praga 2015
Page 6: WordCamp Praga 2015

Why's it worth using the Theme Customizer?

Page 7: WordCamp Praga 2015

Live preview for setting changes

Page 8: WordCamp Praga 2015

Easy theme-option implementation

Page 9: WordCamp Praga 2015

Test configuration options easily without affecting the live website

Page 10: WordCamp Praga 2015

A user doesn’t have to learn how to use the theme settings page

Page 11: WordCamp Praga 2015

4 practical bits of advice from me to you

Page 12: WordCamp Praga 2015

As the number of options grows, the potential number of users requesting assistance will grow exponentially too

1

0Number of options

Page 13: WordCamp Praga 2015

Giving a user full control over colors, in most cases, ends with dramatic results

Page 14: WordCamp Praga 2015

The Dashboard will accept everything, but the user won't

Page 15: WordCamp Praga 2015

Solutions implemented in the official APIs generally work better

Page 16: WordCamp Praga 2015

How does it work?

Page 17: WordCamp Praga 2015
Page 18: WordCamp Praga 2015

<iframe>

Page 19: WordCamp Praga 2015

<iframe>

refreshor

JavaScript data binding

Page 20: WordCamp Praga 2015

is_preview()

<iframe>

refreshor

JavaScript data binding

Page 21: WordCamp Praga 2015

is_preview()wp_head

<iframe>

refreshor

JavaScript data binding

Page 22: WordCamp Praga 2015

is_preview()

customize_register

wp_head

<iframe>

refreshor

JavaScript data binding

Page 23: WordCamp Praga 2015

is_preview()

customize_register

wp_head

<iframe>

customize_preview_init

refreshor

JavaScript data binding

Page 24: WordCamp Praga 2015

is_preview()

customize_register

wp_head

<iframe>

customize_preview_init

customize_controls_enqueue_scripts

refreshor

JavaScript data binding

Page 25: WordCamp Praga 2015

Customize Manager

Page 26: WordCamp Praga 2015

Customize Manager

Panel

Page 27: WordCamp Praga 2015

Customize Manager

Panel

Section

Page 28: WordCamp Praga 2015

Customize Manager

Panel

Section

Control

Page 29: WordCamp Praga 2015

Customize Manager

Panel

Section

Control

Setting

Page 30: WordCamp Praga 2015

Customize Manager

Panel

Section

Control

Setting

Context

Context

Context

Page 31: WordCamp Praga 2015

Syntax

Page 32: WordCamp Praga 2015

function PREFIX_customize_register($wp_customizer){// Theme Customizer code

}

Page 33: WordCamp Praga 2015

function PREFIX_customize_register($wp_customizer){// Theme Customizer code

}

add_action('customize_register', ‘PREFIX_customize_register’

);

Page 34: WordCamp Praga 2015

function PREFIX_customize_register($wp_customizer){// Theme Customizer code

}

add_action('customize_register', ‘PREFIX_customize_register’

);

Page 35: WordCamp Praga 2015

$wp_customizer->add_X(

);

Page 36: WordCamp Praga 2015

$wp_customizer->add_X(

);

X = setting, panel, section, control

Page 37: WordCamp Praga 2015

$wp_customizer->add_X(‘name’,

);

X = setting, panel, section, control

Page 38: WordCamp Praga 2015

$wp_customizer->add_X(‘name’,array(

‘setting_1’ => ‘value’,‘setting_2’ => ‘value’,…‘setting_N’ => ‘value’

));

X = setting, panel, section, control

Page 39: WordCamp Praga 2015

$wp_customizer->get_X(‘name’)

Page 40: WordCamp Praga 2015

$wp_customizer->get_X(‘name’)

$wp_customizer->remove_X(‘name’)

Page 41: WordCamp Praga 2015

Available paramsparam / element panel section control setting

type ✔ ✔ ✔ ✔

description ✔ ✔ ✔

priority ✔ ✔ ✔

capability ✔ ✔ ✔

theme_supports ✔ ✔ ✔

title ✔ ✔

panel ✔

section ✔

label ✔

choices ✔

input_attrs ✔

active_callback ✔

sanitize_callback ✔

sanitize_js_callback ✔

default ✔

transport ✔

Page 42: WordCamp Praga 2015

Types of controls

Page 43: WordCamp Praga 2015

text

radio

checkbox

textarea

select

dropdown-pages

Page 44: WordCamp Praga 2015

WP_Customize_Color_ControlWP_Customize_Image_Control

Page 45: WordCamp Praga 2015

More controls

Page 46: WordCamp Praga 2015

$wp_customize->add_control( ‘mytheme_layout_width’, array(

'type' => 'range', 'section' => 'layout', 'label' => ‘Layout width', 'input_attrs' => array( 'min' => 720, 'max' => 1280, 'step' => 1, 'class' => ‘layout-width-control‘ ) ) );

Page 47: WordCamp Praga 2015

$wp_customize->add_control( ‘mytheme_layout_width’, array(

'type' => 'range', 'section' => 'layout', 'label' => ‘Layout width', 'input_attrs' => array( 'min' => 720, 'max' => 1280, 'step' => 1, 'class' => ‘layout-width-control‘ ) ) );

Page 48: WordCamp Praga 2015

$wp_customize->add_control( ‘mytheme_layout_width’, array(

'type' => 'range', 'section' => 'layout', 'label' => ‘Layout width', 'input_attrs' => array( 'min' => 720, 'max' => 1280, 'step' => 1, 'class' => ‘layout-width-control‘ ) ) );

Page 49: WordCamp Praga 2015
Page 50: WordCamp Praga 2015
Page 51: WordCamp Praga 2015
Page 52: WordCamp Praga 2015
Page 53: WordCamp Praga 2015

But there is a small problem…

Page 54: WordCamp Praga 2015

Source: http://caniuse.com/#search=date

Page 55: WordCamp Praga 2015

Custom controls

Page 56: WordCamp Praga 2015

class My_Customize_Textarea_Control extends WP_Customize_Control {

}

Page 57: WordCamp Praga 2015

class My_Customize_Textarea_Control extends WP_Customize_Control {public $type = 'textarea';

}

Page 58: WordCamp Praga 2015

class My_Customize_Textarea_Control extends WP_Customize_Control {public $type = 'textarea'; public function render_content() {?>

<?php}

}

Page 59: WordCamp Praga 2015

class My_Customize_Textarea_Control extends WP_Customize_Control {public $type = 'textarea'; public function render_content() {?>

<label><?php if(!empty($this->label)) : ?><span class=“customize-control-title">

<?php echo esc_html( $this->label ); ?></span><?php endif; ?>

</label><?php}

}

Page 60: WordCamp Praga 2015

class My_Customize_Textarea_Control extends WP_Customize_Control {public $type = 'textarea'; public function render_content() {?>

<label><?php if(!empty($this->label)) : ?><span class=“customize-control-title">

<?php echo esc_html( $this->label ); ?></span><?php endif; ?><?php if(!empty($this->description)) : ?><span class="description customize-control-description”>

<?php echo $this->description ; ?></span><?php endif; ?>

</label><?php}

}

Page 61: WordCamp Praga 2015

class My_Customize_Textarea_Control extends WP_Customize_Control {public $type = 'textarea'; public function render_content() {?>

<label><?php if(!empty($this->label)) : ?><span class=“customize-control-title">

<?php echo esc_html( $this->label ); ?></span><?php endif; ?><?php if(!empty($this->description)) : ?><span class="description customize-control-description”>

<?php echo $this->description ; ?></span><?php endif; ?><textarea rows=“5" cols=“20” <?php $this->link(); ?>><?php echo esc_textarea($this->value()); ?></textarea>

</label><?php}

}

Page 62: WordCamp Praga 2015

class My_Customize_Textarea_Control extends WP_Customize_Control {public $type = 'textarea'; public function render_content() {?>

<label><?php if(!empty($this->label)) : ?><span class=“customize-control-title">

<?php echo esc_html( $this->label ); ?></span><?php endif; ?><?php if(!empty($this->description)) : ?><span class="description customize-control-description”>

<?php echo $this->description ; ?></span><?php endif; ?><textarea rows=“5" cols=“20” <?php $this->link(); ?>><?php echo esc_textarea($this->value()); ?></textarea>

</label><?php}

}

Page 63: WordCamp Praga 2015

$wp_customize->add_control(

);

Page 64: WordCamp Praga 2015

$wp_customize->add_control( new My_Customize_Textarea_Control(

));

Page 65: WordCamp Praga 2015

$wp_customize->add_control( new My_Customize_Textarea_Control( $wp_customize, ‘theme_copyright_text’,

));

Page 66: WordCamp Praga 2015

$wp_customize->add_control( new My_Customize_Textarea_Control( $wp_customize, ‘theme_copyright_text’,

array( 'label' => __(‘Copyright text', 'theme'), 'section' => 'features',

'settings' => ‘theme_copyright_text’, 'priority' => 2

))

);

Page 67: WordCamp Praga 2015

How to use context?

Page 68: WordCamp Praga 2015

• Hiding unnecessary options

Page 69: WordCamp Praga 2015

• Hiding unnecessary options

• Options dedicated to specific subpages

Page 70: WordCamp Praga 2015

• Hiding unnecessary options

• Options dedicated to specific subpages

• Creating dependencies between options

Page 71: WordCamp Praga 2015
Page 72: WordCamp Praga 2015
Page 73: WordCamp Praga 2015

$wp_customize->add_control('mytheme_google_font',array(

'section' => 'mytheme_font_options','label' => __('Google Font URL', 'mytheme'),'type' => 'text','active_callback' => 'mytheme_show_font_field'

));

Page 74: WordCamp Praga 2015

function mytheme_show_font_field($control) {

}

Page 75: WordCamp Praga 2015

function mytheme_show_font_field($control) {$option = $control->manager->get_setting(‘mytheme_font');

}

Page 76: WordCamp Praga 2015

function mytheme_show_font_field($control) {$option = $control->manager->get_setting(‘mytheme_font');

return $option->value() == 'google';}

Page 77: WordCamp Praga 2015

Problems with active_callback

is_front_page will work fine as:

“active_callback” => “is_front_page”

Page 78: WordCamp Praga 2015

Problems with active_callback

is_front_page will work fine as:

“active_callback” => “is_front_page”

but is_single or is_singular won’t, because they take an argument which in this case is a handler to

the panel/section/control

Page 79: WordCamp Praga 2015

Problems with active_callback

function PREFIX_is_single() {

return is_single();

}

Page 80: WordCamp Praga 2015

Live preview

Page 81: WordCamp Praga 2015
Page 82: WordCamp Praga 2015

$wp_customize->add_setting( 'mytheme_primary_color', array(

'default' => '#5cc1a9', 'capability' => 'edit_theme_options',

'transport' => 'postMessage', 'sanitize_callback' => 'sanitize_hex_color'

));

Page 83: WordCamp Praga 2015

$wp_customize->add_setting( 'mytheme_primary_color', array(

'default' => '#5cc1a9', 'capability' => 'edit_theme_options',

'transport' => 'postMessage', 'sanitize_callback' => 'sanitize_hex_color'

));

Page 84: WordCamp Praga 2015

function mytheme_customize_preview_js() {wp_enqueue_script(

'mytheme-customize-preview', get_template_directory_uri() . '/js/customize-preview.js', array(), ‘1.0', true

);}

Page 85: WordCamp Praga 2015

function mytheme_customize_preview_js() {wp_enqueue_script(

'mytheme-customize-preview', get_template_directory_uri() . '/js/customize-preview.js', array(), ‘1.0', true

);}

add_action('customize_preview_init', ‘mytheme_customize_preview_js'

);

Page 86: WordCamp Praga 2015

(function($){

})(jQuery);

Page 87: WordCamp Praga 2015

(function($){ wp.customize(

'mytheme_primary_color', function(value) {

} );})(jQuery);

Page 88: WordCamp Praga 2015

(function($){ wp.customize(

'mytheme_primary_color', function(value) {

value.bind(function(to) { }); } );})(jQuery);

Page 89: WordCamp Praga 2015

(function($){ wp.customize(

'mytheme_primary_color', function(value) {

value.bind(function(to) { jQuery('a').css('color', to || '#5cc1a9'); }); } );})(jQuery);

Page 90: WordCamp Praga 2015

Optimisation

Page 91: WordCamp Praga 2015

wp.customize('mytheme_primary_color', function(value) { value.bind( function( to ) {

} );});

Page 92: WordCamp Praga 2015

wp.customize('mytheme_primary_color', function(value) { value.bind( function( to ) { to = to || '#5cc1a9';

} );});

Page 93: WordCamp Praga 2015

wp.customize('mytheme_primary_color', function(value) { value.bind( function( to ) { to = to || '#5cc1a9'; var new_css = 'a { color: ' + to + '; }';

} );});

Page 94: WordCamp Praga 2015

wp.customize('mytheme_primary_color', function(value) { value.bind( function( to ) { to = to || '#5cc1a9'; var new_css = 'a { color: ' + to + '; }'; if($('#new-css').length) { $('#new-css').remove(); }

} );});

Page 95: WordCamp Praga 2015

wp.customize('mytheme_primary_color', function(value) { value.bind( function( to ) { to = to || '#5cc1a9'; var new_css = 'a { color: ' + to + '; }'; if($('#new-css').length) { $('#new-css').remove(); } $(document).find('head') .append($('<style id="new-css">' + new_css + '</style>')); }

);});

Page 96: WordCamp Praga 2015

Results

BEFORE

• potentially thousands of DOM operations

• manipulations with style attributes

AFTER

• maximum 2 DOM operations

• no manipulations with style attributes

Page 97: WordCamp Praga 2015

JavaScript API

Page 98: WordCamp Praga 2015

Possibilities

• Access to all elements

Page 99: WordCamp Praga 2015

Possibilities

• Access to all elements

• Re-render of existing elements

Page 100: WordCamp Praga 2015

Possibilities

• Access to all elements

• Re-render of existing elements

• Modification of existing elements

Page 101: WordCamp Praga 2015

Possibilities

• Access to all elements

• Re-render of existing elements

• Modification of existing elements

• Event tracking and control over the preview area

Page 102: WordCamp Praga 2015

function PREFIX_theme_customizer_tooltips() { ?>

<?php} add_action('customize_controls_print_scripts', ‘PREFIX_theme_customizer_tooltips');

Page 103: WordCamp Praga 2015

function PREFIX_theme_customizer_tooltips() { ?> <script type="text/javascript"> jQuery(document).ready(function() {

}); </script> <?php} add_action('customize_controls_print_scripts', ‘PREFIX_theme_customizer_tooltips');

Page 104: WordCamp Praga 2015

function PREFIX_theme_customizer_tooltips() { ?> <script type="text/javascript"> jQuery(document).ready(function() { wp.customize.bind('ready', function() { // Your Theme Customizer Code }); }); </script> <?php} add_action('customize_controls_print_scripts', ‘PREFIX_theme_customizer_tooltips');

Page 105: WordCamp Praga 2015

Examples

Page 106: WordCamp Praga 2015

wp.customize.section("colors").expand();

Examples

Page 107: WordCamp Praga 2015

wp.customize.section("colors").expand();

wp.customize.control("blogname").priority(100);

Examples

Page 108: WordCamp Praga 2015

wp.customize.section("colors").expand();

wp.customize.control("blogname").priority(100);

wp.customize.control("blogname").section("colors");

Examples

Page 109: WordCamp Praga 2015

wp.customize.section("colors").expand();

wp.customize.control("blogname").priority(100);

wp.customize.control("blogname").section("colors");

var ctrl = wp.customize.control("background_color");ctrl.params.label = "New label";ctrl.renderContent();ctrl.ready();

Examples

Page 110: WordCamp Praga 2015

Reading list• https://www.gavick.com/blog/live-preview-themes-colors-changes-theme-

customizer

• https://www.gavick.com/blog/contextual-controls-theme-customizer

• https://www.gavick.com/blog/theme-customization-controls

• https://www.gavick.com/blog/custom-control-wordpress-theme-customizer

• https://www.gavick.com/blog/custom-category-selection-controls

• https://www.gavick.com/blog/using-javascript-theme-customization-screen

• https://www.gavick.com/blog/using-tooltips-instead-option-descriptions-wordpress-theme-customizer

• https://www.gavick.com/blog/internal-linking-wordpress-theme-customizer

Page 111: WordCamp Praga 2015

Děkuji!

Page 112: WordCamp Praga 2015

Questions?

Page 113: WordCamp Praga 2015

This presentation is available under the GPL license:

http://www.gnu.org/copyleft/gpl.html