Skip to content

Instantly share code, notes, and snippets.

@richtabor
Last active January 11, 2022 16:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save richtabor/59afeeef9e54a9f3c2708c552773d84f to your computer and use it in GitHub Desktop.
Save richtabor/59afeeef9e54a9f3c2708c552773d84f to your computer and use it in GitHub Desktop.
<?php
/**
* Toggle Customizer Control
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// Exit if WP_Customize_Control does not exsist.
if ( ! class_exists( 'WP_Customize_Control' ) ) {
return null;
}
/**
* This class is for the toggle control in the Customizer.
*
* @access public
*/
class Login_Designer_Toggle_Control extends WP_Customize_Control {
/**
* The type of customize control.
*
* @access public
* @since 1.3.4
* @var string
*/
public $type = 'toggle';
/**
* Enqueue scripts and styles.
*
* @access public
* @since 1.0.0
* @return void
*/
public function enqueue() {
wp_enqueue_style( 'login-designer-toggle-control-styles', get_parent_theme_file_uri( 'customizer-controls/toggle/toggle.css' ), false, '1.0.0', 'all' );
wp_enqueue_script( 'login-designer-toggle-control-scripts', get_parent_theme_file_uri( 'customizer-controls/toggle/toggle.js' ), array( 'jquery' ), '1.0.0', true );
}
/**
* Add custom parameters to pass to the JS via JSON.
*
* @access public
* @since 1.0.0
* @return void
*/
public function to_json() {
parent::to_json();
// The setting value.
$this->json['id'] = $this->id;
$this->json['value'] = $this->value();
$this->json['link'] = $this->get_link();
$this->json['defaultValue'] = $this->setting->default;
}
/**
* Don't render the content via PHP. This control is handled with a JS template.
*
* @access public
* @since 1.0.0
* @return void
*/
public function render_content() {}
/**
* An Underscore (JS) template for this control's content.
*
* Class variables for this control class are available in the `data` JS object;
* export custom variables by overriding {@see WP_Customize_Control::to_json()}.
*
* @see WP_Customize_Control::print_template()
*
* @access protected
* @since 1.3.4
* @return void
*/
protected function content_template() {
?>
<label class="toggle">
<div class="toggle--wrapper">
<# if ( data.label ) { #>
<span class="customize-control-title">{{ data.label }}</span>
<# } #>
<input id="toggle-{{ data.id }}" type="checkbox" class="toggle--input" value="{{ data.value }}" {{{ data.link }}} <# if ( data.value ) { #> checked="checked" <# } #> />
<label for="toggle-{{ data.id }}" class="toggle--label"></label>
</div>
<# if ( data.description ) { #>
<span class="description customize-control-description">{{ data.description }}</span>
<# } #>
</label>
<?php
}
}
<?php
/**
* Customizer.
*
* @param WP_Customize_Manager $wp_customize the Customizer object.
*/
function login_designer_customize_register( $wp_customize ) {
// Add custom control.
require get_parent_theme_file_path( 'customizer-controls/toggle/class-login-designer-toggle.php' );
// Register the custom control type.
$wp_customize->register_control_type( 'Login_Designer_Toggle_Control' );
// Add an option to disable the logo.
$wp_customize->add_setting( 'login_designer[disable_logo]', array(
'default' => false,
'type' => 'option',
'transport' => 'postMessage',
'sanitize_callback' => 'login_designer_sanitize_checkbox',
) );
$wp_customize->add_control( new Login_Designer_Toggle_Control( $wp_customize, 'login_designer[disable_logo]', array(
'label' => esc_html__( 'Enable Logo', 'login-designer' ),
'section' => 'login_designer__section--styles',
'type' => 'toggle',
'settings' => 'login_designer[disable_logo]',
) ) );
}
<?php
/**
* Checkbox sanitization callback example.
*
* Sanitization callback for 'checkbox' type controls. This callback sanitizes `$checked`
* as a boolean value, either TRUE or FALSE.
*
* @param bool $checked Whether the checkbox is checked.
* @return bool Whether the checkbox is checked.
*/
function login_designer_sanitize_checkbox( $checked ) {
// Boolean check.
return ( ( isset( $checked ) && true === $checked ) ? true : false );
}
.customize-control-toggle .toggle--wrapper {
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
flex-direction: row;
-webkit-box-direction: normal;
-webkit-box-orient: horizontal;
-webkit-box-pack: start;
-webkit-flex-direction: row;
-ms-flex-direction: row;
-ms-flex-pack: start;
-webkit-justify-content: flex-start;
justify-content: flex-start;
}
.customize-control-toggle .toggle--wrapper .customize-control-title {
font-weight: 400;
margin-bottom: 7px;
vertical-align: middle;
-webkit-box-flex: 2;
-webkit-flex: 2 0 0;
-ms-flex: 2 0 0;
flex: 2 0 0;
}
.customize-control-toggle .toggle--wrapper input[type=checkbox] {
display: none;
}
.customize-control-toggle {
margin-bottom: 0
}
.customize-control-toggle .toggle--wrapper label {
background-color: #555d66;
border-radius: 14px;
cursor: pointer;
display: inline-block;
height: 20px;
outline: none;
position: relative;
right: 0px;
top: 2px;
-webkit-transition: background 0.2s ease;
transition: background 0.2s ease;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
width: 34px;
}
.customize-control-toggle .toggle--wrapper label::after,
.customize-control-toggle .toggle--wrapper label::before {
content: "";
display: block;
position: absolute;
}
.customize-control-toggle .toggle--wrapper label::after {
border: 2px solid #555d66;
border-radius: 50%;
box-sizing: border-box;
height: 12px;
left: 4px;
top: 4px;
-webkit-transition: background 0.2s ease, -webkit-transform 0.2s ease;
transition: background 0.2s ease, -webkit-transform 0.2s ease;
transition: transform 0.2s ease, background 0.2s ease;
transition: transform 0.2s ease, background 0.2s ease, -webkit-transform 0.2s ease;
width: 12px;
}
.customize-control-toggle .toggle--wrapper label::before {
background-color: #eee;
border-radius: 60px;
bottom: 2px;
left: 2px;
right: 2px;
top: 2px;
-webkit-transition: background 0.2s ease;
transition: background 0.2s ease;
}
.customize-control-toggle .toggle--wrapper input[type=checkbox]:checked + label {
background-color: #0085ba;
}
.customize-control-toggle .toggle--wrapper input[type=checkbox]:checked + label::after {
background-color: #0085ba;
border: 2px solid #fff;
-webkit-transform: translateX(14px);
-ms-transform: translateX(14px);
transform: translateX(14px);
}
.customize-control-toggle .toggle--wrapper input[type=checkbox]:checked + label::before {
background-color: #0085ba;
}
.customize-control-toggle:hover .toggle--wrapper label::before {
background-color: #d9dcdf;
}
( function( $, api ) {
api.controlConstructor['toggle'] = api.Control.extend( {
ready: function() {
var control = this;
this.container.on( 'change', 'input:checkbox', function() {
value = this.checked ? true : false;
control.setting.set( value );
} );
}
} );
} )( jQuery, wp.customize );
@kiwimarc
Copy link

It does not save to the database

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment