Skip to content

Instantly share code, notes, and snippets.

@ryelle
Created November 27, 2012 19:48
Show Gist options
  • Save ryelle/4156557 to your computer and use it in GitHub Desktop.
Save ryelle/4156557 to your computer and use it in GitHub Desktop.
Press "Published Date" Metabox
<?php
/**
* Add Press Custom Post Type Metaboxes
*/
add_action( 'add_meta_boxes', 'add_press_metaboxes' );
// Add the Press Meta Boxes
function add_press_metaboxes() {
add_meta_box('press_publish_date', 'Published Date', 'press_publish_date', 'press', 'normal', 'high');
}
// The Publish Date Metabox
function press_publish_date() {
global $post;
// Noncename needed to verify where the data originated
wp_nonce_field( 'save', 'press-meta-nonce' );
// Enqueue Datepicker + jQuery UI CSS
wp_enqueue_script( 'jquery-ui-datepicker' );
wp_enqueue_style( 'jquery-ui-style', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/themes/smoothness/jquery-ui.css', true);
?>
<script>
jQuery( document ).ready( function( $ ) {
$("#archive-date").datepicker({
dateFormat: "MM dd, yy",
defaultDate: '<?php echo $archive_date; ?>'
});
} );
</script>
<table class="form-table">
<tr>
<td>
<div>
<input type="text" name="archive-date" id="archive-date" value="<?php echo $archive_date; ?>" />
</div>
</td>
</tr>
</table>
<?php
}
// Save the Metabox Data
function wpt_save_press_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 ( ! isset( $_POST['press-meta-nonce'] ) || ! wp_verify_nonce( $_POST['press-meta-nonce'], 'save' ) )
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.
$press_meta['_location'] = $_POST['_location'];
// Add values of $press_meta as custom fields
foreach ($press_meta as $key => $value) { // Cycle through the $press_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_press_meta', 1, 2); // save the custom fields
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment