WP Customizer – Checkbox
January 13, 2023
Add to functions.php
function fishoa_add_checkbox($wp_customize){
// Add section
$wp_customize->add_section( 'fishoa_checkbox_section', array(
'title' => 'My Custom Checkbox',
'priority' => 10,
'description' => 'Welcome to My Custom Checkbox section'
));
// Add setting
$wp_customize->add_setting( 'fishoa_checkbox', array(
'default' => '',
));
// Add control
$wp_customize->add_control( 'fishoa_checkbox_control', array(
'label' => 'Display Welcome Message',
'type' => 'checkbox',
'section' => 'fishoa_checkbox_section',
'settings' => 'fishoa_checkbox'
));
}
add_action( 'customize_register', 'fishoa_add_checkbox' );
In the view file, you need to create a variable for the checkbox
$showMessage = get_theme_mod( 'fishoa_checkbox' );
$visible = no_visible; // name of the class for no visible
if( $showMessage == true ){
$visible = is_visible; // name of the class for visible
}
After set the variable, you have to print it in the HTML tag
<div class="home <?php echo $visible; ?>">