2016-08-30 65 views
2

好吧我正在尋找一種方法,使用戶可以提交評論前所需的評論評級。我正在使用add_filter preprocess_comment來檢查評級是否已設置,並且拋出wp_die並停止評論的上傳。我發現了一種我認爲我可以通過下面的鏈接完成的方法。我的擔心之一是我的評分系統和評論批准系統都在完全不同的文件中。無論如何,我在我的評分系統中嘗試了下面的代碼,它在那裏保存了評分。但我一直有一個錯誤嘗試發表評論。 https://www.smashingmagazine.com/2012/05/adding-custom-fields-in-wordpress-comment-form/WordPress的add_filter預處理評論要求評分

//in the construct function 
add_filter('preprocess_comment', 'verify_comment_meta_data'); 

function verify_comment_meta_data($commentdata) { 
     if (! isset($_POST['pixrating'])) 
     wp_die(__('Error: You did not add a rating. Hit the Back button on your Web browser and resubmit your comment with a rating.')); 
    return $commentdata; 
    } 

Rating Error

基本上我只是問有什麼我做錯了,因爲我還是有點新文字新聞和過濾器。

編輯:我沒有使用在那裏發現的確切代碼,我拿出了它的部分代碼工作。我正在使用這裏找到的插件評論評級。 https://wordpress.org/plugins/comments-ratings/

+0

你可以把你的文件容器這些代碼?請。 –

+0

你是什麼意思? – jakecolor

+0

對不起我的英文。我需要審閱你的所有代碼。 –

回答

0

www.smashingmagazine.com上的代碼是完美的,我將所有代碼複製並粘貼到一個文件中並且工作正常。

我建議您將此代碼複製並粘貼到extended-comments.php文件並重試測試。

<?php 
 

 
/* 
 
Plugin Name: Extended Comments 
 
Plugin URI: https://www.smashingmagazine.com/2012/05/adding-custom-fields-in-wordpress-comment-form/ 
 
Description: A brief description of the Plugin. 
 
Version: 1.0 
 
Author: Jose Carlos Ramos Carmenates (Copy and paste) 
 
Author URI: http://URI_Of_The_Plugin_Author 
 
License: A "Slug" license name e.g. GPL2 
 
*/ 
 

 
// Add custom meta (ratings) fields to the default comment form 
 
// Default comment form includes name, email address and website URL 
 
// Default comment form elements are hidden when user is logged in 
 

 
add_filter('comment_form_default_fields', 'custom_fields'); 
 
function custom_fields($fields) { 
 

 
\t $commenter = wp_get_current_commenter(); 
 
\t $req = get_option('require_name_email'); 
 
\t $aria_req = ($req ? " aria-required='true'" : ''); 
 

 
\t $fields[ 'author' ] = '<p class="comment-form-author">'. 
 
\t      '<label for="author">' . __('Name') . '</label>'. 
 
\t      ($req ? '<span class="required">*</span>' : ''). 
 
\t      '<input id="author" name="author" type="text" value="'. esc_attr($commenter['comment_author']) . 
 
\t      '" size="30" tabindex="1"' . $aria_req . ' /></p>'; 
 

 
\t $fields[ 'email' ] = '<p class="comment-form-email">'. 
 
\t      '<label for="email">' . __('Email') . '</label>'. 
 
\t      ($req ? '<span class="required">*</span>' : ''). 
 
\t      '<input id="email" name="email" type="text" value="'. esc_attr($commenter['comment_author_email']) . 
 
\t      '" size="30" tabindex="2"' . $aria_req . ' /></p>'; 
 

 
\t $fields[ 'url' ] = '<p class="comment-form-url">'. 
 
\t     '<label for="url">' . __('Website') . '</label>'. 
 
\t     '<input id="url" name="url" type="text" value="'. esc_attr($commenter['comment_author_url']) . 
 
\t     '" size="30" tabindex="3" /></p>'; 
 

 
\t $fields[ 'phone' ] = '<p class="comment-form-phone">'. 
 
\t      '<label for="phone">' . __('Phone') . '</label>'. 
 
\t      '<input id="phone" name="phone" type="text" size="30" tabindex="4" /></p>'; 
 

 
\t return $fields; 
 
} 
 

 

 
// 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() { 
 
\t echo '<p class="comment-form-title">'. 
 
\t  '<label for="title">' . __('Comment Title') . '</label>'. 
 
\t  '<input id="title" name="title" type="text" size="30" tabindex="5" /></p>'; 
 

 
\t echo '<p class="comment-form-rating">'. 
 
\t  '<label for="rating">'. __('Rating') . '<span class="required">*</span></label> 
 
    <span class="commentratingbox">'; 
 

 
\t //Current rating scale is 1 to 5. If you want the scale to be 1 to 10, then set the value of $i to 10. 
 
\t for($i=1; $i <= 5; $i++) 
 
\t \t echo '<span class="commentrating"><input type="radio" name="rating" id="rating" value="'. $i .'"/>'. $i .'</span>'; 
 

 
\t echo'</span></p>'; 
 

 
} 
 

 

 

 
// Save the comment meta data along with comment 
 

 
add_action('comment_post', 'save_comment_meta_data'); 
 
function save_comment_meta_data($comment_id) { 
 
\t if ((isset($_POST['phone'])) && ($_POST['phone'] != '')) 
 
\t \t $phone = wp_filter_nohtml_kses($_POST['phone']); 
 
\t add_comment_meta($comment_id, 'phone', $phone); 
 

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

 
\t if ((isset($_POST['rating'])) && ($_POST['rating'] != '')) 
 
\t \t $rating = wp_filter_nohtml_kses($_POST['rating']); 
 
\t add_comment_meta($comment_id, 'rating', $rating); 
 
} 
 

 

 
// Add the filter to check whether the comment meta data has been filled 
 

 
add_filter('preprocess_comment', 'verify_comment_meta_data'); 
 
function verify_comment_meta_data($commentdata) { 
 
\t if (! isset($_POST['rating'])) 
 
\t \t wp_die(__('Error: You did not add a rating. Hit the Back button on your Web browser and resubmit your comment with a rating.')); 
 
\t return $commentdata; 
 
} 
 

 

 
// 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() { 
 
\t add_meta_box('title', __('Comment Metadata - Extend Comment'), 'extend_comment_meta_box', 'comment', 'normal', 'high'); 
 
} 
 

 
function extend_comment_meta_box ($comment) { 
 
\t $phone = get_comment_meta($comment->comment_ID, 'phone', true); 
 
\t $title = get_comment_meta($comment->comment_ID, 'title', true); 
 
\t $rating = get_comment_meta($comment->comment_ID, 'rating', true); 
 
\t wp_nonce_field('extend_comment_update', 'extend_comment_update', false); 
 
\t ?> 
 
\t <p> 
 
\t \t <label for="phone"><?php _e('Phone'); ?></label> 
 
\t \t <input type="text" name="phone" value="<?php echo esc_attr($phone); ?>" class="widefat" /> 
 
\t </p> 
 
\t <p> 
 
\t \t <label for="title"><?php _e('Comment Title'); ?></label> 
 
\t \t <input type="text" name="title" value="<?php echo esc_attr($title); ?>" class="widefat" /> 
 
\t </p> 
 
\t <p> 
 
\t \t <label for="rating"><?php _e('Rating: '); ?></label> 
 
     <span class="commentratingbox"> 
 
     <?php for($i=1; $i <= 5; $i++) { 
 
\t  echo '<span class="commentrating"><input type="radio" name="rating" id="rating" value="'. $i .'"'; 
 
\t  if ($rating == $i) echo ' checked="checked"'; 
 
\t  echo ' />'. $i .' </span>'; 
 
     } 
 
     ?> 
 
     </span> 
 
\t </p> 
 
\t <?php 
 
} 
 

 
// Update comment meta data from comment editing screen 
 

 
add_action('edit_comment', 'extend_comment_edit_metafields'); 
 

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

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

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

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

 
}

我希望幫你。