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

CMB2 Field Type: Content Wrap

Custom field for CMB2 to store a content wrap values (padding, margin or border width).

★ 6stars

Install

Download source zip No release zip yet

This is the repository archive. It installs, but the folder name will carry the branch suffix and updates will not flow.

Install straight from GitHub with WP-CLI:

wp plugin install https://github.com/rubengc/cmb2-field-content-wrap/archive/refs/heads/master.zip
No built-in updater. New versions won't appear under Dashboard → Updates. Install new releases yourself, or let Git Updater track the repository.

Custom field for CMB2 to store a content wrap values (padding, margin or border width).

example

Examples

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

    $prefix = 'your_prefix_demo_';

    $cmb_demo = new_cmb2_box( array(
        'id'            => $prefix . 'metabox',
        'title'         => __( 'Content Wrap 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 . 'content_wrap',
        'type'          => 'content_wrap',
        // Custom units (units by default are 'px', '%' and 'em'
        'units'     => array(
            'px' => 'px',
            '%' => '%',
        )
    ) );

}

Retrieve the field value

    $value = get_post_meta( get_the_ID(), 'your_field_id', false );
    $unit = isset( $value['unit'] ) ? $value['unit'] : 'px';

    if( isset( $value['all'] ) && ! empty( $value['all'] ) ) {
        echo 'padding: ' . $value['all'] . $unit . ';';
    } else {
        if( isset( $value['top'] ) && ! empty( $value['top'] ) ) {
            echo 'padding-top: ' . $value['top'] . $unit . ';<br>';
        }

        if( isset( $value['right'] ) && ! empty( $value['right'] ) ) {
            echo 'padding-right: ' . $value['right'] . $unit . ';<br>';
        }

        if( isset( $value['bottom'] ) && ! empty( $value['bottom'] ) ) {
            echo 'padding-bottom: ' . $value['bottom'] . $unit . ';<br>';
        }

        if( isset( $value['left'] ) && ! empty( $value['left'] ) ) {
            echo 'padding-left: ' . $value['left'] . $unit . ';<br>';
        }
    }

Changelog

1.0.1

  • Added the ability to define custom units

1.0.0

  • Initial commit