2011-07-28 6 views
1

워드 프레스 메타 데이터 코드를 워드 프레스 테마의 functions.php에 추가했습니다. 메타 볼 데이터가 전혀 저장되지 않는 문제에 직면했습니다. 사라집니다. 초안을 저장하거나 게시하십시오. 한 달 전에 완벽하게 작동하는 이전 버전의 코드가 있었지만 일부 오류로 인해 더 이상 필요하지 않으며이 초안에서 코드가 어떻게 작동 하는지를 기억할 수 없습니다.WordPress 메타 박스 데이터가 저장되지 않고 사라졌습니다.

나는 4 시간 오늘이 코드에 응시 한, 나는 대답은 간단해야 알아,하지만 그것은 나를 미친 운전 거라고 :

add_action('save_post', 'mytheme_save_data'); 

// Save data from meta box 
function mytheme_save_data($post_id) { 
    global $meta_box; 

    // verify nonce 
    if (!wp_verify_nonce($_POST['mytheme_meta_box_nonce'], basename(__FILE__))) { 
     return $post_id; 
    } 

    // check autosave 
    if (defined('DOING_AUTOSAVE') && 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; 
    } 

    foreach ($meta_box['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); 
     } 
    } 
} 

도움을 주 시겠어요?

+0

어디에서 $ meta_box 전역 변수를 설정 했습니까? – hadvig

답변

2
function save_portfolio_meta($post_id, $post) 
{ 
    if (!wp_verify_nonce($_POST['portfoliometa_noncename'], plugin_basename(__FILE__))) return $post->ID; 

    if (!current_user_can('edit_post', $post_id)) 
     return $post_id; 

    $portfolio_meta['_link'] = $_POST['_link']; 
    $portfolio_meta['_class'] = $_POST['_class']; 

    foreach($portfolio_meta as $key => $value) 
    {  
     $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_portfolio_meta', 1, 2); 
관련 문제