Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Custom Meta Box - Drop Down WordPress

  • SOLVED

I am after some help with a drop down in a custom metabox.
This is for months and I want the text values, not numerical.

I also need the snippet to echo the values
<?php
$custom = get_post_custom($post->ID);
global $post;
$post_id = $post->ID;
$custom = get_post_custom($post->ID);
$start_month = get_post_meta($post_id, 'start_month', true);

?>

<?PHP
/* Use the admin_menu action to define the custom boxes */
add_action('admin_menu', 'departuresplugin_add_custom_box');

/* Use the save_post action to do something with the data entered */
add_action('save_post', 'departuresplugin_save_postdata');

/* Adds a custom section to the "normal" Post and Page edit screens */
function departuresplugin_add_custom_box() {
if( function_exists( 'add_meta_box' )) {
add_meta_box( 'departuresplugin_sectionid', __( 'Depature Details', 'departuresplugin_textdomain' ),
'departuresplugin_inner_custom_box', 'departures', 'normal' ,'high');
}
}

function departuresplugin_inner_custom_box() {

// Use nonce for verification
echo '<input type="hidden" name="departuresplugin_noncename" id="departuresplugin_noncename" value="' .
wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
global $post;
if($post->post_type == 'departures'){
$post_id = $post->ID;
}

$start_month = get_post_meta($post_id, 'start_month', true);

echo '<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="10%"><label for="date">' . __("Date", 'departuresplugin_textdomain' ) . '</label></td>';
echo "<td>

<label for="start_month">Month</label>
<select name="start_month" value="start_month">
<option value="January">January</option>
<option value="February">February</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>

<input type=\"text\" name=\"year\" value=\"$year\" style=\"width:70%\" />

</td></tr>
</table>
";


/* When the post is saved, saves our custom data */
function departuresplugin_save_postdata( $post_id ) {

// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !wp_verify_nonce( $_POST['departuresplugin_noncename'], plugin_basename(__FILE__) )) {
return $post_id;
}

if ( 'departures' == $_POST['post_type'] ) {
if ( !current_user_can( 'edit_page', $post_id ))
return $post_id;
}

// OK, we're authenticated: we need to find and save the data
$start_month = $_POST['start_month'];

//update post meta into database
update_post_meta($post_id, 'start_month', $start_month);
}

?>

Answers (1)

2011-08-16

Peter Michael answers:

Use this function instead of day_option_list() in the code I sent.

function day_option_list_names( $selected = NULL ) {
$months = array('January','February','March','April','May','June','July','August','September','October','November','December')
foreach($months as $month) {
$is_selected = ($month == $selected) ? 'selected="selected"' : '';
echo '<option value="'.$month.'" '.$is_selected.'>'.$month.'</option>';
}
}


HTH


Peter Michael comments:

Oops, a little typo here, that is of course 'month_option_list()' to replace.

function month_option_list_names( $selected = NULL ) {
$months = array('January','February','March','April','May','June','July','August','September','October','November','December')
foreach($months as $month) {
$is_selected = ($month == $selected) ? 'selected="selected"' : '';
echo '<option value="'.$month.'" '.$is_selected.'>'.$month.'</option>';
}
}


HTH