2016-08-30 73 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; 
 

 
}

我希望帮你。