Advertisement
Guest User

Untitled

a guest
Dec 13th, 2012
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.04 KB | None | 0 0
  1. function wpt_events_date() {
  2.     global $post;
  3.     // Noncename needed to verify where the data originated
  4.     echo '<input type="hidden" name="eventmeta_noncename" id="eventmeta_noncename" value="' .
  5.     wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
  6.     // Get the location data if its already been entered
  7.     $month = get_post_meta($post->ID, '_month', true);
  8.     $date = get_post_meta( $post->ID, '_date', true );
  9.     // Echo out the field
  10.     echo '<p>Enter the month</p>';
  11.     echo '<input type="text" name="_month" value="' . $month  . '" class="widefat" />';
  12.     echo '<p>Enter the date</p>';
  13.     echo '<input type="text" name="_date" value="' . $date  . '" class="widefat" />';
  14. }
  15.  
  16. // Save the Metabox Data
  17. function wpt_save_events_meta($post_id, $post) {
  18.     // verify this came from the our screen and with proper authorization,
  19.     // because save_post can be triggered at other times
  20.     if ( !wp_verify_nonce( $_POST['eventmeta_noncename'], plugin_basename(__FILE__) )) {
  21.     return $post->ID;
  22.     }
  23.     // Is the user allowed to edit the post or page?
  24.     if ( !current_user_can( 'edit_post', $post->ID ))
  25.         return $post->ID;
  26.     // OK, we're authenticated: we need to find and save the data
  27.     // We'll put it into an array to make it easier to loop though.
  28.     $events_meta['_month'] = $_POST['_month'];
  29.     $events_meta['_date'] = $_POST['_date'];
  30.     // Add values of $events_meta as custom fields
  31.     foreach ($events_meta as $key => $value) { // Cycle through the $events_meta array!
  32.         if( $post->post_type == 'revision' ) return; // Don't store custom data twice
  33.         $value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
  34.         if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
  35.             update_post_meta($post->ID, $key, $value);
  36.         } else { // If the custom field doesn't have a value
  37.             add_post_meta($post->ID, $key, $value);
  38.         }
  39.         if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement