Forked from damiencarbery/cmb2-repeater-demo-display-data.php
Created
January 19, 2020 22:09
-
-
Save pgroot91/3675332dfb167dd20ac8478b523b8c54 to your computer and use it in GitHub Desktop.
CMB2 Repeater Demo - a simple example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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', | |
) ); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: CMB2 Repeater Demo | |
Plugin URI: http://www.damiencarbery.com | |
Description: Demo using repeater feature of CMB2. | |
Author: Damien Carbery | |
Version: 0.1 | |
*/ | |
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment