2016-08-30 11 views
2

私は、ユーザーがコメントを送信する前に、必要なコメントの評価を行う方法を探しています。私はadd_filter preprocess_commentを使用して、評価が設定されているかどうかを確認し、wp_dieをスローしてコメントのアップロードを停止します。私は私が下のリンクを通してそれを行うことができると思う方法を見つけた。私の懸念事項の1つは、評価システムとコメント承認システムが全く別のファイルにあることです。とにかく私は私の評価システムで以下のコードを試して評価を保存しました。しかし、私は間違いを続けてコメントしてみてください。 https://www.smashingmagazine.com/2012/05/adding-custom-fields-in-wordpress-comment-form/WordPress add_filter preprocesscomment rating required

//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; 
 

 
}

私はあなたを助ける願っています。

関連する問題