• Hi, I am working on a demo project and have been stuck in this stage for past few hours. I intend to create a custom post with custom meta fields which will save my contact details. I followed an tutorial on how to create multiple fields at once using array. Everything is nicely done and showing all the fields, however I can not get to show data entered in the custom fields. I am posting my code to show where am I doing wrong? I really need this to work so any help would be super welcome.
    Thanks in advance.
    The code

    <?php
    function contact_details(){
      $labels = array(
        'name' => _x('Contact', 'post type general name'),
        'singular_name' => _x('Contact Details', 'post type singular_name'),
        'add_new' => _x('Add New', 'item'),
        'add_new_item' => __('Add New Item'),
        'edit_item' => __('Edit Item'),
        'new_item' => __('New Item'),
        'view_item' => __('View Item'),
        'search_items' => __('Search'),
        'not_found' => __('Nothing found'),
        'not_found_in_trash' => __('Nothing found in trash'),
        'parent_item_colon' => ''
        );
    
      $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'menu_position' => 6,
        'taxonomies' => array('category'),
        'supports' => array('title', 'thumbnail')
    
        );
    
      register_post_type('contacts', $args);
      flush_rewrite_rules();
    
    }
    add_action('init', 'contact_details');
    
    function contact_meta_box(){
      add_meta_box(
        'contact_details_meta_box',
        'Add Contact Deatils',
        'show_contact_meta_box',
        'contacts',
        'normal',
        'high'
        );
    }
    
    add_action('add_meta_boxes', 'contact_meta_box');
    
    // field array
    $prefix = 'custom';
    $custom_meta_fields = array(
      array(
        'label' => 'Mail',
        'desc' => 'enter email address',
        'id' => $prefix.'mail',
        'type' => 'email'
        ),
      array(
        'label' => 'Contact Number',
        'desc' => 'enter contact number',
        'id' => $prefix.'text',
        'type' => 'text'
        ),
      array(
        'label' => 'Address',
        'desc' => 'enter address',
        'id' => $prefix.'text',
        'type' => 'textarea'
        ),
      array(
        'label' => 'Skype name',
        'desc' => 'enter skype name',
        'id' => $prefix.'text',
        'type' => 'text'
        ),
      );
    
    function show_contact_meta_box(){
      global $custom_meta_fields, $contacts;
    
      echo '
    <ul>';
      foreach ($custom_meta_fields as $field){
        $meta = get_post_meta($post->ID, $field['id'], true);
    
        echo ' <label for="'.$field['id'].'">'. $field['label']. '</label>
    <li>';
        switch($field['type']){
    
          case'email':
          echo '<input type="email" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'">
    
            <span class="desc">'.$field['desc'].'</span>';
            break;
    
            case'text':
            echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'">
    
            <span class="desc">'.$field['desc'].'</span>';
            break;
    
            case'textarea':
            echo '<textarea name="'.$field['id'].'" id="'.$field['id'].'">'.$meta.'</textarea>
    
            <span class="desc">'.$field['desc'].'</span>';
            break;
        }
        echo '</li>
    ';
      }
    echo '</ul>
    ';
    }
    
    function save_custom_meta($post_id){
      global $custom_meta_fields;
    
      if('contacts' == $_POST['post_type']){
    
        if ( !current_user_can( 'edit_post', $post_id ) )
            return $post_id;
      }
      else
      {
        if ( !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);
            }
      }
    }
    
    add_action('save_post', 'save_custom_meta');
    ?>
  • The topic ‘Custom meta boxes array not saving data’ is closed to new replies.