Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- ////////////////////////////// Begin 'event' Custom Post Type ///////////////////////////////////
- function benchmark() {
- static $start = NULL;
- if( is_null($start) ) {
- $start = get_microtime();
- } else {
- $benchmark = get_microtime() - $start;
- $start = get_microtime();
- return $benchmark;
- }
- }
- function get_microtime() {
- list($usec, $sec) = explode(" ", microtime());
- return ((float)$usec + (float)$sec);
- }
- ///////////////// end Bench Marks
- add_action( 'init', 'register_cpt_event' );
- function register_cpt_event() {
- $labels = array(
- 'name' => _x( 'Events', 'event' ),
- 'singular_name' => _x( 'Event', 'event' ),
- 'add_new' => _x( 'Add New Event', 'event' ),
- 'add_new_item' => _x( 'Add New Event', 'event' ),
- 'edit_item' => _x( 'Edit Event', 'event' ),
- 'new_item' => _x( 'New Event', 'event' ),
- 'view_item' => _x( 'View Event', 'event' ),
- 'search_items' => _x( 'Search Events', 'event' ),
- 'not_found' => _x( 'No events found', 'event' ),
- 'not_found_in_trash' => _x( 'No events found in Trash', 'event' ),
- 'parent_item_colon' => _x( 'Parent Event:', 'event' ),
- 'menu_name' => _x( 'Events', 'event' ),
- );
- $args = array(
- 'labels' => $labels,
- 'hierarchical' => true,
- 'description' => 'Events at regional arts venues in the Ohio area.',
- 'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'trackbacks', 'custom-fields' ),
- 'taxonomies' => array( 'locations' ),
- 'public' => true,
- 'show_ui' => true,
- 'show_in_menu' => true,
- 'show_in_nav_menus' => true,
- 'publicly_queryable' => true,
- 'exclude_from_search' => false,
- 'query_var' => true,
- 'can_export' => true,
- 'rewrite' => array('slug'=>'','with_front'=>false),
- 'has_archive' => 'events',
- 'capability_type' => 'post'
- );
- register_post_type( 'event', $args );
- }
- add_action( 'init', 'create_locations' );
- function create_locations() {
- $labels = array(
- 'name' => _x( 'Locations', 'taxonomy general name' ),
- 'singular_name' => _x( 'Location', 'taxonomy singular name' ),
- 'search_items' => __( 'Search Locations' ),
- 'all_items' => __( 'All Locations' ),
- 'parent_item' => __( 'Parent Location' ),
- 'parent_item_colon' => __( 'Parent Location:' ),
- 'edit_item' => __( 'Edit Location' ),
- 'update_item' => __( 'Update Location' ),
- 'add_new_item' => __( 'Add New Location' ),
- 'new_item_name' => __( 'New Location Name' ),
- );
- register_taxonomy('location','event',array(
- 'hierarchical' => true,
- 'labels' => $labels
- ));
- }
- // Custom Meta Data for Events
- $meta_box['event'] = array(
- 'id' => 'event-meta-details',
- 'title' => 'Event Information',
- 'context' => 'normal',
- 'priority' => 'high',
- 'fields' => array(
- array(
- 'name' => 'Start Date',
- 'desc' => '(Enter yyyy/mm/dd)',
- 'id' => 'start_date',
- 'type' => 'text',
- 'default' => ''
- ),
- array(
- 'name' => 'End Date',
- 'desc' => '(Enter yyyy/mm/dd)',
- 'id' => 'end_date',
- 'type' => 'text',
- 'default' => ''
- ),
- array(
- 'name' => 'Opening',
- 'desc' => 'YYYY-MM-DD 00:00 24 hour clock',
- 'id' => 'opening_time',
- 'type' => 'text',
- 'default' => ''
- ),
- array(
- 'name' => 'Closing',
- 'desc' => 'YYYY-MM-DD 00:00 24 hour clock',
- 'id' => 'closing_time',
- 'type' => 'text',
- 'default' => ''
- ),
- array(
- 'name' => 'Artist Talk',
- 'desc' => 'YYYY-MM-DD 00:00 24 hour clock',
- 'id' => 'artist_talk_time',
- 'type' => 'text',
- 'default' => ''
- ),
- array(
- 'name' => 'Lecture',
- 'desc' => 'YYYY-MM-DD 00:00 24 hour clock',
- 'id' => 'lecture_time',
- 'type' => 'text',
- 'default' => ''
- ),
- array(
- 'name' => 'Panel',
- 'desc' => 'YYYY-MM-DD 00:00 24 hour clock',
- 'id' => 'panel_time',
- 'type' => 'text',
- 'default' => ''
- ),
- array(
- 'name' => 'Special Event',
- 'desc' => 'YYYY-MM-DD 00:00 24 hour clock',
- 'id' => 'special_event_time',
- 'type' => 'text',
- 'default' => ''
- ),
- array(
- 'name' => 'Workshop',
- 'desc' => 'YYYY-MM-DD 00:00 24 hour clock',
- 'id' => 'workshop_time',
- 'type' => 'text',
- 'default' => ''
- ),
- array(
- 'name' => 'Venue',
- 'desc' => 'Venue of Event',
- 'id' => $prefix . 'event_venue',
- 'type' => 'radio',
- 'options' => array(
- array('name' => 'Pine Gallery', 'value' => 'Pine Gallery'),
- array('name' => 'Spruce Gallery', 'value' => 'Spruce Gallery'),
- array('name' => 'Oak Gallery', 'value' => 'Oak Gallery')
- )
- ),
- array(
- 'name' => 'Custom Venue',
- 'desc' => 'Enter Venue Name if Enter Text Below is Checked',
- 'id' => 'custom_event_venue',
- 'type' => 'text',
- 'default' => ''
- ),
- array(
- 'name' => 'Featured / Reccomended',
- 'desc' => 'Recommended Event',
- 'id' => 'featured_event',
- 'type' => 'checkbox',
- 'default' => ''
- )
- )
- );
- ///////////////
- function get_related_event_data( $venue, $field = 'all' ) {
- $event_data = array(
- 'Pine Gallery' => array(
- 'address' => 'Test Address 2',
- 'phone_no' => '444-333-3333',
- 'url' => 'http://www.url2.com'
- ),
- 'Spruce Gallery' => array(
- 'address' => 'Test Address 2',
- 'phone_no' => '444-333-3333',
- 'url' => 'http://www.url2.com'
- ),
- 'Oak Gallery' => array(
- 'address' => 'Test Address 3',
- 'phone_no' => '555-666-6666',
- 'url' => 'http://www.url3.com'
- ),
- );
- if( !isset( $event_data[$venue] ) )
- return;
- // Optionally return all fields(note: you can't echo arrays, so be sure to loop over the data if pulling all fields
- if( 'all' == $field )
- return $event_data[$venue];
- if( !isset( $event_data[$venue][$field] ) )
- return;
- return $event_data[$venue][$field];
- }
- ///////////////
- // Add meta boxes to admin panel only needs to be added once
- add_action('admin_menu', 'plib_add_box');
- //Add meta boxes to post types
- function plib_add_box() {
- global $meta_box;
- foreach($meta_box as $post_type => $value) {
- add_meta_box($value['id'], $value['title'], 'plib_format_box', $post_type, $value['context'], $value['priority']);
- }
- }
- //Format meta boxes
- function plib_format_box() {
- global $meta_box, $post;
- // Use once for verification
- echo '<input type="hidden" name="plib_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
- echo '<table class="form-table">';
- foreach ($meta_box[$post->post_type]['fields'] as $field) {
- // get current post meta data
- $meta = get_post_meta($post->ID, $field['id'], true);
- echo '<tr>'.
- '<th style="width:20%"><label for="'. $field['id'] .'">'. $field['name']. '</label></th>'.
- '<td>';
- switch ($field['type']) {
- case 'text':
- echo '<input type="text" name="'. $field['id']. '" id="'. $field['id'] .'" value="'. ($meta ? $meta : $field['default']) . '" size="30" style="width:30%" />'. ' - '. $field['desc'];
- break;
- case 'textarea':
- echo '<textarea name="'. $field['id']. '" id="'. $field['id']. '" cols="60" rows="4" style="width:97%">'. ($meta ? $meta : $field['default']) . '</textarea>'. '<br />'. $field['desc'];
- break;
- case 'select':
- echo '<select name="'. $field['id'] . '" id="'. $field['id'] . '">';
- foreach ($field['options'] as $option) {
- echo '<option '. ( $meta == $option ? ' selected="selected"' : '' ) . '>'. $option . '</option>';
- }
- echo '</select>';
- break;
- case 'radio':
- foreach ($field['options'] as $option) {
- echo '<input type="radio" name="' . $field['id'] . '" value="' . $option['value'] . '"' . ( $meta == $option['value'] ? ' checked="checked"' : '' ) . ' />' . $option['name'];
- }
- break;
- case 'checkbox':
- echo '<input type="checkbox" name="' . $field['id'] . '" id="' . $field['id'] . '"' . ( $meta ? ' checked="checked"' : '' ) . ' />';
- break;
- }
- echo '<td>'.'</tr>';
- }
- echo '</table>';
- }
- // Save data from meta box
- function plib_save_data($post_id) {
- global $meta_box, $post;
- //Verify nonce
- if (!wp_verify_nonce($_POST['plib_meta_box_nonce'], basename(__FILE__))) {
- return $post_id;
- }
- //Check autosave
- if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
- return $post_id;
- }
- //Check permissions
- if ('page' == $_POST['post_type']) {
- if (!current_user_can('edit_page', $post_id)) {
- return $post_id;
- }
- } elseif (!current_user_can('edit_post', $post_id)) {
- return $post_id;
- }
- foreach ($meta_box[$post->post_type]['fields'] as $field) {
- $old = get_post_meta($post_id, $field['id'], true);
- $new = $_POST[$field['id']];
- if ($new && $new != $old) {
- update_post_meta($post_id, $field['id'], $new);
- } elseif ('' == $new && $old) {
- delete_post_meta($post_id, $field['id'], $old);
- }
- }
- }
- add_action('save_post', 'plib_save_data');
- // Remove Meta entry Fields from Event Custom Posts
- add_action('admin_menu', 'dbt_remove_boxes');
- function dbt_remove_boxes(){
- remove_meta_box('postexcerpt', 'event', 'normal');
- // remove_meta_box('postcustom', 'post', 'normal');
- remove_meta_box('trackbacksdiv', 'event', 'normal');
- // remove_meta_box('slugdiv', 'post', 'normal');
- remove_meta_box('authordiv', 'event', 'normal');
- // remove_meta_box('commentstatusdiv', 'post', 'normal');
- }
- /**
- * Adds event post metaboxes for start time and end time
- * http://codex.wordpress.org/Function_Reference/add_meta_box
- *
- * We want two time event metaboxes, one for the start time and one for the end time.
- * Two avoid repeating code, we'll just pass the $identifier in a callback.
- * If you wanted to add this to regular posts instead, just swap 'event' for 'post' in add_meta_box.
- */
- function ep_eventposts_metaboxes() {
- add_meta_box( 'ept_event_date_start', 'Start Date and Time', 'ept_event_date', 'event', 'side', 'default', array( 'id' => '_start') );
- add_meta_box( 'ept_event_date_end', 'End Date and Time', 'ept_event_date', 'event', 'side', 'default', array('id'=>'_end') );
- add_meta_box( 'ept_event_location', 'Event Location', 'ept_event_location', 'event', 'normal', 'default', array('id'=>'_end') );
- }
- add_action( 'admin_init', 'ep_eventposts_metaboxes' );
- // Metabox HTML
- function ept_event_date($post, $args) {
- $metabox_id = $args['args']['id'];
- global $post, $wp_locale;
- // Use nonce for verification
- wp_nonce_field( plugin_basename( __FILE__ ), 'ep_eventposts_nonce' );
- $time_adj = current_time( 'timestamp' );
- $month = get_post_meta( $post->ID, $metabox_id . '_month', true );
- if ( empty( $month ) ) {
- $month = gmdate( 'm', $time_adj );
- }
- $day = get_post_meta( $post->ID, $metabox_id . '_day', true );
- if ( empty( $day ) ) {
- $day = gmdate( 'd', $time_adj );
- }
- $year = get_post_meta( $post->ID, $metabox_id . '_year', true );
- if ( empty( $year ) ) {
- $year = gmdate( 'Y', $time_adj );
- }
- $hour = get_post_meta($post->ID, $metabox_id . '_hour', true);
- if ( empty($hour) ) {
- $hour = gmdate( 'H', $time_adj );
- }
- $min = get_post_meta($post->ID, $metabox_id . '_minute', true);
- if ( empty($min) ) {
- $min = '00';
- }
- $month_s = '<select name="' . $metabox_id . '_month">';
- for ( $i = 1; $i < 13; $i = $i +1 ) {
- $month_s .= "\t\t\t" . '<option value="' . zeroise( $i, 2 ) . '"';
- if ( $i == $month )
- $month_s .= ' selected="selected"';
- $month_s .= '>' . $wp_locale->get_month_abbrev( $wp_locale->get_month( $i ) ) . "</option>\n";
- }
- $month_s .= '</select>';
- echo $month_s;
- echo '<input type="text" name="' . $metabox_id . '_day" value="' . $day . '" size="2" maxlength="2" />';
- echo '<input type="text" name="' . $metabox_id . '_year" value="' . $year . '" size="4" maxlength="4" /> @ ';
- echo '<input type="text" name="' . $metabox_id . '_hour" value="' . $hour . '" size="2" maxlength="2"/>:';
- echo '<input type="text" name="' . $metabox_id . '_minute" value="' . $min . '" size="2" maxlength="2" />';
- }
- function ept_event_location() {
- global $post;
- // Use nonce for verification
- wp_nonce_field( plugin_basename( __FILE__ ), 'ep_eventposts_nonce' );
- // The metabox HTML
- $event_location = get_post_meta( $post->ID, '_event_location', true );
- echo '<label for="_event_location">Location:</label>';
- echo '<input type="text" name="_event_location" value="' . $event_location . '" />';
- }
- // Save the Metabox Data
- function ep_eventposts_save_meta( $post_id, $post ) {
- if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
- return;
- if ( !isset( $_POST['ep_eventposts_nonce'] ) )
- return;
- if ( !wp_verify_nonce( $_POST['ep_eventposts_nonce'], plugin_basename( __FILE__ ) ) )
- return;
- // Is the user allowed to edit the post or page?
- if ( !current_user_can( 'edit_post', $post->ID ) )
- return;
- // 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
- $metabox_ids = array( '_start', '_end' );
- foreach ($metabox_ids as $key ) {
- $events_meta[$key . '_month'] = $_POST[$key . '_month'];
- $events_meta[$key . '_day'] = $_POST[$key . '_day'];
- if($_POST[$key . '_hour']<10){
- $events_meta[$key . '_hour'] = '0'.$_POST[$key . '_hour'];
- } else {
- $events_meta[$key . '_hour'] = $_POST[$key . '_hour'];
- }
- $events_meta[$key . '_year'] = $_POST[$key . '_year'];
- $events_meta[$key . '_hour'] = $_POST[$key . '_hour'];
- $events_meta[$key . '_minute'] = $_POST[$key . '_minute'];
- $events_meta[$key . '_eventtimestamp'] = $events_meta[$key . '_year'] . $events_meta[$key . '_month'] . $events_meta[$key . '_day'] . $events_meta[$key . '_hour'] . $events_meta[$key . '_minute'];
- }
- // Add values of $events_meta as custom fields
- foreach ( $events_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', 'ep_eventposts_save_meta', 1, 2 );
- /**
- * Helpers to display the date on the front end
- */
- // Get the Month Abbreviation
- function eventposttype_get_the_month_abbr($month) {
- global $wp_locale;
- for ( $i = 1; $i < 13; $i = $i +1 ) {
- if ( $i == $month )
- $monthabbr = $wp_locale->get_month_abbrev( $wp_locale->get_month( $i ) );
- }
- return $monthabbr;
- }
- // Display the date
- function eventposttype_get_the_event_date() {
- global $post;
- $eventdate = '';
- $month = get_post_meta($post->ID, '_month', true);
- $eventdate = eventposttype_get_the_month_abbr($month);
- $eventdate .= ' ' . get_post_meta($post->ID, '_day', true) . ',';
- $eventdate .= ' ' . get_post_meta($post->ID, '_year', true);
- $eventdate .= ' at ' . get_post_meta($post->ID, '_hour', true);
- $eventdate .= ':' . get_post_meta($post->ID, '_minute', true);
- echo $eventdate;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement