2016-07-30 2 views
0

Wordpress (버전 4.5.3)를 사용 중이며 코멘트 양식에 사용자 정의 필드를 추가했습니다. 필드는 select 태그를 사용하는 드롭 다운 목록입니다. 필드는 양식에 잘 나타나서 옵션을 선택할 수 있으며 해당 옵션의 값이 올바르게 저장됩니다.Wordpress에서 코멘트 메타 데이터를 편집 할 수 없습니다.

또한 주석 편집 페이지 (/wordpress/wp-admin/comment.php?action=editcomment)에서 사용자 정의 필드를 편집하는 코드를 추가했습니다. 저장된 값을 검색 한 다음 적절한 값을 선택하여 드롭 다운 목록을 다시 만들 수 있습니다.

사용자 정의 필드의 편집 된 값을 저장하는 코드를 추가했지만 작동하지 않습니다. 코멘트 편집 페이지에서 드롭 다운에서 다른 값을 선택한 다음 "업데이트"버튼을 클릭하면 새로 선택된 값이 저장되지 않습니다.

function.php에서이 모든 작업을 수행하는 코드를 추가하고 있습니다. 여기

// Add fields after default fields above the comment box, always visible 

    add_action('comment_form_logged_in_after', 'additional_fields'); 
    add_action('comment_form_after_fields', 'additional_fields'); 

    function additional_fields() { 

    echo '<p class="comment-form-area">'. 
    '<label for="region">' . __('Choose a <strong>region</strong>') . '<span class="required">*&nbsp;&nbsp;&nbsp;&nbsp;</span></label>'.   
    '<br /><select id="region" name="region"> 
    <option value="one">one</option> 
    <option value="two">two</option> 
    <option value="three">three</option> 
    <option value="four">four</option>  
    </select></p>'; 
    } 

    // Save the comment meta data along with comment 

    add_action('comment_post', 'save_comment_meta_data'); 
    function save_comment_meta_data($comment_id) { 

     if ((isset($_POST['region'])) && ($_POST['region'] != '')) 
     $region = wp_filter_nohtml_kses($_POST['region']);    
     add_comment_meta($comment_id, 'region', $region);  

    } 

가 주석 편집 페이지에 사용자 정의 필드를 추가하는 코드는 다음과 같습니다 : 다음 코드는 폼에 필드를 추가하고 데이터를 저장하는 데있어

// Add an edit option to comment editing screen 

    add_action('add_meta_boxes_comment', 'extend_comment_add_meta_box'); 

    function extend_comment_add_meta_box() { 
     add_meta_box('title', __('Region field'),    'extend_comment_meta_box', 'comment', 'normal', 'high'); 
    } 

    function extend_comment_meta_box ($comment) { 

    $_regions = array 
     ("one", 
     "two", 
     "three", 
     "four"); 


     $region = get_comment_meta($comment->comment_ID, 'region', true);   
     ?> 
     <p> 
      <label for="region"><?php _e('Region*'); ?></label> 
      <p><select id="region" name="region"> 
     <?php 
     for ($ix = 0; $ix < count($_regions); $ix++) { 
      echo '<option value="' . $_regions[$ix] . '"'; 
      if ($region == $_regions[$ix]) { 
       echo ' selected';     
      } 
     echo '>' . $_regions[$ix] . '</option>';  
     } 
     ?> 
     </select></p> 

    <?php 
    } 

이 모든 작동하는 것 같군 벌금. 다음은 맞춤 입력란의 수정 된 값을 저장하는 코드입니다.

// Update comment meta data from comment editing screen 

    add_action('edit_comment', 'extend_comment_edit_metafields'); 

    function extend_comment_edit_metafields($comment_id) { 
     if(! isset($_POST['extend_comment_update']) || ! wp_verify_nonce(  $_POST['extend_comment_update'], 'extend_comment_update')) return; 

     if ((isset($_POST['region'])) && ($_POST['region'] != '')) : 
      $region = wp_filter_nohtml_kses($_POST['region']); 
      update_comment_meta($comment_id, 'region', $region); 
     else : 
      delete_comment_meta($comment_id, 'region'); 
     endif; 

    } 

이 코드는 작동하지 않습니다. 댓글 편집 페이지로 돌아 가면 편집 된 값이 아닌 사용자 정의 필드의 원래 값을 볼 수 있습니다.

왜 편집 된 값이 저장되지 않습니까?

답변

0

extend_comment_edit_metafields() 함수는 처음에 nonce 필드가 설정되었는지 확인하기 위해 테스트합니다. update_comment_meta()를 호출하지 않고 반환이 실행되지는 않습니다. 이 같은) 임시 필드가 extend_comment_meta_box (설정해야 제대로 작동하려면

는 임시 필드의
wp_nonce_field('extend_comment_update', 'extend_comment_update', false); 

토론은 여기에 있습니다 : https://codex.wordpress.org/Function_Reference/wp_nonce_field

관련 문제