2011-09-23 3 views
10

WordPress의 사용자 지정 메타 상자에 확인란을 추가하려고하는데 저장 문제가 발생했습니다. 확인란을 선택하고 게시/페이지를 업데이트 할 때마다 다시 선택 취소됩니다. 예상대로WordPress의 확인란 메타 박스를 저장하는 방법은 무엇입니까?

add_meta_box(
    'sl-meta-box-sidebar',  // id 
    'Sidebar On/Off',   // title 
    'sl_meta_box_sidebar',  // callback function 
    'page',      // type of write screen 
    'side',      // context 
    'low'      // priority 
); 

function sl_meta_box_sidebar() { 
    global $meta; sl_post_meta($post->ID); ?> 
    <input type="checkbox" name="sl_meta[sidebar]" value="<?php echo htmlspecialchars ($meta['sidebar']); ?>" />Check to turn the sidebar <strong>off</strong> on this page. 
} 

이것은, "편집 페이지"화면의 사이드 바에서 체크 박스를 만들고, 거기에 아무 문제 :

여기 내가 사용하는 코드입니다. 나는 체크 박스의 값에 무엇을 입력해야하는지 모르겠다. 메타 필드 정보로 저장된 텍스트 필드는 분명히 반환된다 ... 나는 단지 "checked"를 사용하여 그 대신에 내 첫 추측 일 것이다. 이 메타 데이터를 사용할 때의 값), 확인란도 저장하지 않았습니다.

function sl_save_meta_box($post_id, $post) { 
    global $post, $type; 

    $post = get_post($post_id); 

    if(!isset($_POST[ "sl_meta" ])) 
     return; 

    if($post->post_type == 'revision') 
     return; 

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

    $meta = apply_filters('sl_post_meta', $_POST[ "sl_meta" ]); 

    foreach($meta as $key => $meta_box) { 
     $key = 'meta_' . $key; 
     $curdata = $meta_box; 
     $olddata = get_post_meta($post_id, $key, true); 

     if($olddata == "" && $curdata != "") 
      add_post_meta($post_id, $key, $curdata); 
     elseif($curdata != $olddata) 
      update_post_meta($post_id, $key, $curdata, $olddata); 
     elseif($curdata == "") 
      delete_post_meta($post_id, $key); 
    } 

    do_action('sl_saved_meta', $post); 
} 

add_action('save_post', 'sl_save_meta_box', 1, 2); 

그것은 텍스트 필드에 대해 완벽하게 작동하지만, 체크 박스를 그냥 저장하지 않습니다 :

는 여기에 내가이 문제가 원인을 가정하는 모든 메타 데이터를 저장하는 기능입니다. 저장 기능이 잘못되었거나 확인란의 값에 대해 빠뜨린 것이 확실하지 않습니다.

도움을 주셨습니다.

답변

14

나는 이전에이 문제가 있었는데 여기서 어떻게 해결 했는가?

먼저 체크 박스를 만듭니다.

<?php 
function sl_meta_box_sidebar(){ 
    global $post; 
    $custom = get_post_custom($post->ID); 
    $sl_meta_box_sidebar = $custom["sl-meta-box-sidebar"][0]; 
?> 

<input type="checkbox" name="sl-meta-box-sidebar" <?php if($sl_meta_box_sidebar == true) { ?>checked="checked"<?php } ?> /> Check the Box. 
<?php } ?> 

다음으로, 절약하십시오.

<?php 
add_action('save_post', 'save_details'); 

function save_details($post_ID = 0) { 
    $post_ID = (int) $post_ID; 
    $post_type = get_post_type($post_ID); 
    $post_status = get_post_status($post_ID); 

    if ($post_type) { 
    update_post_meta($post_ID, "sl-meta-box-sidebar", $_POST["sl-meta-box-sidebar"]); 
    } 
    return $post_ID; 
} ?> 
관련 문제