I am creating a plugin called BANDS for my personal site which basically sits under PAGES and has added metaboxes to add band details such as videos, soundcloud & crucially Festival Appearances.
I have now created the plugin and it works really well for what I need. I have a new menu item called 'bands' for all my pages and the metaboxes in admin I require for the extra information.
I have one or 2 small issues left - 1 which is really vexing me! As you will see in the code below - I have one section called "Festival Appearances" which has a field for the festival name & then then the date of the show. I want to make this field a REPEATABLE field so the user can click a "add another +" link and get a new meta box to enter a 2nd festival appearance.
I have seen plenty of code to do it but am having a real hard time mixing it with what I have.
Along the way I wouldn't mind making the date field a 'date picker' rather than the standard text area if anyone know how?
Any help would be greatly appreciated! Code below:
// Registers the new post type and taxonomy
function wpt_bands_posttype() {
register_post_type( 'bands',
array(
'labels' => array(
'name' => __( 'Bands' ),
'singular_name' => __( 'Band' ),
'add_new' => __( 'Add New Band' ),
'add_new_item' => __( 'Add New Band' ),
'edit_item' => __( 'Edit Band' ),
'new_item' => __( 'Add New Band' ),
'view_item' => __( 'View Band' ),
'search_items' => __( 'Search Band' ),
'not_found' => __( 'No bands found' ),
'not_found_in_trash' => __( 'No bands found in trash' )
),
'public' => true,
'supports' => array( 'title', 'editor', 'thumbnail', 'comments' ),
'capability_type' => 'post',
'rewrite' => array("slug" => "bands"), // Permalinks format
'menu_position' => 5,
'menu_icon' => plugins_url( 'icon.png', __FILE__ ),
'register_meta_box_cb' => 'add_bands_metaboxes'
)
);
}
add_action( 'init', 'wpt_bands_posttype' );
add_action( 'add_meta_boxes', 'add_bands_metaboxes' );
// Add the Bands Meta Boxes
function add_bands_metaboxes() {
add_meta_box('wpt_bands_details', 'Band Details', 'wpt_bands_details', 'bands', 'normal', 'default');
}
function wpt_bands_details() {
global $post;
// Noncename needed to verify where the data originated
echo '<input type="hidden" name="bandsmeta_noncename" id="bandsmeta_noncename" value="' .
wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
// Get the soundcloud data if its already been entered
$video = get_post_meta($post->ID, '_video', true);
$soundcloud = get_post_meta($post->ID, '_soundcloud', true);
$facebookLink = get_post_meta($post->ID, '_facebookLink', true);
$festivalname = get_post_meta($post->ID, '_festivalname', true);
$festivaldate = get_post_meta($post->ID, '_festivaldate', true);
// Echo out the field
echo '<p><span style="font-weight: bold; font-size: 18px; color: #F00">Video URL:</span></p>';
echo '<textarea id="texty" name="_video" class="widefat">' . esc_html( $video ) . '</textarea>';
echo '<p><span style="font-weight: bold; font-size: 18px; color: #F00">SoundCloud Playlist URL</span></p>';
echo '<textarea id="texty" name="_soundcloud" class="widefat">' . esc_html( $soundcloud ) . '</textarea>';
echo '<p style="font-weight: bold; font-size: 18px; color: #F00">Facebook Link</p>';
echo '<input type="url" name="_facebookLink" value="' . $facebookLink . '" class="widefat" />';
echo '<p style="font-weight: bold; font-size: 18px; color: #F00">Festival Appearances</p>';
echo '
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><p>Festival Name </p><br/><input type="text" name="_whichfestone" value="' . $festivalname . '" class="widefat" /></td>
<td><p>Festival Date </p><br/><input type="text" name="_whichfesttwo" value="' . $festivaldate . '" class="widefat" /></td>
</tr>
</table>
';
}
// Save the Metabox Data
function wpt_save_bands_meta($post_id, $post) {
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !wp_verify_nonce( $_POST['bandsmeta_noncename'], plugin_basename(__FILE__) )) {
return $post->ID;
}
// Is the user allowed to edit the post or page?
if ( !current_user_can( 'edit_post', $post->ID ))
return $post->ID;
// OK, we're authenticated: we need to find and save the data
// We'll put it into an array to make it easier to loop though.
$bands_meta['_details'] = $_POST['_details'];
$bands_meta['_video'] = $_POST['_video'];
$bands_meta['_soundcloud'] = $_POST['_soundcloud'];
$bands_meta['_festivalname'] = $_POST['_festivalname'];
$bands_meta['_festivaldate'] = $_POST['_festivaldate'];
// Add values of $events_meta as custom fields
foreach ($bands_meta as $key => $value) { // Cycle through the $events_meta array!
if( $post->post_type == 'revision' ) return; // Don't store custom data twice
$value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
update_post_meta($post->ID, $key, $value);
} else { // If the custom field doesn't have a value
add_post_meta($post->ID, $key, $value);
}
if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
}
}
add_action('save_post', 'wpt_save_bands_meta', 1, 2); // save the custom fields
// My template force code
function include_template_function( $template_path ) {
if ( get_post_type() == 'bands' ) {
if ( is_single() ) {
// checks if the file exists in the theme first,
// otherwise serve the file from the plugin
if ( $theme_file = locate_template( array ( 'plugin-page.php' ) ) ) {
$template_path = $theme_file;
} else {
$template_path = plugin_dir_path( __FILE__ ) . '/plugin-page.php';
}
}
}
return $template_path;
}
// My template force call
add_filter( 'template_include', 'include_template_function', 1 );
?>