Events Meta Box For Posts

Add a start and end time meta field for posts

Just paste this entire snipit into your functions file and you should be good to go

function added_events_options_box() {
 
    $screens = array( 'post' );                              // this is the post type that the box will appear on (post,page,custom post type) separated by commas
 
    foreach ( $screens as $screen ) {  // 1122AM
 
      add_meta_box(
          'events_options_box',                     // the id of the div that controls the meta box. Case you want to style it
          __( 'Event Settings', 'Event Settings' ),          // little title that appears in the meta box in the editor
          'added_events_options_content',                    // this is the function that prints the HTML out
          $screen,                                           // this is the post type that the box will appear on (post,page,custom post type)
          'side',                                            // puts the box under the editor // sometimes I use "side" for side boxes
          'high'                                             // places it first under the editor
      );
 
   } // 1122AM
}
add_action( 'add_meta_boxes', 'added_events_options_box' );



////////////////////////////////////////////////////////////////////////



function added_events_options_save( $post_id ) { // 847AM
 
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) // this stops auto save from updating my fields automatically
    return;
 
    
    

    $post_page_options    = get_post_meta( $post_id, 'post_page_options', true );
    
    $event_settings['event_start_month']  = $_POST['event_start_month']; 
    $event_settings['event_start_day']    = $_POST['event_start_day']; 
    $event_settings['event_start_year']   = $_POST['event_start_year'];
    $event_settings['event_start_hour']   = $_POST['event_start_hour'];
    $string_start_date = $event_settings['event_start_day']."-".$event_settings['event_start_month']."-".$event_settings['event_start_year'];
    $event_settings['event_start_time']   = strtotime($string_start_date);
    
    $event_settings['event_end_month']    = $_POST['event_end_month']; 
    $event_settings['event_end_day']      = $_POST['event_end_day']; 
    $event_settings['event_end_year']     = $_POST['event_end_year'];
    $event_settings['event_end_hour']     = $_POST['event_end_hour'];
    $string_end_date = $event_settings['event_end_day']."-".$event_settings['event_end_month']."-".$event_settings['event_end_year'];
    $event_settings['event_end_time']     = strtotime($string_end_date);

    $event_settings['event_location']     = $_POST['event_location'];
    
    $post_page_options['event_settings']  = $event_settings;
    

    update_post_meta( $post_id, 'post_page_options', $post_page_options );


 
} // 847AM
 
 
add_action( 'save_post', 'added_events_options_save' );



////////////////////////////////////////////////////////////////////////


function added_events_options_content($post) { // 1102AM ?>

<?     $post_id = $post->ID; ?>
<?     $post_page_options    = get_post_meta( $post_id, 'post_page_options', true ); ?>
<?     $event_settings       = $post_page_options['event_settings']; ?>

<label>Event Start</label><br />
<select name="event_start_month" id="event_start_month">
	<option value="<? echo $event_settings['event_start_month']; ?>"><? echo $event_settings['event_start_month']; ?></option>
	<option value="January">January</option>
	<option value="Febuary">Febuary</option>
	<option value="March">March</option>
	<option value="April">April</option>
	<option value="May">May</option>
	<option value="June">June</option>
	<option value="July">July</option>
	<option value="August">August</option>
	<option value="September">September</option>
	<option value="October">October</option>
	<option value="November">November</option>
	<option value="December">December</option>
</select>

<select name="event_start_day" id="event_start_day">
	<option value="<? echo $event_settings['event_start_day']; ?>"><? echo $event_settings['event_start_day']; ?>  </option>
	<option value="1">1</option>
	<option value="2">2</option>
	<option value="3">3</option>
	<option value="4">4</option>
	<option value="5">5</option>
	<option value="6">6</option>
	<option value="7">7</option>
	<option value="8">8</option>
	<option value="9">9</option>
	<option value="10">10</option>
	<option value="11">11</option>
	<option value="12">12</option>
	<option value="13">13</option>
	<option value="14">14</option>
	<option value="15">15</option>
	<option value="16">16</option>
	<option value="17">17</option>
	<option value="18">18</option>
	<option value="19">19</option>
	<option value="20">20</option>
	<option value="21">21</option>
	<option value="22">22</option>
	<option value="23">23</option>
	<option value="24">24</option>
	<option value="25">25</option>
	<option value="26">26</option>
	<option value="27">27</option>
	<option value="28">28</option>
	<option value="29">29</option>
	<option value="30">30</option>
	<option value="31">31</option>
</select>

<select name="event_start_year" id="event_start_year">
	<option value="<? echo $event_settings['event_start_year']; ?>"><? echo $event_settings['event_start_year']; ?></option>
	<option value="2014">2014</option>
	<option value="2015">2015</option>
	<option value="2016">2016</option>
	<option value="2017">2017</option>
	<option value="2018">2018</option>
	<option value="2019">2019</option>
	<option value="2020">2020</option>
</select>


<br />
<label>Start Time</label><br />
<input type="text" name="event_start_hour" id="event_start_hour" placeholder="12:30 PM" value="<? echo $event_settings['event_start_hour']; ?>" />







<!-------------------------------->





<br />
<label>Event End</label><br />
<select name="event_end_month" id="event_end_month">
	<option value="<? echo $event_settings['event_end_month']; ?>"><? echo $event_settings['event_end_month']; ?></option>
	<option value="January">January</option>
	<option value="Febuary">Febuary</option>
	<option value="March">March</option>
	<option value="April">April</option>
	<option value="May">May</option>
	<option value="June">June</option>
	<option value="July">July</option>
	<option value="August">August</option>
	<option value="September">September</option>
	<option value="October">October</option>
	<option value="November">November</option>
	<option value="December">December</option>
</select>

<select name="event_end_day" id="event_end_day">
	<option value="<? echo $event_settings['event_end_day']; ?>"><? echo $event_settings['event_end_day']; ?></option>
	<option value="1">1</option>
	<option value="2">2</option>
	<option value="3">3</option>
	<option value="4">4</option>
	<option value="5">5</option>
	<option value="6">6</option>
	<option value="7">7</option>
	<option value="8">8</option>
	<option value="9">9</option>
	<option value="10">10</option>
	<option value="11">11</option>
	<option value="12">12</option>
	<option value="13">13</option>
	<option value="14">14</option>
	<option value="15">15</option>
	<option value="16">16</option>
	<option value="17">17</option>
	<option value="18">18</option>
	<option value="19">19</option>
	<option value="20">20</option>
	<option value="21">21</option>
	<option value="22">22</option>
	<option value="23">23</option>
	<option value="24">24</option>
	<option value="25">25</option>
	<option value="26">26</option>
	<option value="27">27</option>
	<option value="28">28</option>
	<option value="29">29</option>
	<option value="30">30</option>
	<option value="31">31</option>
</select>

<select name="event_end_year" id="event_end_year">
	<option value="<? echo $event_settings['event_end_year']; ?>"><? echo $event_settings['event_end_year']; ?></option>
	<option value="2014">2014</option>
	<option value="2015">2015</option>
	<option value="2016">2016</option>
	<option value="2017">2017</option>
	<option value="2018">2018</option>
	<option value="2019">2019</option>
	<option value="2020">2020</option>
</select>

<br />
<label>End Time</label><br />
<input type="text" name="event_end_hour" id="event_end_hour" placeholder="3:00 PM" value="<? echo $event_settings['event_end_hour']; ?>" />

<br />
<label>Location</label><br />
<input type="text" name="event_location" id="event_location" placeholder="West Port Plaza" value="<? echo $event_settings['event_location']; ?>" />


<? } // 1102AM

Now spit it out!!! spit it out!!! spit it out!!!

<?    function string_limit_words($string, $word_limit) { //607AM
      $words = explode(' ', $string, ($word_limit + 1));
      if(count($words) > $word_limit)
      array_pop($words);
      return implode(' ', $words);
    } //607AM


    $call_args = array(
        'category_name'    => 'event',
        'order'            => 'DESC'
    );
 
    $custom_query = new WP_Query( $call_args );
    $count = 0;     
 
    if ( $custom_query->have_posts() ) { while ( $custom_query->have_posts() ) { $custom_query->the_post(); // 8:22AM
    
        $this_id = $post->ID;
        
     
        $post_page_options          = get_post_meta( $this_id, 'post_page_options', true );
        
        $this['title']              = get_the_title();
        $this['content']            = get_the_content(); 
        $this_post_excerpt          = get_the_excerpt();
        $this['excerpt']            = string_limit_words($this_post_excerpt,15);
        $this['excerpt']           .= ' <span class="tail">more ></span>'; 
        $this['link']               = get_permalink($this_id);
        $this['featured']           = wp_get_attachment_image_src( get_post_thumbnail_id($this_id), 'full');
        $this['post_page_options']  = $post_page_options;
        
        $all_events[$this_id] = $this;
        
        ////////////////////////////////////////////////////
        //////////////// HANDLES ORDER /////////////////////
        
        $event_settings  = $post_page_options['event_settings'];
        
        $event_start = $event_settings['event_start_time'];
        
        $event_order[$this_id] = $event_start;
        
 
    } }  // 8:22AM
    
    
    asort($event_order);
    
    if (!empty($event_order)) { foreach ($event_order as $key => $event) { //8:11AM
    
    $count++;
    
    $this_event              =  $all_events[$key];
    
    $this_event_options      =  $this_event['post_page_options'];
    
    $featured_image          =  $this['featured'];
    
    $this_post_page_options  =  $this_event['post_page_options'];
    
    $this_event_settings     =  $this_post_page_options['event_settings']; 
    
    $this_event_start_month  =  $this_event_settings['event_start_month'];
    $this_event_start_day    =  $this_event_settings['event_start_day'];
    $this_event_start_year   =  $this_event_settings['event_start_year'];
    $this_event_start_hour   =  $this_event_settings['event_start_hour'];
    $this_event_start_time   =  $this_event_settings['event_start_time'];
    
    $this_event_end_month    =  $this_event_settings['event_end_month'];
    $this_event_end_day      =  $this_event_settings['event_end_day'];
    $this_event_end_year     =  $this_event_settings['event_end_year'];
    $this_event_end_hour     =  $this_event_settings['event_end_hour'];
    $this_event_end_time     =  $this_event_settings['event_end_time'];
    
    $this_event_location     =  $this_event_settings['event_location'];   
    
    
    $display_event_start_time = date('l, F, d', $this_event_start_time);
    
                  ?>
    
    
           <a href="<? echo $this_event['link']; ?>">
           <div class="single_post_wrap" id="single_post_<? echo $count; ?>">

               <div class="event_date"><? echo $display_event_start_time; ?></div> 
               
              
               <? if (!empty($featured_image[0])) { ?>
               <div class="featured_wrap">
                 <img class="featured_image" id="featured_image_<? echo $count; ?>" src="<? echo $featured_image[0]; ?>" />
               </div><!--featured_wrap-->
               <? } ?>
  
               <h1 class="post_title"><? echo $this_event['title']; ?></h1>
               
               
               <? if (!empty($this_event_location)) { ?><div class="event_location"><? echo $this_event_location; ?></div><? } ?>

               <? if (!empty($this_event_start_hour)) { ?><div class="event_hours"><? echo $this_event_start_hour; ?><? } ?>
               
               <? if (!empty($this_event_end_hour)) {  echo " - $this_event_end_hour";  } ?>
               
               <? if (!empty($this_event_start_hour)) { ?></div><!--event_hours--><? } ?>
               
               
               <div class="post_excerpt"><? echo $this_event['excerpt']; ?></div>
 
           </div><!--single_post_wrap-->
           </a>
    
    
    <? } } //8:11AM ?>

Comments