2012-12-30 87 views
-1

所以我一直在玩一些自定義metaboxes和後續教程等 - 我已經到了頂部metabox字段正確保存的點,但第二個(寬度)doesn不保存 - 有什麼明顯的我在這裏做錯了嗎?我嘗試將第二個字段的保存分隔到一個單獨的函數中進行測試,但這也不起作用。WordPress Meta Box - 第二個字段沒有保存

add_action('add_meta_boxes', 'youtube_metaboxes'); 

    function youtube_metaboxes() { 
     add_meta_box('wpt_youtube', 'youtube URL', 'wpt_youtube', 'product', 'side', 'default'); 
    } 

    function wpt_youtube() { 
     global $post; 
     echo '<input type="hidden" name="youtubemeta_noncename" id="youtubemeta_noncename" value="' . 
     wp_create_nonce(plugin_basename(__FILE__)) . '" />'; 
     $addyoutube = get_post_meta($post->ID, '_youtube', true); 
     echo '<input type="text" name="_youtube" value="' . $addyoutube . '" class="widefat" />'; 
     $youtubeWidth = get_post_meta($post->ID, '_width', true); 
     echo 'Width: <br /><input type="text" name="_width" value="' . $youtubeWidth . '" class="widefat" />'; 
    } 

    // Save the Metabox Data 
    function wpt_save_youtube_meta($post_id, $post) { 
     if (!wp_verify_nonce($_POST['youtubemeta_noncename'], plugin_basename(__FILE__))) { 
     return $post->ID; 
     } 
     if (!current_user_can('edit_post', $post->ID)) 
      return $post->ID; 
     $addyoutube_meta['_youtube'] = $_POST['_youtube']; 
     foreach ($addyoutube_meta as $key => $value) { 
      if($post->post_type == 'revision') return; 
      $value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely) 
      if(get_post_meta($post->ID, $key, FALSE)) { 
       update_post_meta($post->ID, $key, $value); 
      } else { 
       add_post_meta($post->ID, $key, $value); 
      } 
      if(!$value) delete_post_meta($post->ID, $key); 
     } 

     $addWidth_meta['_width'] = $_POST['_width']; 
     foreach ($addWidth_meta as $key => $value) { 
      if($post->post_type == 'revision') return; 
      $value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely) 
      if(get_post_meta($post->ID, $key, FALSE)) { 
       update_post_meta($post->ID, $key, $value); 
      } else { 
       add_post_meta($post->ID, $key, $value); 
      } 
      if(!$value) delete_post_meta($post->ID, $key); 
     } 
    } 

add_action('save_post', 'wpt_save_youtube_meta', 1, 2); // save the custom fields 

回答

相關問題