2017 © Pedro Peláez
 

wordpress-plugin cmb2-flexible-content

A flexible content field for CMB2.

image

reaktivstudios/cmb2-flexible-content

A flexible content field for CMB2.

  • Friday, July 7, 2017
  • by jjeaton
  • Repository
  • 6 Watchers
  • 12 Stars
  • 295 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 3 Open issues
  • 4 Versions
  • 1 % Grown

The README.md

CMB2 Flexible Field

The CMB2 Flexible Field allows the creation of group layouts, which can then be selected and switched between by a user. Each layout group contains a set of existing CMB2 fields, defined in an array in the field setup. These layout groups are then toggleable, sortable and repeatable., (*1)

Screenshot of UI, (*2)

The data itself is saved to the field's ID in a serialized and iterable array with the layout group ID attached., (*3)

Adding a Flexible Field

Adding a flexible field is the same as adding any other CMB2 field. The only addition is the layouts option, which should contain an array of arrays, each with a separate group., (*4)

To begin, set up a standard CMB2 metabox:, (*5)

// Basic CMB2 Metabox declaration
$cmb = new_cmb2_box( array(
    'id'           => 'prefix-metabox-id',
    'title'        => __( 'Flexible Content Test' ),
    'object_types' => array( 'post', ),
) );

Then add your flexible field definition. Each layout group should be defined in the layouts array, with the `ID` for that group as its key. Each layout group can contain a `title` and a list of CMB2 `fields`.

// Sample Flexible Field
$cmb->add_field( array(
    'name'       => __( 'Test Flexible', 'cmb2-flexible' ),
    'desc'       => __( 'field description (optional)', 'cmb2-flexible' ),
    'id'         => 'prefix_flexible',
    'type'       => 'flexible',
    'layouts' => array(
            'text' => array(
                'title' => 'Text Group',
                'fields' => array(
                    array(
                        'type' => 'text',
                        'name' => 'Title for Text Group',
                        'id' => 'title',
                    ),
                    array(
                        'type' => 'textarea',
                        'name' => 'Description for Text Group',
                        'id' => 'description',
                    )
                ),
            ),
            'image' => array(
                'title' => 'Image Group',
                'fields' => array(
                    array(
                        'type' => 'file',
                        'name' => 'Image for Image Group',
                        'id' => 'title',
                    ),
                    array(
                        'type' => 'textarea',
                        'name' => 'Description for Image Group',
                        'id' => 'description',
                    )
                ),
            ),
        )
) );

Getting Data from a Flexible Field

Flexible fields are stored in a single meta key, and can be retrieved using the WordPress API. This will return an array of data, each with the layout key defined as layout and the group's data by field ID., (*6)

$flexible_fields = get_post_meta( $post_id, 'flexible_field_name', true );
foreach( $flexible_fields as $field ) {
    if ( 'text' === $field['layout'] ) { ?>
        <h2><?php echo esc_html( $field['title'] ); ?></h2>
        <?php echo esc_html( $field['description'] );
    }
}

The Versions

07/07 2017

dev-master

9999999-dev https://github.com/reaktivstudios/cmb2-flexible-content

A flexible content field for CMB2.

  Sources   Download

GPL-2.0

The Requires

  • php >=5.3.0

 

05/07 2017

dev-develop

dev-develop https://github.com/reaktivstudios/cmb2-flexible-content

A flexible content field for CMB2.

  Sources   Download

GPL-2.0

The Requires

  • php >=5.3.0

 

05/07 2017

dev-one-big-group

dev-one-big-group https://github.com/reaktivstudios/cmb2-flexible-content

A flexible content field for CMB2.

  Sources   Download

GPL-2.0

The Requires

  • php >=5.3.0

 

04/07 2017

dev-plugin-structure

dev-plugin-structure https://github.com/reaktivstudios/cmb2-flexible-content

A flexible content field for CMB2.

  Sources   Download

GPL-2.0

The Requires

  • php >=5.3.0