title: How to add all categories into an array and extract the categories names and use it with a custom meta box
body: <p>Basically I need to add custom meta boxes to pages and one of this options must be a list of categories from previous posts. I've managed to add the metaboxes and wrapped the categories in a array, however there must be something I missing as on the page, only the first letter of the category name is being displayed. Here's a screenshot of the result:
  <img src="https://i.stack.imgur.com/ow8Rf.jpg" alt="screenshot of the problem"></p>
  
  <p>I can't figure it out what I'm missing. Here's the code for the metabox:</p>
  
  <pre><code>            // Check if is page-homepage.php template
          $post_id = (isset($_GET['post'])) ? $_GET['post'] : ((isset($_POST['post_ID'])) ? $_POST['post_ID'] : false);
  
          if ($post_id) : 
          $post_template = get_post_meta($post_id, '_wp_page_template', true);
          if ($post_template == 'page-homepage.php') {
  
          // Add the Meta Box
          function home_custom_meta_box() {
              add_meta_box(
                  'home_meta_box', // $id
                  'Custom Meta Box', // $title 
                  'home_show_custom_meta_box', // $callback
                  'page', // $page
                  'normal', // $context
                  'high'); // $priority
          }
          add_action('add_meta_boxes', 'home_custom_meta_box');
  
          // Field Array
          $prefix = 'custom_';
          $home_custom_meta_fields = array(
              array(
                  'label' =&gt; 'Title for the middle column',
                  'desc'  =&gt; 'Optional',
                  'id'    =&gt; $prefix.'title1',
                  'type'  =&gt; 'text'
              ),
                  array(
                  'label' =&gt; 'Category',
                  'id'    =&gt; $prefix.'category',
                  'type'  =&gt; 'select',
                  'options' =&gt; $wp_cats
              ),
              array(
                  'label' =&gt; 'Title for the right column',
                  'desc'  =&gt; 'Optional',
                  'id'    =&gt; $prefix.'title2',
                  'type'  =&gt; 'text'
              ),
              array(
                  'label' =&gt; 'Textarea',
                  'desc'  =&gt; 'A description for the field.',
                  'id'    =&gt; $prefix.'textarea',
                  'type'  =&gt; 'textarea'
              ),
              array(
                  'label' =&gt; 'Checkbox Input',
                  'desc'  =&gt; 'A description for the field.',
                  'id'    =&gt; $prefix.'checkbox',
                  'type'  =&gt; 'checkbox'
              ),
              array(
                  'label' =&gt; 'Select Box',
                  'desc'  =&gt; 'A description for the field.',
                  'id'    =&gt; $prefix.'select',
                  'type'  =&gt; 'select',
                  'options' =&gt; array (
                      'one' =&gt; array (
                          'label' =&gt; 'Option One',
                          'value' =&gt; 'one'
                      ),
                      'two' =&gt; array (
                          'label' =&gt; 'Option Two',
                          'value' =&gt; 'two'
                      ),
                      'three' =&gt; array (
                          'label' =&gt; 'Option Three',
                          'value' =&gt; 'three'
                      )
                  )
              ),
              array (
                  'label' =&gt; 'Radio Group',
                  'desc'  =&gt; 'A description for the field.',
                  'id'    =&gt; $prefix.'radio',
                  'type'  =&gt; 'radio',
                  'options' =&gt; array (
                      'one' =&gt; array (
                          'label' =&gt; 'Option One',
                          'value' =&gt; 'one'
                      ),
                      'two' =&gt; array (
                          'label' =&gt; 'Option Two',
                          'value' =&gt; 'two'
                      ),
                      'three' =&gt; array (
                          'label' =&gt; 'Option Three',
                          'value' =&gt; 'three'
                      )
                  )
              ),
              array (
                  'label' =&gt; 'Checkbox Group',
                  'desc'  =&gt; 'A description for the field.',
                  'id'    =&gt; $prefix.'checkbox_group',
                  'type'  =&gt; 'checkbox_group',
                  'options' =&gt; array (
                      'one' =&gt; array (
                          'label' =&gt; 'Option One',
                          'value' =&gt; 'one'
                      ),
                      'two' =&gt; array (
                          'label' =&gt; 'Option Two',
                          'value' =&gt; 'two'
                      ),
                      'three' =&gt; array (
                          'label' =&gt; 'Option Three',
                          'value' =&gt; 'three'
                      )
                  )
              ),
              array(
                  'label' =&gt; 'Post List',
                  'desc'  =&gt; 'A description for the field.',
                  'id'    =&gt;  $prefix.'post_id',
                  'type'  =&gt; 'post_list',
                  'post_type' =&gt; array('post','page')
              ),
          );
  
          // The Callback
          function home_show_custom_meta_box() {
              global $home_custom_meta_fields, $post;
              // Use nonce for verification
              echo '&lt;input type="hidden" name="custom_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" /&gt;';
  
              // Begin the field table and loop
              echo '&lt;table class="form-table"&gt;';
              foreach ($home_custom_meta_fields as $field) {
                  // get value of this field if it exists for this post
                  $meta = get_post_meta($post-&gt;ID, $field['id'], true);
                  // begin a table row with
                  echo '&lt;tr&gt;
                          &lt;th&gt;&lt;label for="'.$field['id'].'"&gt;'.$field['label'].'&lt;/label&gt;&lt;/th&gt;
                          &lt;td&gt;';
                          switch($field['type']) {
                              // text
                              case 'text':
                                  echo '&lt;input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" /&gt;
                                          &lt;br /&gt;&lt;span class="description"&gt;'.$field['desc'].'&lt;/span&gt;';
                              break;
                              // textarea
                              case 'textarea':
                                  echo '&lt;textarea name="'.$field['id'].'" id="'.$field['id'].'" cols="60" rows="4"&gt;'.$meta.'&lt;/textarea&gt;
                                          &lt;br /&gt;&lt;span class="description"&gt;'.$field['desc'].'&lt;/span&gt;';
                              break;
                              // checkbox
                              case 'checkbox':
                                  echo '&lt;input type="checkbox" name="'.$field['id'].'" id="'.$field['id'].'" ',$meta ? ' checked="checked"' : '','/&gt;
                                          &lt;label for="'.$field['id'].'"&gt;'.$field['desc'].'&lt;/label&gt;';
                              break;
                              // select
                              case 'select':
                                  echo '&lt;select name="'.$field['id'].'" id="'.$field['id'].'"&gt;';
                                  foreach ($field['options'] as $option) {
                                      echo '&lt;option', $meta == $option['value'] ? ' selected="selected"' : '', ' value="'.$option['value'].'"&gt;'.$option['label'].'&lt;/option&gt;';
                                  }
                                  echo '&lt;/select&gt;&lt;br /&gt;&lt;span class="description"&gt;'.$field['desc'].'&lt;/span&gt;';
                              break;
                              // radio
                              case 'radio':
                                  foreach ( $field['options'] as $option ) {
                                      echo '&lt;input type="radio" name="'.$field['id'].'" id="'.$option['value'].'" value="'.$option['value'].'" ',$meta == $option['value'] ? ' checked="checked"' : '',' /&gt;
                                              &lt;label for="'.$option['value'].'"&gt;'.$option['label'].'&lt;/label&gt;&lt;br /&gt;';
                                  }
                                  echo '&lt;span class="description"&gt;'.$field['desc'].'&lt;/span&gt;';
                              break;
                              // checkbox_group
                              case 'checkbox_group':
                                  foreach ($field['options'] as $option) {
                                      echo '&lt;input type="checkbox" value="'.$option['value'].'" name="'.$field['id'].'[]" id="'.$option['value'].'"',$meta &amp;&amp; in_array($option['value'], $meta) ? ' checked="checked"' : '',' /&gt; 
                                              &lt;label for="'.$option['value'].'"&gt;'.$option['label'].'&lt;/label&gt;&lt;br /&gt;';
                                  }
                                  echo '&lt;span class="description"&gt;'.$field['desc'].'&lt;/span&gt;';
                              break;
                              // tax_select
                              case 'tax_select':
                                  echo '&lt;select name="'.$field['id'].'" id="'.$field['id'].'"&gt;
                                          &lt;option value=""&gt;Select One&lt;/option&gt;'; // Select One
                                  $terms = get_terms($field['id'], 'get=all');
                                  $selected = wp_get_object_terms($post-&gt;ID, $field['id']);
                                  foreach ($terms as $term) {
                                      if (!empty($selected) &amp;&amp; !strcmp($term-&gt;slug, $selected[0]-&gt;slug)) 
                                          echo '&lt;option value="'.$term-&gt;slug.'" selected="selected"&gt;'.$term-&gt;name.'&lt;/option&gt;'; 
                                      else
                                          echo '&lt;option value="'.$term-&gt;slug.'"&gt;'.$term-&gt;name.'&lt;/option&gt;'; 
                                  }
                                  $taxonomy = get_taxonomy($field['id']);
                                  echo '&lt;/select&gt;&lt;br /&gt;&lt;span class="description"&gt;&lt;a href="'.get_bloginfo('home').'/wp-admin/edit-tags.php?taxonomy='.$field['id'].'"&gt;Manage '.$taxonomy-&gt;label.'&lt;/a&gt;&lt;/span&gt;';
                              break;
                              // post_list
                              case 'post_list':
                              $items = get_posts( array (
                                  'post_type' =&gt; $field['post_type'],
                                  'posts_per_page' =&gt; -1
                              ));
                                  echo '&lt;select name="'.$field['id'].'" id="'.$field['id'].'"&gt;
                                          &lt;option value=""&gt;Select One&lt;/option&gt;'; // Select One
                                      foreach($items as $item) {
                                          echo '&lt;option value="'.$item-&gt;ID.'"',$meta == $item-&gt;ID ? ' selected="selected"' : '','&gt;'.$item-&gt;post_type.': '.$item-&gt;post_title.'&lt;/option&gt;';
                                      } // end foreach
                                  echo '&lt;/select&gt;&lt;br /&gt;&lt;span class="description"&gt;'.$field['desc'].'&lt;/span&gt;';
                              break;
                              // date
                              case 'date':
                                  echo '&lt;input type="text" class="datepicker" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" /&gt;
                                          &lt;br /&gt;&lt;span class="description"&gt;'.$field['desc'].'&lt;/span&gt;';
                              break;
                              // slider
                              case 'slider':
                              $value = $meta != '' ? $meta : '0';
                                  echo '&lt;div id="'.$field['id'].'-slider"&gt;&lt;/div&gt;
                                          &lt;input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$value.'" size="5" /&gt;
                                          &lt;br /&gt;&lt;span class="description"&gt;'.$field['desc'].'&lt;/span&gt;';
                              break;
                              // image
                              case 'image':
                                  $image = get_template_directory_uri().'/images/image.png';  
                                  echo '&lt;span class="custom_default_image" style="display:none"&gt;'.$image.'&lt;/span&gt;';
                                  if ($meta) { $image = wp_get_attachment_image_src($meta, 'medium'); $image = $image[0]; }               
                                  echo    '&lt;input name="'.$field['id'].'" type="hidden" class="custom_upload_image" value="'.$meta.'" /&gt;
                                              &lt;img src="'.$image.'" class="custom_preview_image" alt="" /&gt;&lt;br /&gt;
                                                  &lt;input class="custom_upload_image_button button" type="button" value="Choose Image" /&gt;
                                                  &lt;small&gt;&amp;nbsp;&lt;a href="#" class="custom_clear_image_button"&gt;Remove Image&lt;/a&gt;&lt;/small&gt;
                                                  &lt;br clear="all" /&gt;&lt;span class="description"&gt;'.$field['desc'].'&lt;/span&gt;';
                              break;
                              // repeatable
                              case 'repeatable':
                                  echo '&lt;a class="repeatable-add button" href="#"&gt;+&lt;/a&gt;
                                          &lt;ul id="'.$field['id'].'-repeatable" class="custom_repeatable"&gt;';
                                  $i = 0;
                                  if ($meta) {
                                      foreach($meta as $row) {
                                          echo '&lt;li&gt;&lt;span class="sort hndle"&gt;|||&lt;/span&gt;
                                                      &lt;input type="text" name="'.$field['id'].'['.$i.']" id="'.$field['id'].'" value="'.$row.'" size="30" /&gt;
                                                      &lt;a class="repeatable-remove button" href="#"&gt;-&lt;/a&gt;&lt;/li&gt;';
                                          $i++;
                                      }
                                  } else {
                                      echo '&lt;li&gt;&lt;span class="sort hndle"&gt;|||&lt;/span&gt;
                                                  &lt;input type="text" name="'.$field['id'].'['.$i.']" id="'.$field['id'].'" value="" size="30" /&gt;
                                                  &lt;a class="repeatable-remove button" href="#"&gt;-&lt;/a&gt;&lt;/li&gt;';
                                  }
                                  echo '&lt;/ul&gt;
                                      &lt;span class="description"&gt;'.$field['desc'].'&lt;/span&gt;';
                              break;
                          } //end switch
                  echo '&lt;/td&gt;&lt;/tr&gt;';
              } // end foreach
              echo '&lt;/table&gt;'; // end table
          }
  
          // Save the Data
          function home_save_custom_meta($post_id) {
              global $home_custom_meta_fields;
  
              // verify nonce
              if (!wp_verify_nonce($_POST['custom_meta_box_nonce'], basename(__FILE__))) 
                  return $post_id;
              // check autosave
              if (defined('DOING_AUTOSAVE') &amp;&amp; DOING_AUTOSAVE)
                  return $post_id;
              // check permissions
              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;
              }
  
              // loop through fields and save the data
              foreach ($home_custom_meta_fields as $field) {
                  $old = get_post_meta($post_id, $field['id'], true);
                  $new = $_POST[$field['id']];
                  if ($new &amp;&amp; $new != $old) {
                      update_post_meta($post_id, $field['id'], $new);
                  } elseif ('' == $new &amp;&amp; $old) {
                      delete_post_meta($post_id, $field['id'], $old);
                  }
              } // enf foreach
          }
          add_action('save_post', 'home_save_custom_meta');
  
          }
  </code></pre>
  
  <p>Here's how I've wrapped all categories into a array:</p>
  
  <pre><code>            // Get all categories from WP and create a dropdown
          $categories = get_categories('hide_empty=0&amp;orderby=name');
          $wp_cats = array();
          foreach ($categories as $category_list ) {
                 $wp_cats[$category_list-&gt;cat_ID] = $category_list-&gt;cat_name;
          }
          array_unshift($wp_cats, "Choose a category"); 
  </code></pre>
  
  <p>Here's what I expect from the $wp_cats array:</p>
  
  <pre><code>'options' =&gt; array (
      'one' =&gt; array (
          'label' =&gt; 'Option One',
          'value' =&gt; 'one'
      ),
      'two' =&gt; array (
          'label' =&gt; 'Option Two',
          'value' =&gt; 'two'
      ),
      'three' =&gt; array (
          'label' =&gt; 'Option Three',
          'value' =&gt; 'three'
      )
  )
  </code></pre>
  
  <p>Here's an image with the details:
  <img src="https://i.stack.imgur.com/EpZMs.jpg" alt="enter image description here"></p>