WP Manifestindependent plugin directory
manifest / content / cmb2-field-unit

CMB2 Field Type: Unit

CMB2 field type to setup unit values (font size, line height, etc).

by Ruben Garcia <rubengcdev@gmail.com> · github.com/rubengc/cmb2-field-unit · website

2stars
1forks

Install

No release zip yet. The repository archive installs, but the folder name will carry the branch suffix and updates will not flow:

wp plugin install https://github.com/rubengc/cmb2-field-unit/archive/refs/heads/master.zip

Declares an update source (https://github.com/rubengc/cmb2-field-unit), so updates arrive through the plugin's own updater.

Readme

CMB2 Field Type: Unit

Custom field for CMB2 to store a unit values (font size, line height, etc).

example

Examples

add_action( 'cmb2_admin_init', 'cmb2_unit_metabox' );
function cmb2_unit_metabox() {

    $prefix = 'your_prefix_demo_';

    $cmb_demo = new_cmb2_box( array(
        'id'            => $prefix . 'metabox',
        'title'         => __( 'Unit Metabox', 'cmb2' ),
        'object_types'  => array( 'page', 'post' ), // Post type
    ) );

    $cmb_demo->add_field( array(
        'name'          => __( 'Default field', 'cmb2' ),
        'desc'          => __( 'Field description (optional)', 'cmb2' ),
        'id'            => $prefix . 'unit',
        'type'          => 'unit',
        // Custom units (units by default are 'px', 'em' and 'rem'
        'units'     => array(
            'px' => 'px',
            'em' => 'em',
        )
    ) );

}

Retrieve the field value

    $post_meta = get_post_meta( get_the_ID(), 'your_field_id', false );

    $value = isset( $post_meta['value'] ) ? $post_meta['value'] : 0';
    $unit = isset( $post_meta['unit'] ) ? $post_meta['unit'] : 'px';

    echo 'font-size: ' . $value . $unit . ';';

Changelog

1.0.0

  • Initial commit

Read the full README on GitHub →