Skip to content

Instantly share code, notes, and snippets.

@hrsetyono
Last active January 18, 2021 11:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hrsetyono/ac02b00e38c437de7ad8a5fb9ac8d164 to your computer and use it in GitHub Desktop.
Save hrsetyono/ac02b00e38c437de7ad8a5fb9ac8d164 to your computer and use it in GitHub Desktop.
Helper class for modifying WordPress Customizer - https://wptips.dev/simplify-wp-customizer-api
<?php
/**
* Helper class for modifying WordPress Customizer
*/
class MyCustomizer {
private $customize; // $wp_customize object
private $section;
function __construct( $customize ) {
$this->customize = $customize;
}
/**
* Create section
*
* @param string $name - Name of the section.
* @param array $args - See https://developer.wordpress.org/reference/classes/wp_customize_section/__construct/
*/
function add_section( $name, $args = [] ) {
$args = wp_parse_args( $args, [
// format title from slug, if not given
'title' => ucwords( str_replace( ['_', '-'], ' ', $name ) )
] );
$this->customize->add_section( $name, $args );
}
/**
* Add setting and control
*
* @param string $name - Setting name
* @param string $args - Setting args with Control args under the key 'control' - https://developer.wordpress.org/reference/classes/wp_customize_setting/__construct/
*/
function add_setting( $name, $args ) {
// Default args
$args = wp_parse_args( $args, [
'type' => 'theme_mod',
'transport' => 'postMessage',
] );
$this->customize->add_setting( $name, $args );
// Default control args
$control_args = $args['control'];
$control_args = wp_parse_args( $control_args, [
'type' => 'text',
'label' => $this->to_title( $name ),
'settings' => $name,
// in case the section arg is placed inside setting's
'section' => $args['section'] ?? $control_args['section'] ??
trigger_error( __( 'MyCustomizer Error: ' . $name . ' setting needs section argument' ) ),
] );
$this->add_control( $name . '_control', $control_args );
}
/**
* Add control to the customizer
*
* @param string $name - Control name
* @param array $args - Control args - https://developer.wordpress.org/reference/classes/wp_customize_control/__construct/
*/
private function add_control( $name, $args ) {
$control = false;
// Check if the input type is complex
switch( $args['type'] ) {
case 'code_editor':
$args['editor_settings'] = array(
'codemirror' => array(
'mode' => $args['code_language'] ?? 'default'
),
);
$control = new \WP_Customize_Code_Editor_Control( $this->customize, $name, $args );
break;
case 'image':
$control = new \WP_Customize_Image_Control( $this->customize, $name, $args );
break;
case 'cropped_image':
$control = new \WP_Customize_Cropped_Image_Control( $this->customize, $name, $args );
break;
case 'color':
$control = new \WP_Customize_Color_Control( $this->customize, $name, $args );
break;
case 'upload':
$control = new \WP_Customize_Upload_Control( $this->customize, $name, $args );
break;
case 'background_image':
$control = new \WP_Customize_Background_Image_Control( $this->customize, $name, $args );
break;
case 'header_image':
$control = new \WP_Customize_Header_Image_Control( $this->customize, $name, $args );
break;
// text | select | radio | checkbox | textarea
// dropdown-pages | email | url | number | hidden | date
default:
break;
}
// if has complex control
if( $control ) {
$this->customize->add_control( $control );
} else {
$this->customize->add_control( $name, $args );
}
}
/**
* Convert slug to titleized case
*/
private function to_title( $slug ) {
return ucwords( str_replace( ['_', '-'], ' ', $slug ) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment