title: Undefined index error in custom post metabox
body: <p>I'm getting this error with my custom post metabox. The custom post and metabox works fine, but if I have debug on and when I try to save any other page or post, I will get an error:</p>
  
  <pre><code>Notice: Undefined index: iam_video_select in C:\xampp\htdocs\Iam\wp-content\themes\Iam\inc\classes\Custom_Meta_Boxes.class.php on line 149
  
  Notice: Undefined index: iam_video_url in C:\xampp\htdocs\Iam\wp-content\themes\Iam\inc\classes\Custom_Meta_Boxes.class.php on line 149
  
  Notice: Undefined index: iam_link in C:\xampp\htdocs\Iam\wp-content\themes\Iam\inc\classes\Custom_Meta_Boxes.class.php on line 149
  </code></pre>
  
  <p>My code:</p>
  
  <pre><code>&lt;?php 
  
  $meta_boxes = array();
  
  //Video meta box
  $meta_boxes[] = array(
      'id'        =&gt; 'video-meta-box',
      'title'     =&gt; __( 'Video', 'iamtheme' ),
      'pages'     =&gt; 'post',
      'context'   =&gt; 'normal',
      'priority'  =&gt; 'high',
      'fields'    =&gt; array(
          array(
              'name'      =&gt; 'Select Video Host',
              'id'        =&gt; PTHEME . '_video_select',
              'type'      =&gt; 'select',
              'options'   =&gt; array( 'Youtube', 'Vimeo', 'Self hosted' )
          ),
          array(
              'name'      =&gt; 'URL',
              'desc'      =&gt; 'Enter video url here.',
              'id'        =&gt; PTHEME . '_video_url',
              'std'       =&gt; 'Default value here.',
              'type'      =&gt; 'text'
          )               
      )
  );
  
  //Link meta box
  $meta_boxes[] = array(
      'id'        =&gt; 'link-meta-box',
      'title'     =&gt; __( 'Link', 'iamtheme' ),
      'pages'     =&gt; 'post',
      'context'   =&gt; 'normal',
      'priority'  =&gt; 'high',
      'fields'    =&gt; array(
          array(
              'name'      =&gt; 'Link',
              'desc'      =&gt; 'Enter your url here.',
              'id'        =&gt; PTHEME . '_link',
              'std'       =&gt; 'Default value here.',
              'type'      =&gt; 'text'
          )
      )
  );
  
  /**
  * 
  */
  class Custom_Meta_Boxes{
  
      public $_meta_box;
  
      public function __construct( $meta_box ){
  
          $this-&gt;_meta_box = $meta_box;
          add_action( 'add_meta_boxes', array( $this, 'iam_add_meta_box' ) );
          add_action( 'save_post', array( $this, 'iam_save_meta_box_data' ) );
  
      }
  
      /**
       * Adds a meta box to the post editing screen
       */
      public function iam_add_meta_box(){
  
          add_meta_box(
              $this-&gt;_meta_box['id'], 
              $this-&gt;_meta_box['title'],
              array( &amp;$this, 'iam_display_custom_meta_box' ), 
              $this-&gt;_meta_box['pages'], 
              $this-&gt;_meta_box['context'], 
              $this-&gt;_meta_box['priority']
          );
  
      }
  
      /**
       * Render Meta Box content.
       */
      public function iam_display_custom_meta_box() {
  
          global $post;
  
          // Add an nonce field so we can check for it later.
          wp_nonce_field( 'iam_nonce_check', 'iam_nonce_check_value' );
  
          echo '&lt;div class="metabox-wrapper"&gt;';
  
              foreach ( $this-&gt;_meta_box['fields'] as $field) {
  
                  // get current post meta data
                  $meta = get_post_meta( $post-&gt;ID, $field['id'], true );
  
                  echo '&lt;div class="metabox-fields metabox_' , $field['type'] , '"&gt;';
  
                      echo '&lt;label for="', $field['id'] , '"&gt;', $field['name'] , '&lt;/label&gt;';
  
                      switch ( $field['type'] ) {
  
                          case 'text':
                          echo '&lt;input type="text" name="', $field['id'] , '" id="', $field['id'] , '" value="', $meta , '" /&gt;';
                          echo '&lt;p class="meta-desc"&gt;' , $field['desc'] , '&lt;/p&gt;';
                          break;
  
                          case 'textarea':
                          echo '&lt;textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%"&gt;', $meta , '&lt;/textarea&gt;';
                          echo '&lt;p class="meta-desc"&gt;' , $field['desc'] , '&lt;/p&gt;';
                          break;
  
                          case 'select':
                          echo '&lt;select name="', $field['id'], '" id="', $field['id'], '"&gt;';
                          foreach ( $field['options'] as $option ) {
                              echo '&lt;option', $meta == $option ? ' selected="selected"' : '', '&gt;', $option, '&lt;/option&gt;';
                          }
                          echo '&lt;/select&gt;';
                          break;
  
                          case 'radio':
                          foreach ( $field['options'] as $option ) {
                              echo '&lt;input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' /&gt;', $option['name'];
                          }
                          break;
  
                          case 'checkbox':
                          echo '&lt;input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' /&gt;';
                          break;
  
                      }
  
                  echo '&lt;/div&gt;';
  
              }
  
          echo '&lt;/div&gt;';
      }
  
      /**
       * Save the meta when the post is saved.
       */
      public function iam_save_meta_box_data( $post_id ){
  
          if( $this-&gt;iam_user_can_save( $post_id, 'iam_nonce_check_value' ) ) {
  
              // Checks for input and sanitizes/saves if needed
              foreach ( $this-&gt;_meta_box['fields'] as $field ) {
  
                  $old = get_post_meta( $post_id, $field['id'], true );
                  $new = sanitize_text_field( $_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 );
  
                  }
              }
          }
  
      }
  
      /**
       * Determines whether or not the current user has the ability to save meta 
       * data associated with this post.
       *
       * @param       int     $post_id    The ID of the post being save
       * @param       bool                Whether or not the user has the ability to save this post.
      */
      public function iam_user_can_save( $post_id, $nonce ){
  
          // Checks save status
          $is_autosave = wp_is_post_autosave( $post_id );
          $is_revision = wp_is_post_revision( $post_id );
          $is_valid_nonce = ( isset( $_POST[ $nonce ] ) &amp;&amp; wp_verify_nonce( $_POST[ $nonce ], 'iam_nonce_check' ) ) ? 'true' : 'false';
  
          // Return true if the user is able to save; otherwise, false.
          return ! ( $is_autosave || $is_revision ) &amp;&amp; $is_valid_nonce;
  
      }
  
  }
  
  // Instantiate theme
  if ( class_exists( 'Custom_Meta_Boxes' ) ){
      foreach ( $meta_boxes as $meta_box ) {
          $my_box = new Custom_Meta_Boxes( $meta_box );
      }
  }
  
  ?&gt;
  </code></pre>
  
  <p>Thanks for any helps.</p>