Skip to content

Instantly share code, notes, and snippets.

@jovanialferez
Created December 20, 2013 10:35
Show Gist options
  • Select an option

  • Save jovanialferez/8053074 to your computer and use it in GitHub Desktop.

Select an option

Save jovanialferez/8053074 to your computer and use it in GitHub Desktop.
add_action('add_meta_boxes', 'gov_meta_box_add');
function gov_meta_box_add()
{
add_meta_box('post-meta-box-id', 'Event Type', 'gov_meta_box_cb', 'post', 'side', 'high');
add_meta_box('page-meta-box-id', 'Event Type', 'gov_meta_box_cb', 'page', 'side', 'high');
}
function gov_meta_box_cb($post)
{
$values = get_post_custom($post->ID); // error_log(json_encode($values));
$selected = isset($values['gov_event_type']) ? esc_attr($values['gov_event_type'][0]) : '';
wp_nonce_field('gov_meta_box_nonce', 'gov_meta_box_nonce');
?>
<p>
<label for="my_meta_box_select">Please select type:</label>
<select name="gov_event_type" id="sel-gov_event_type">
<option value="">--</option>
<option value="month" <?php selected($selected, 'month'); ?>>Month</option>
<option value="week" <?php selected($selected, 'week'); ?>>Week</option>
<option value="day" <?php selected($selected, 'day'); ?>>Day</option>
</select>
</p>
<?php
}
add_action('save_post', 'gov_meta_box_save');
function gov_meta_box_save($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['gov_meta_box_nonce']) || !wp_verify_nonce($_POST['gov_meta_box_nonce'], 'gov_meta_box_nonce')) return;
// if our current user can't edit this post, bail
if (!current_user_can('edit_post')) return;
if (isset($_POST['gov_event_type']) && $_POST['gov_event_type']) {
update_post_meta($post_id, 'gov_event_type', esc_attr($_POST['gov_event_type']));
// update_post_meta( $post_id, 'gove_event_month', )
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment