Skip to content

Instantly share code, notes, and snippets.

@epatr
Created July 27, 2017 15:03
Show Gist options
  • Save epatr/992b9a4cfeb10f7f0050359074102e1c to your computer and use it in GitHub Desktop.
Save epatr/992b9a4cfeb10f7f0050359074102e1c to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: Custom Post Type - Events
Version: 0.0.1
Plugin URI: http://ukyrgf.com
Description: Defines a custom post type for an event calendar
Author URI: https://epatr.com
Author: Eric Patrick
*/
function register_custom_event_type() {
$labels = array(
'name' => _x('Events', 'event'),
'singular_name' => _x('Event', 'event'),
'add_new' => _x('Add New', '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' => false,
'supports' => array('title', 'editor'),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'rewrite' => true,
'capability_type' => 'post'
);
register_post_type('event', $args);
}
add_action('init', 'register_custom_event_type');
/**
* Since we removed everything in the 'supports' area above, we will
* add it back in using meta types
*/
add_action('add_meta_boxes', 'add_events_fields_box');
function add_events_fields_box() {
add_meta_box('events_fields_box_id', 'Event Info', 'display_event_info_box', 'event');
}
function display_event_info_box() {
global $post;
$values = get_post_custom($post->ID);
$eve_start_date = isset($values['_eve_sta_date']) ? esc_attr($values['_eve_sta_date'][0]) : '';
$eve_end_date = isset($values['_eve_end_date']) ? esc_attr($values['_eve_end_date'][0]) : '';
wp_nonce_field('event_frm_nonce', 'event_frm_nonce');
$html = "<label>Event Start Date</label> <input id='datepickerStart' type='text' name='datepickerStart' value='$eve_start_date' /><br>
<label>Event End Date</label> <input id='datepickerEnd' type='text' name='datepickerEnd' value='$eve_end_date' />";
echo $html;
}
add_action('save_post', 'save_event_information');
function save_event_information($post_id) {
// Bail if we're doing an auto save
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return;
// if our nonce isn't there, or we can't verify it, bail
if (!isset($_POST['event_frm_nonce']) || !wp_verify_nonce($_POST['event_frm_nonce'], 'event_frm_nonce'))
return;
// if our current user can't edit this post, bail
if (!current_user_can('edit_post'))
return;
if (isset($_POST['datepickerStart']))
update_post_meta($post_id, '_eve_sta_date', esc_attr($_POST['datepickerStart']));
if (isset($_POST['datepickerEnd']))
update_post_meta($post_id, '_eve_end_date', esc_attr($_POST['datepickerEnd']));
}
/**
* Output the year's posts to JSON
*/
function get_posts_for_year() {
global $post, $wpdb;
$allEvents = array();
$sql = "SELECT $wpdb->posts.guid,$wpdb->posts.post_title,DATE_FORMAT(post_date, '%m-%d-%Y') as post_date FROM $wpdb->posts WHERE Year($wpdb->posts.post_date)='" . $_POST['currentYear'] . "' and post_status='publish' and post_type='post' ";
$allPosts = array();
$yearlyPosts = $wpdb->get_results($sql, ARRAY_A);
foreach ($yearlyPosts as $key => $singlePost) {
$singlePost['type'] = 'post';
array_push($allEvents, $singlePost);
}
$sql = "SELECT $wpdb->posts.ID,$wpdb->posts.guid,$wpdb->posts.post_title,DATE_FORMAT(post_date, '%m-%d-%Y') as post_date FROM $wpdb->posts inner join $wpdb->postmeta on $wpdb->posts.ID=$wpdb->postmeta.post_id WHERE $wpdb->postmeta.meta_key='_eve_sta_date' and Year(STR_TO_DATE($wpdb->postmeta.meta_value, '%m/%d/%Y'))='" . $_POST['currentYear'] . "' and post_status='publish' and post_type='event'";
$yearlyEvents = $wpdb->get_results($sql, ARRAY_A);
foreach ($yearlyEvents as $key => $singleEvent) {
$startDate = str_replace("/", "-", get_post_meta($singleEvent['ID'], '_eve_sta_date'));
$endDate = str_replace("/", "-", get_post_meta($singleEvent['ID'], '_eve_end_date'));
$singleEvent['startDate'] = $startDate[0];
$singleEvent['endDate'] = $endDate[0];
$singleEvent['type'] = 'event';
array_push($allEvents, $singleEvent);
}
echo json_encode($allEvents);
exit;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment