abuyoyo/cmb2-switch-button

There is no license information available for the latest version (1.2.2) of this package.

CMB2 Switch Button

Installs: 187

Dependents: 1

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 3

Type:wordpress-plugin

1.2.2 2020-11-19 02:24 UTC

This package is auto-updated.

Last update: 2024-04-19 09:50:38 UTC


README

Custom Switch Button field type for CMB2 Metabox for WordPress.

Installation

You can install it as a plugin, or include the main file into your theme or plugin folder.

Usage:

add_action( 'cmb2_admin_init', 'create_your_metabox' );
if(!function_exists('create_your_metabox')){
  function create_your_metabox(){
    $prefix = '_slug_';

    $cmb2_metabox = new_cmb2_box( array(
        'id'            => $prefix . 'test_metabox',
        'title'         => esc_html__( 'Test Metabox', 'tmv' ),
        'object_types'  => array( 'page'), // Post type
        'priority'   => 'high',
        'context'    => 'normal',
    ) );

    $cmb2_metabox->add_field( array(
        'name'             => esc_html__( 'Dynamically Load', 'text-domain' ),
        'id'               => $prefix . 'metabox_id',
        'desc'             => esc_html__('','text-domain'),
        'type'	           => 'switch',
        'default'          => true, //If it's checked by default 
        'active_value'     => true,
        'inactive_value'   => false
    ) );
  }
}
  • If you set the active_value and inactive_value to a boolean value such as 1/0, true/false, just use it as below
$test_meta = get_post_meta($post->ID, '_slug_metabox_id', true);

if($test_meta){
  //Do something when it's checked;
}
  • If you set the active_value and inactive_value to the specific value other than the boolean value, let's say enable/disable, you can use it as:
$test_meta = get_post_meta($post->ID, '_slug_metabox_id', true);

if($test_meta === 'enable'){
  //Do something when it's checked;
}else{
  //Do something when it's unchecked;
}

Screenshot:

example_screenshot.gif

Follow us: