Contributors: florianbeck
License: GPLv3 (http://www.gnu.org/licenses/gpl-3.0.html)
WordPress-Tags-like field type for Custom Metaboxes and Fields for WordPress using CMB2.
WordPress-Tags-like field type for CMB2. Based on WordPress Tags-Like Meta Box.
This plugin gives you two CMB2 field types:
- The
tags
field acts like WordPress's default tags list. - The
tags_sortable
field acts like WordPress's default tags list as well, but adds sorting.
The Entrys will be saved as single string separated with semicolons.
Note: This field may not support repetition, repeatable fields/groups is untested.
You can install this field type as you would a WordPress plugin:
- Download the plugin.
- Place the plugin folder in your
wp-content/plugins/
directory. - Activate the plugin in the Plugin dashboard.
Alternatively, you can place the plugin folder within your theme/plugin. After you call CMB2, add another line to include the cmb2-field-tags.php
file. Something like:
require_once 'cmb2-field-type-tags/cmb2-field-type-tags.php';
See CMB2's Wiki for general information and a basic example of how to create a metabox with CMB2.
- Create a metabox.
- Add the custom field by calling the
add_field
-function of the metabox:
tags
— WordPress-Tags-like input field. Example:
$cmb->add_field( array(
'name' => __( 'Related Publications', 'textdomain' ),
'desc' => __( 'Add or remove BibTeX-keys.', 'textdomain' ),
'id' => $prefix . 'related_publications',
'type' => 'tags',
) );
tags_sortable
— WordPress-Tags-like input field that allows sorting. Example:
$cmb->add_field( array(
'name' => __( 'Tasks', 'textdomain' ),
'desc' => __( 'Add or remove tasks, drag and drop to sort.', 'textdomain' ),
'id' => $prefix . 'tasks',
'type' => 'tags_sortable',
) );
You can get the entrys by get_post_meta( get_the_ID(), 'prefix_tasks', true ) );
The entrys are stored as one single string separated with semicolons. Example:
idea;concept designer;system designer;programmer;deploying artist
Generate an array. Example:
$tasks = explode( ';', get_post_meta( get_the_ID(), 'prefix_tasks', true ) );
foreach ( $tasks as $task ) {
printf('<li>%s</li>', $task);
}
Replace separator. Example:
$tasks = str_replace(';', ', ', get_post_meta( get_the_ID(), 'prefix_tasks', true ) );
echo $tasks;