I have a custom meta box. In it there are input fields, as well as a select box. What I want, is to have the get_tags array list to be outputted as the values in the select box.
I currently have the select box option values hard coded. I want this to be dynamic depending on the tags on the website.
Here is the code;
// Add the Meta Box
function spk_project_info_meta_box() {
add_meta_box(
'spk_project_info_meta_box', // $id
'Project Information', // $title
'show_spk_project_info_meta_box', // $callback
'post', // $page
'normal', // $context
'default'); // $priority
}
add_action('add_meta_boxes', 'spk_project_info_meta_box');
// Field Array
$prefix = 'spk_project_info_';
$tags_array = get_tags();
$custom_meta_fields = array(
array(
'label'=> 'Client',
'desc' => 'A description for the field.',
'id' => $prefix.'client',
'type' => 'text'
),
array(
'label'=> 'Our Role',
'desc' => 'A description for the field.',
'id' => $prefix.'role',
'type' => 'text'
),
array(
'label'=> 'Year',
'desc' => 'A description for the field.',
'id' => $prefix.'year',
'type' => 'text'
),
array(
'label'=> 'Select Box',
'desc' => 'A description for the field.',
'id' => $prefix.'select',
'type' => 'select',
'options' => array (
'one' => array (
'label' => 'Option One',
'value' => 'one'
),
'two' => array (
'label' => 'Option Two',
'value' => 'two'
),
'three' => array (
'label' => 'Option Three',
'value' => 'three'
)
)
)
);
// The Callback
function show_spk_project_info_meta_box() {
global $custom_meta_fields, $post;
echo '<input type="hidden" name="spk_project_info_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';
echo '<table class="form-table">';
foreach ($custom_meta_fields as $field) {
$meta = get_post_meta($post->ID, $field['id'], true);
echo '<tr>
<th><label for="'.$field['id'].'">'.$field['label'].'</label></th>
<td>';
switch($field['type']) {
case 'text':
echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" />
<br /><span class="description">'.$field['desc'].'</span>';
break;
case 'select':
echo '<select name="'.$field['id'].'" id="'.$field['id'].'">';
foreach ($field['options'] as $option) {
echo '<option', $meta == $option['value'] ? ' selected="selected"' : '', ' value="'.$option['value'].'">'.$option['label'].'</option>';
}
echo '</select><br /><span class="description">'.$field['desc'].'</span>';
break;
}
echo '</td></tr>';
} // end foreach
echo '</table>'; // end table
}
function save_spk_project_info_meta_data($post_id) {
global $custom_meta_fields;
if ( !wp_verify_nonce($_POST['spk_project_info_meta_box_nonce'], basename(__FILE__) ) )
return $post_id;
if ( defined('DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
if ('page' == $_POST['post_type']) {
if ( !current_user_can( 'edit_page', $post_id ) )
return $post_id;
} elseif ( !current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
foreach ( $custom_meta_fields as $field ) {
$old = get_post_meta( $post_id, $field['id'], true );
$new = $_POST[$field['id']];
if ($new && $new != $old) {
update_post_meta( $post_id, $field['id'], $new );
} elseif ('' == $new && $old) {
delete_post_meta( $post_id, $field['id'], $old );
}
} // end foreach
}
add_action('save_post', 'save_spk_project_info_meta_data');