Skip to content

Instantly share code, notes, and snippets.

@dcondrey

dcondrey/control.php

Last active Jan 28, 2018
Embed
What would you like to do?
WP Customizer / Custom Controls / Sanitization
<?php
class litho_textarea_control extends WP_Customize_Control {
public $type = 'textarea';
public function render_content() { ?>
<label>
<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
<textarea rows="5" style="width:100%;" <?php $this->link(); ?>><?php echo esc_textarea( $this->value() ); ?></textarea>
</label>
<?php }
}
class litho_slider_control extends WP_Customize_Control {
public $type = 'slider';
public function render_content() { ?>
<label>
<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
<div id="slider">
<input id="slide" type="range" min="1" max="100" step="1" value="<?php echo esc_attr($this->value()); ?>" onchange="updateSlider(<?php $this->link(); checked( $this->value() ); ?>)" <?php $this->link(); ?> />
</div>
</label>
<?php }
}
class litho_toggle_control extends WP_Customize_Control {
public $type = 'toggle';
public function render_content() { ?>
<label>
<input type="checkbox" class="ios-switch" value="<?php echo esc_attr($this->value()); ?>"
<?php $this->link(); checked( $this->value() ); ?> />
<div class="switch"></div>
<?php echo esc_html( $this->label ); ?>
</label>
<?php }
}
<?php
/** Adds the Customize page to the WordPress admin area */
function litho_customizer_menu() {
add_theme_page( 'Customize', 'Customize', 'edit_theme_options', 'customize.php' );
}
add_action( 'admin_menu', 'litho_customizer_menu' );
/** Adds the individual sections, settings, and controls to the theme customizer */
function litho_customizer( $wp_customize ) {
if (class_exists('WP_Customize_Control')) {
include_once('control.php');
}
$this->add_section( 'header_image', array('title' => __( 'Site Header' ),'theme_supports' => 'custom-header','priority' => 1 ));
$this->add_section( 'title_tagline', array('title' => __( 'Labels' ),'priority' => 2 ));
$this->add_section( 'colors', array('title' => __( 'Colors' ),'priority' => 40 ));
$this->add_section( 'background_image', array('title' => __( 'Background Image' ),'theme_supports' => 'custom-background','priority' => 80 ));
$this->add_section( 'nav', array('title' => __( 'Navigation' ),'theme_supports' => 'menus','priority' => 100,'description' => sprintf( _n('Your theme supports %s menu. Select which menu you would like to use.', 'Your theme supports %s menus. Select which menu appears in each location.', $num_locations ), number_format_i18n( $num_locations ) ) . "\n\n" . __('You can edit your menu content on the Menus screen in the Appearance section.') ));
$this->add_section( 'static_front_page', array('title' => __( 'Static Front Page' ),'priority' => 120,'description' => __( 'Your theme supports a static front page.' ) ));
$wp_customize->add_setting( 'litho_logo' );
$wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, 'litho_logo',array(
'label' => 'Site Logo',
'section' => 'header_image',
'settings' => 'litho_logo'
)));
$wp_customize->add_setting( 'litho_header_height' );
$wp_customize->add_control( 'litho_header_height',array(
'type' => 'slider',
'label' => 'Header Height',
'section' => 'header_image'
));
$wp_customize->add_setting( 'litho_logo_position', array( 'default' => 'left','sanitize_callback' => 'litho_sanitize_position', ) );
$wp_customize->add_control( 'litho_logo_position',array(
'type' => 'radio',
'label' => 'Logo Position',
'section' => 'header_image',
'choices' => array( 'left' => 'left','right' => 'right','center' => 'center')
));
$wp_customize->add_setting( 'litho_subtitle', array( 'default' => 'Default subtitle text','sanitize_callback' => 'litho_sanitize_text', ) );
$wp_customize->add_control('litho_subtitle', array(
'label' => 'Site Subtitle',
'section' => 'header_image',
'type' => 'text'
));
$wp_customize->add_setting( 'litho_subtitle_position', array( 'default' => 'left','sanitize_callback' => 'litho_sanitize_position', ) );
$wp_customize->add_control( 'litho_subtitle_position',array(
'type' => 'radio',
'label' => 'Subtitle Position',
'section' => 'header_image',
'choices' => array( 'left' => 'Left','right' => 'Right','center' => 'Center')
));
$wp_customize->add_setting( 'litho_subtitle_bgcolor', array( 'default' => '#000000','sanitize_callback' => 'sanitize_hex_color', ) );
$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'litho_subtitle_bgcolor',array(
'label' => 'Subtitle Background Color',
'section' => 'header_image',
'settings' => 'litho_subtitle_bgcolor'
)));
$wp_customize->add_section( 'litho_site_settings', array('title' => 'Framework Settings','description' => 'Adjust settings','priority' => 35));
$wp_customize->add_section( 'site_settings', array('title' => 'Header','description' => 'Adjust settings','priority' => 35));
$wp_customize->add_setting( 'litho_copyright_textbox',array( 'default' => 'Company name','sanitize_callback' => 'litho_sanitize_text', ) );
$wp_customize->add_control( 'litho_copyright_textbox',array(
'label' => 'Copyright text',
'section' => 'litho_site_settings',
'type' => 'text'
));
$wp_customize->add_setting( 'litho_logo_position', array( 'default' => 'left','sanitize_callback' => 'litho_sanitize_position', ) );
$wp_customize->add_control( 'litho_logo_position',array(
'type' => 'radio',
'label' => 'Logo placement',
'section' => 'litho_site_settings',
'choices' => array( 'left' => 'Left','right' => 'Right','center' => 'Center')
));
$wp_customize->add_setting( 'litho_powered_by', array( 'default' => 'wordpress','sanitize_callback' => 'litho_sanitize_powered_by', ) );
$wp_customize->add_control( 'litho_powered_by',array(
'type' => 'select',
'label' => 'This site is powered by:',
'section' => 'litho_site_settings',
'choices' => array( 'wordpress' => 'WordPress','nuclear-energy' => 'Nuclear Energy')
));
$wp_customize->add_setting( 'page-setting', array( 'sanitize_callback' => 'litho_sanitize_integer', ) );
$wp_customize->add_control( 'page-setting',array(
'type' => 'dropdown-pages',
'label' => 'Choose a page:',
'section' => 'litho_site_settings'
));
$wp_customize->add_setting( 'color-setting', array( 'default' => '#000000','sanitize_callback' => 'sanitize_hex_color', ) );
$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'color-setting',array(
'label' => 'Color Setting',
'section' => 'litho_site_settings',
'settings' => 'color-setting'
)));
$wp_customize->add_setting( 'file-upload' );
$wp_customize->add_control( new WP_Customize_Upload_Control( $wp_customize, 'file-upload',array(
'label' => 'File Upload',
'section' => 'litho_site_settings',
'settings' => 'file-upload'
)));
$wp_customize->add_setting( 'textarea' );
$wp_customize->add_control( new Example_Customize_Textarea_Control( $wp_customize, 'textarea',array(
'label' => 'Textarea',
'section' => 'litho_site_settings',
'settings' => 'textarea'
)));
$wp_customize->add_setting( 'font-color', array( 'default' => '#444444', 'sanitize_callback' => 'sanitize_hex_color', ));
$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'font-color',array(
'label' => 'Font Color',
'section' => 'colors',
'settings' => 'font-color'
)));
$wp_customize->add_setting( 'featured-background',array( 'default' => '#ffffff','sanitize_callback' => 'sanitize_hex_color','transport' => 'postMessage', ));
$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize,'featured-background',array(
'label' => 'Featured Background',
'section' => 'colors',
'settings' => 'featured-background'
)));
/* AJAX function for live preview */
if ( $wp_customize->is_preview() && ! is_admin() ) {
add_action( 'wp_footer', 'litho_customize_preview', 21);
}
function litho_customize_preview() {
wp_enqueue_script( 'litho-customize-js', get_template_directory_uri().'/core/js/customize.js',array());
}
add_action( 'wp_enqueue_scripts', 'litho_customize_preview' );
$wp_customize->get_setting('blogname')->transport='postMessage';
}
add_action( 'customize_register', 'litho_customizer' );
function litho_header_alignment() {
$litho_position = get_theme_mod( 'litho_logo_position' );
if( $litho_position != '' ) {
switch ( $litho_position ) {
case 'left':
break;
case 'right':
echo '<style type="text/css">';
echo '#header #logo{ float: right; }';
echo '</style>';
break;
case 'center':
echo '<style type="text/css">';
echo '#header{ text-align: center; }';
echo '#header #logo{ float: none; margin-left: auto; margin-right: auto; }';
echo '</style>';
break;
}
}
}
add_action ( 'wp_head','litho_header_alignment' );
include_once('sanitize.php'); /* Validate user input */
<?php
function litho_sanitize_text( $input ) {
return wp_kses_post( force_balance_tags( $input ) );
}
function litho_sanitize_checkbox( $input ) {
if ( $input == 1 ) {
return 1;
} else {
return '';
}
}
function litho_sanitize_position( $input ) {
$valid = array(
'left' => 'Left',
'right' => 'Right',
'center' => 'Center',
);
if ( array_key_exists( $input, $valid ) ) {
return $input;
} else {
return '';
}
}
function litho_sanitize_powered_by( $input ) {
$valid = array(
'wordpress' => 'WordPress',
'nuclear-energy' => 'Nuclear Energy',
);
if ( array_key_exists( $input, $valid ) ) {
return $input;
} else {
return '';
}
}
function litho_sanitize_integer( $input ) {
if( is_numeric( $input ) ) {
return intval( $input );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment