Creating Your Own Controller (Field)

Customizer allows you to easily create your own fields (UI elements). Let's create support for the field type="range" (range slider). This field allows selecting values by dragging the slider.

Core controllers:

Basic fields:

Basic fields:

Nav Menu:

Widget:

Image:

Others:

To create a new field, you need to create a class based on WP_Customize_Control:

<?php

if( class_exists( 'WP_Customize_Control' ) ) {

	class WP_Customize_Range extends WP_Customize_Control {

		public $type = 'range';

		public function __construct( $manager, $id, $args = array() ) {

			parent::__construct( $manager, $id, $args );

			$defaults = [
				'min'  => 0,
				'max'  => 10,
				'step' => 1
			];

			$args = wp_parse_args( $args, $defaults );

			$this->min  = $args['min'];
			$this->max  = $args['max'];
			$this->step = $args['step'];
		}

		public function render_content(){
			?>
			<label>
				<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>

				<input class="range-slider"
					   min="<?php echo $this->min ?>"
					   max="<?php echo $this->max ?>"
					   step="<?php echo $this->step ?>"
					   type="range"
					<?php $this->link(); ?>
					   value="<?php echo esc_attr( $this->value() ); ?>"
					   oninput="jQuery(this).next('input').val( jQuery(this).val() )">

				<input type="text" value="<?php echo esc_attr( $this->value() ); ?>" onKeyUp="jQuery(this).prev('input').val( jQuery(this).val() )">

			</label>
			<?php
		}
	}

}

We extend the base class WP_Customize_Control. We declare the property $type and the method render_content(). In the method, we need to output the HTML element (form field), in which we should use the methods:

  • $this->value() — the current value of the field.
  • $this->link() — the name attribute of the field.

min, max, step — these are arbitrarily created properties that can then be used in the class.

Usage

Now an example of how to use our new controller to create a new field.

add_action( 'customize_register', 'my_customize_register' );

function my_customize_register( WP_Customize_Manager $wp_customize ){

	$section = 'section_id';
	$wp_customize->add_section( $section, [
		'title'    => 'My Settings',
		'priority' => 220, // at the bottom of customizer
		//'panel'    => $panel,
	] );

	$wp_customize->add_setting( 'my_range' , array(
		'default'   => 0,
		'transport' => 'postMessage',
	) );

	$wp_customize->add_control( new WP_Customize_Range( $wp_customize, 'my_range', [
		'label'   => 'My Range',
		'min'     => 10,
		'max'     => 9999,
		'step'    => 10,
		'section' => 'section_id',
	] ) );

}

Note that the section 'section_id' must already be created. How to do this see here.

Live Preview

All you need to do to support live preview is to update the field in HTML with the new value of the range using JavaScript.

wp.customize( 'my_range', function( value ) {
	value.bind( function( newval ) {
		$( '#my_range span' ).html( newval );
	} );
} );

As a result, we will get something like this: