2013-06-18 22 views
0

我想創建一個元框,它應該很好,很容易,但我錯了。它涉及到一個旅遊自定義後,並加入價格爲旅遊:WordPress的 - 創建元框和保存

所以最初我創建這個自定義文章類型(這工作):

register_post_type('tours', 
      array(
       'labels' => array(
        'name' => __('Tours'), 
        'singular_name' => __('Tour'), 
        'add_new' => 'Add New Tour Instance', 
        'add_new_item' => 'Add New Tour Instance', 
        'edit' => 'Edit', 
        'edit_item' => 'Edit Tour Instance', 
        'new_item' => 'New Tour Instance', 
        'view' => 'View', 
        'view_item' => 'View Tour Instance', 
        'search_items' => 'Search Tour Instances', 
        'not_found' => 'No Tour Instances found', 
        'not_found_in_trash' => 'No Tour Instances found in Rubbish', 
        'parent' => 'Parent Tour Instance' 
       ), 
      'public' => true, 
      'supports' => array('title', 'editor', 'thumbnail'), 
      'taxonomies' => array(''), 
      //'menu_icon' => plugins_url('images/image.png', __FILE__), 
      'capability_type' => 'post', 
      'rewrite' => array("slug" => "tours") // Permalinks format 
      ) 
     ); 

然後,我創建盒子本身(這也工程):

add_action('admin_init', 'tour_meta'); 
     function tour_meta() { 
      add_meta_box(
       'tours_meta_box', 
       'Tour Price', 
       'display_tours_price_meta_box', 
       'tours', 
       'side', 
       'high' 
      ); 
     } 

現在,整體功能中,然後我試圖讓細節,並將其保存:

function display_tours_price_meta_box() { 
      global $post; 
      // Noncename needed to verify where the data originated 
      echo '<input type="hidden" name="tourmeta_noncename" id="tourmeta_noncename" value="' . 
      wp_create_nonce(plugin_basename(__FILE__)) . '" />'; 

      // Get the price data if its already been entered 
      $price = get_post_meta($post->ID, '_price', true); 
      echo 'Add the total cost for this tour here, do not include monetary characters like £ or $'; 
      echo '<input type="text" name="_price" ' . $price . '" class="widefat" />'; 
     } 
     function save_tours_meta($tour_id, $tour) { 
      if (!wp_verify_nonce($_POST['tourmeta_noncename'], plugin_basename(__FILE__))) { 
      return $post->ID; 
      } 
      // Is the user allowed to edit the post or page? 
      if (!current_user_can('edit_post', $post->ID)) 
       return $post->ID; 

      $tour_meta['_price'] = $_POST['_price']; 

      // Add values of $tour_meta as custom fields 
      foreach ($tour_meta as $key => $value) { 
       if($post->post_type == 'revision') return; 
       $value = implode(',', (array)$value); 
       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', 'save_tours_meta', 1, 2); 

出現實際框,並且信息被回顯。但它不能保存。我沒有模糊的原因。它必須與我最後的功能有關,但我不明白髮生了什麼問題。

感謝

回答

1

你應該更好地簡化您的保存過程是這樣的:

function save_tours_meta($post_id) { 
     if (!wp_verify_nonce($_POST['tourmeta_noncename'], plugin_basename(__FILE__)))  { 
     // You don't need to return anything 
     return; 
     } 
     // Is the user allowed to edit the post or page? 
     if (!current_user_can('edit_post', $post_id) && !isset($_POST['_price'])) 
      return; 

     // If you just have 1 value 
     update_post_meta($post_id, '_price', $_POST['_price']); 
    } 

就注意到update_post_meta功能將更新後間或創建它,如果它不存在所以不要打擾我們​​3210。

0

我認爲這是你的觀點。您聲明函數爲

function save_tours_meta($tour_id, $tour) { 

但您正在使用(例如)$post->ID。在這種情況下,$post不是全局變量,而是函數參數。請嘗試使用$tour->ID或僅使用$tour_id

一對夫婦的其他(無關)的東西:

  1. 你可以調用update_post_meta(),它會添加鍵值,如果不存在的話,這樣你就可以刪除你if聲明(只是減少的線碼)。
  2. 您的「修訂版」檢查應該返回帖子ID(並且可以在您的循環之外移動)