Skip to content

Instantly share code, notes, and snippets.

@damiencarbery
Last active September 9, 2023 02:53
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save damiencarbery/9117a1810940949ccd87194d89742864 to your computer and use it in GitHub Desktop.
Save damiencarbery/9117a1810940949ccd87194d89742864 to your computer and use it in GitHub Desktop.
CMB2 Repeater Demo - a simple example
<?php
add_filter( 'the_content', 'crd_append_post_links' );
function crd_append_post_links( $content ) {
if ( is_page() ) {
$post_links_data = get_post_meta( get_the_ID() );
if ( isset( $post_links_data[ 'blog_group' ][ 0 ] ) ) {
$blog_list = maybe_unserialize( $post_links_data[ 'blog_group' ][ 0 ] );
$posts_list = '<ul>';
foreach ( $blog_list as $post_info ) {
$posts_list .= sprintf( '<li><a href="%s">%s</a></li>', $post_info[ 'url' ], $post_info[ 'title' ] );
}
$posts_list .= '</ul>';
return $content . $posts_list;
}
}
return $content;
}
<?php
add_action( 'cmb2_admin_init', 'crd_repeater_metaboxes' );
/**
* Define the metabox and field configurations.
*/
function crd_repeater_metaboxes() {
/**
* Initiate the metabox
*/
$cmb = new_cmb2_box( array(
'id' => 'repeater_demo', // Belgrove Bouncing Castles
'title' => 'Repeater Demo',
'object_types' => array( 'page', ), // Post type
'context' => 'normal',
'priority' => 'high',
'show_names' => true, // Show field names on the left
) );
$blog_group_id = $cmb->add_field( array(
'id' => 'blog_group',
'type' => 'group',
'repeatable' => true,
'options' => array(
'group_title' => 'Post {#}',
'add_button' => 'Add Another Post',
'remove_button' => 'Remove Post',
'closed' => true, // Repeater fields closed by default - neat & compact.
'sortable' => true, // Allow changing the order of repeated groups.
),
) );
$cmb->add_group_field( $blog_group_id, array(
'name' => 'Post Title',
'desc' => 'Enter the post title for the link text.',
'id' => 'title',
'type' => 'text',
) );
$cmb->add_group_field( $blog_group_id, array(
'name' => 'Post URL',
'desc' => 'Enter the url of the post.',
'id' => 'url',
'type' => 'text_url',
) );
}
<?php
/*
Plugin Name: CMB2 Repeater Demo
Plugin URI: http://www.damiencarbery.com
Description: Demo using repeater feature of CMB2.
Author: Damien Carbery
Version: 0.2
*/
add_action( 'cmb2_admin_init', 'crd_repeater_metaboxes' );
/**
* Define the metabox and field configurations.
*/
function crd_repeater_metaboxes() {
/**
* Initiate the metabox
*/
$cmb = new_cmb2_box( array(
'id' => 'repeater_demo', // Belgrove Bouncing Castles
'title' => 'Repeater Demo',
'object_types' => array( 'page', ), // Post type
'context' => 'normal',
'priority' => 'high',
'show_names' => true, // Show field names on the left
) );
$blog_group_id = $cmb->add_field( array(
'id' => 'blog_group',
'type' => 'group',
'repeatable' => true,
'options' => array(
'group_title' => 'Post {#}',
'add_button' => 'Add Another Post',
'remove_button' => 'Remove Post',
'closed' => true, // Repeater fields closed by default - neat & compact.
'sortable' => true, // Allow changing the order of repeated groups.
),
) );
$cmb->add_group_field( $blog_group_id, array(
'name' => 'Post Title',
'desc' => 'Enter the post title for the link text.',
'id' => 'title',
'type' => 'text',
) );
$cmb->add_group_field( $blog_group_id, array(
'name' => 'Post URL',
'desc' => 'Enter the url of the post.',
'id' => 'url',
'type' => 'text_url',
) );
}
add_filter( 'the_content', 'crd_append_post_links' );
function crd_append_post_links( $content ) {
if ( is_page() ) {
$post_links_data = get_post_meta( get_the_ID() );
if ( isset( $post_links_data[ 'blog_group' ][ 0 ] ) ) {
$blog_list = maybe_unserialize( $post_links_data[ 'blog_group' ][ 0 ] );
$posts_list = '<ul>';
foreach ( $blog_list as $post_info ) {
$posts_list .= sprintf( '<li><a href="%s">%s</a></li>', $post_info[ 'url' ], $post_info[ 'title' ] );
}
$posts_list .= '</ul>';
return $content . $posts_list;
}
}
return $content;
}
// Verify that CMB2 plugin is active.
add_action( 'admin_notices', 'verify_cmb2_active' );
function verify_cmb2_active() {
if ( ! defined( 'CMB2_LOADED' ) ) {
$plugin_data = get_plugin_data( __FILE__ );
$plugin_name = $plugin_data['Name'];
?>
<div class="notice notice-warning is-dismissible"><p>Plugin <strong><?php echo $plugin_name; ?></strong> requires <a href="https://wordpress.org/plugins/cmb2/">CMB2 plugin</a>.</p></div>
<?php
//error_log( 'CMB2 is not active.' );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment