By: Anthony Skelton
ajskelton
Follow this Live Slideshow @
https://slides.com/ajskelton/customizer/live
The Customizer is everything.
- Matt Mullenweg
require get_template_directory() . '/inc/customizer.php';
function ajs_customize_register( $wp_customize ) {
// Do stuff with $wp_customize
}
add_action( 'customize_register', 'ajs_customize_register' );
Four main Customizer objects
Panels
Sections
Settings
Controls
Each Object has three methods
add_ get_ remove_
$wp_customize->get_panel();
$wp_customize->remove_section();
$wp_customize->add_setting();
$wp_customize->add_control();
$wp_customize->remove_setting( 'blogname');
$wp_customize->get_setting( 'blogname' )
->transport = 'postMessage';
$wp_customize->get_setting( 'blogdescription' )
->transport = 'postMessage';
$wp_customize->add_panel( 'panel_id', array(
'priority' => 10,
'title' => __( 'Panel Title' ),
'description' => __( 'Panel Description' ),
'capability' => 'edit_theme_options',
'theme_supports' => '',
) );
$wp_customize->add_section( 'section_id', array(
'priority' => 10,
'title' => __( 'Section Title' ),
'description' => __( 'Section Description' ),
'panel' => '',
'active_callback' => '',
) );
Title | ID | Priority (Order) |
---|---|---|
Site Title & Tagline | title_tagline | 20 |
Colors | colors | 40 |
Header Image | header_image | 60 |
Background Image | background_image | 80 |
Menus (Panel)* | menus | 100 |
Widgets (Panel) | widgets | 110 |
Static Front Page | static_front_page | 120 |
$wp_customize->add_setting( 'setting_id', array(
'type' => 'theme_mod',
'capability' => 'edit_theme_options',
'theme_supports' => '',
'default' => '',
'transport' => 'refresh',
'sanitize_callback' => '',
'validate_callback' => '',
) );
$wp_customize->add_control( 'setting_id', array(
'priority' => 10,
'label' => _( 'Control Label' ),
'description' => _( 'Control Description' ),
'type' => 'text',
'section' => 'section_id',
'input_attrs' => array(),
'active_callback' => '',
) );
text | textarea | date |
range | url | |
password | hidden | checkbox |
radio | select | dropdown-pages |
number | time | datetime |
week | search |
Enqueue Javascript with custom_preview_init
Require customize-preview and jquery dependencies
function ajs_preview_js() {
wp_enqueue_script(
'custom_css_preview',
'path/to/file.js',
array( 'customize-preview', 'jquery' )
);
}
add_action( 'customize_preview_init', 'ajs_preview_js' );
Use the wp.customize object
( function( $ ) {
wp.customize( 'setting_id', function ( value ) {
value.bind( function( to ) {
$( '#custom-theme-css' ).html( to );
} );
} );
} )( jQuery );
Don't go overboard.
Save complicated changes for Selective Refresh
$wp_customize->selective_refresh->add_partial(
'partial_id',
array(
'settings' => 'partial_id'
'selector' => '#css-selector',
'container_inclusive' => false,
'render_callback' => function() {
// template tag or php logic
},
'fallback_refresh' => true
)
);
$wp_customize->add_setting(
'backwardstext_setting_id',
array(
'capability' => 'edit_theme_options',
'transport' => 'postMessage',
'default' => 'Lorem Ipsum',
)
);
$wp_customize->add_control(
'backwardstext_setting_id',
array(
'type' => 'text',
'priority' => 10,
'section' => 'custom_section',
)
);
$wp_customize->selective_refresh->add_partial(
'backwardstext_setting_id',
array(
'selector' => '.backwards-text',
'render_callback' => 'ajs_customizer_partial_backwardstext',
)
);
function ajs_customizer_partial_backwardstext() {
echo strrev(get_theme_mod('backwardstext_setting_id'));
}
When sections are shown based on Customizer Preview Windows location
'active_callback' => 'is_front_page'
'active_callback' => function() {
return is_page();
}
Needs to return a truthy value
'active_callback' => 'radio_callback'
function radio_callback( $control ) {
$radio_setting = $control->manager->get_setting('demo_radio_control')->value();
$control_id = $control->id;
if( $control_id == 'choice_a_text' && $radio_setting == 'a' ) return true;
if( $control_id == 'choice_b_image' && $radio_setting == 'b' ) return true;
return false;
}
'sanitize_callback' => 'sanitize_text_field'
$wp_customize->add_setting( 'radio_setting_id', array(
'default' => 'blue',
'sanitize_callback' => 'ajs_customizer_sanitize_radio',
) );
$wp_customize->add_control( 'radio_setting_id', array(
'type' => 'radio',
'section' => 'custom_section',.
'label' => __( 'Custom Radio Selection' ),
'description' => __( 'This is a custom radio input.' ),
'choices' => array(
'red' => __( 'Red' ),
'blue' => __( 'Blue' ),
'green' => __( 'Green' ),
),
) );
function ajs_customizer_sanitize_radio( $input ) {
$valid = array(
'red' => __( 'Red' ),
'blue' => __( 'Blue' ),
'green' => __( 'Green' ),
);
if( array_key_exists( $input, $valid ) ) {
return $input;
} else {
return '';
}
}
function validate_established_year( $validity, $value ) {
$value = intval( $value );
if ( empty( $value ) || ! is_numeric( $value ) ) {
$validity->add( 'required', __( 'You must supply a valid year.' ) );
} elseif ( $value < 1900 ) {
$validity->add( 'year_too_small', __( 'Year is too old.' ) );
} elseif ( $value > gmdate( 'Y' ) ) {
$validity->add( 'year_too_big', __( 'Year is too new.' ) );
}
return $validity;
}
Use $validity and $value to add validation checks
'validate_callback' => 'validate_established_year'
function ajs_established_year() {
echo get_theme_mod('established_year');
}
$wp_customize->selective_refresh->add_partial(
'established_year',
array(
'selector' => '.established-year',
'render_callback' => 'ajs_established_year',
)
);
Add Partial
Add function for render_callback