Giter Club home page Giter Club logo

cmb2-meta-split's Introduction

CMB2 Meta Split

A Custom Meta Boxes 2 plugin addition that will take care of saving non serialized meta for repeatable fields and groups

Caveat

Thes meta splitting will only apply to posts (of any type, not just post and page) and users. It will not apply to option pages.

Installation

Download, copy and paste into WordPress plugin folder and activate; this plugin has no UI.

Usage

The plugin exposes no template tags or functions and will simply save non serialized meta in the database.
The code below will add a repeatable text field to the page, id my_text, and a group field with the id of my_group; any entry in the repeatable text field will be saved to the my_text_split meta while entries in the group field will be saved in the my_group_title and my_group_description meta.

add_action( 'cmb2_init', 'yourprefix_register_repeatable_group_field_metabox' );
/**
 * Hook in and add a metabox to demonstrate repeatable grouped fields
 */
function yourprefix_register_repeatable_group_field_metabox() {

	/**
	 * Repeatable Field Groups
	 */
	$cmb_group = new_cmb2_box( array(
		'id'           => 'metabox',
		'title'        => __( 'Repeating Field Group', 'cmb2' ),
		'object_types' => array( 'page', ),
	) );

	$cmb_group->add_field( array(
		'name'       => __( 'Repeatable text', 'cmb2' ),
		'desc'       => __( 'field description (optional)', 'cmb2' ),
		'id'         => 'my_text',
		'type'       => 'text_small',
		'repeatable' => true,
		'on_front'   => false,
	) );

	$group_field_id = $cmb_group->add_field( array(
		'id'          => 'my_group',
		'type'        => 'group',
		'description' => __( 'Generates reusable form entries', 'cmb2' ),
		'options'     => array(
			'group_title'   => __( 'Entry {#}', 'cmb2' ),
			'add_button'    => __( 'Add Another Entry', 'cmb2' ),
			'remove_button' => __( 'Remove Entry', 'cmb2' ),
			'sortable'      => true
		),
	) );

	$cmb_group->add_group_field( $group_field_id, array(
		'name' => __( 'Entry Title', 'cmb2' ),
		'id'   => 'title',
		'type' => 'text',
	) );

	$cmb_group->add_group_field( $group_field_id, array(
		'name'        => __( 'Description', 'cmb2' ),
		'description' => __( 'Write a short description for this entry', 'cmb2' ),
		'id'          => 'description',
		'type'        => 'textarea_small',
	) );
}

Split meta postfix

The _split postfix might not be ideal for any application so the plugin comes with the cmb2_meta_split_postfix filter to allow for the customization of that.

Nopslit

By defaul the meta splitting will apply to any repeatable or group field but that might not be the desired outcome; the solution if two-fold: either apply a filter to the cmb2_meta_split_applies filter (return false to prevent the splitting from happening) or set the nosplit parameter on a field.

cmb2-meta-split's People

Contributors

lucatume avatar noahott avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

noahott

cmb2-meta-split's Issues

a more generic solution

class cmb2_fix {
    const TYPES = ['term', 'user'];

    static private $_instance;function instance() {if(!isset(self::$_instance)) self::$_instance =  new self();return self::$_instance;}

    function __construct() {

        add_filter('cmb2_override_meta_value', function($data, $object_id, $a, $instance) {
            if(!in_array($a['type'], self::TYPES) || !$a['repeat']) return $data;
            $data = get_metadata( $a['type'], $a['id'], $a['field_id'], false );
            return $data;
        }, 10, 4);

        add_filter( 'cmb2_override_meta_save', function($override, $a, Array $args, $instance ) {
            if(!in_array($a['type'], self::TYPES) || !$a['repeat']) return $override;
            $id = delete_metadata($a['type'], $a['id'], $a['field_id']);
            if(!empty($a['value'])) foreach($a['value'] as $value) $id = add_metadata( $a['type'], $a['id'], $a['field_id'], $value, false );
            return $id ? $id : false;
        }, 10, 4);

    } //__construct
} //cmb2_fix
cmb2_fix::instance();

Generate Meta Split programatically

I'm using WP All import to import a bunch of posts. I'd like to have the import automatically generate the split post meta when it is done, but I can't get this to work.

Do you know how it would be possible to trigger this programmatically? I've tried calling wp_update_post() and do_action('save_post', $post_id, $post, true) but neither of these methods end up triggering CMB2_Meta_Split->met_save().

When I manually click the "update" button when editing a post the meta split data is generated correctly.

I appreciate any help or advice you can give.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.