Advertisement
terorama

WP / Comment form custom fields

Mar 10th, 2013
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.15 KB | None | 0 0
  1. <?php
  2. add_filter('comment_form_default_fields', 'custom_fields');
  3. function custom_fields($fields) {
  4.  
  5.         $commenter = wp_get_current_commenter();
  6.         $req = get_option( 'require_name_email' );
  7.         $aria_req = ( $req ? " aria-required='true'" : '' );
  8.  
  9.         $fields[ 'author' ] = '<p class="comment-form-author">'.
  10.             '<label for="author">' . __( 'Name' ) . '</label>'.
  11.             ( $req ? '<span class="required">*</span>' : '' ).
  12.             '<input id="author" name="author" type="text" value="'. esc_attr( $commenter['comment_author'] ) .
  13.             '" size="30" tabindex="1"' . $aria_req . ' /></p>';
  14.  
  15.         $fields[ 'email' ] = '<p class="comment-form-email">'.
  16.             '<label for="email">' . __( 'Email' ) . '</label>'.
  17.             ( $req ? '<span class="required">*</span>' : '' ).
  18.             '<input id="email" name="email" type="text" value="'. esc_attr( $commenter['comment_author_email'] ) .
  19.             '" size="30"  tabindex="2"' . $aria_req . ' /></p>';
  20.  
  21.         $fields[ 'url' ] = '<p class="comment-form-url">'.
  22.             '<label for="url">' . __( 'Website' ) . '</label>'.
  23.             '<input id="url" name="url" type="text" value="'. esc_attr( $commenter['comment_author_url'] ) .
  24.             '" size="30"  tabindex="3" /></p>';
  25.  
  26.         $fields[ 'phone' ] = '<p class="comment-form-phone">'.
  27.             '<label for="phone">' . __( 'Phone' ) . '</label>'.
  28.             '<input id="phone" name="phone" type="text" size="30"  tabindex="4" /></p>';
  29.  
  30.     return $fields;
  31. }
  32.  
  33. add_action( 'comment_form_logged_in_after', 'additional_fields' );
  34. add_action( 'comment_form_after_fields', 'additional_fields' );
  35.  
  36. function additional_fields () {
  37.     echo '<p class="comment-form-title">'.
  38.     '<label for="title">' . __( 'Comment Title' ) . '</label>'.
  39.     '<input id="title" name="title" type="text" size="30"  tabindex="5" /></p>';
  40.  
  41.     echo '<p class="comment-form-rating">'.
  42.     '<label for="rating">'. __('Rating') . '<span class="required">*</span></label>
  43.     <span class="commentratingbox">';
  44.  
  45.         //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.
  46.         for( $i=1; $i <= 5; $i++ )
  47.         echo '<span class="commentrating"><input type="radio" name="rating" id="rating" value="'. $i .'"/>'. $i .'</span>';
  48.  
  49.     echo'</span></p>';
  50.  
  51. }
  52.  
  53. add_action( 'comment_post', 'save_comment_meta_data' );
  54. function save_comment_meta_data( $comment_id ) {
  55.     if ( ( isset( $_POST['phone'] ) ) && ( $_POST['phone'] != '') )
  56.     $phone = wp_filter_nohtml_kses($_POST['phone']);
  57.     add_comment_meta( $comment_id, 'phone', $phone );
  58.  
  59.     if ( ( isset( $_POST['title'] ) ) && ( $_POST['title'] != '') )
  60.     $title = wp_filter_nohtml_kses($_POST['title']);
  61.     add_comment_meta( $comment_id, 'title', $title );
  62.  
  63.     if ( ( isset( $_POST['rating'] ) ) && ( $_POST['rating'] != '') )
  64.     $rating = wp_filter_nohtml_kses($_POST['rating']);
  65.     add_comment_meta( $comment_id, 'rating', $rating );
  66. }
  67.  
  68. add_filter( 'preprocess_comment', 'verify_comment_meta_data' );
  69. function verify_comment_meta_data( $commentdata ) {
  70.     if ( ! isset( $_POST['rating'] ) )
  71.     wp_die( __( 'Error: You did not add a rating. Hit the Back button on your Web browser and resubmit your comment with a rating.' ) );
  72.     return $commentdata;
  73. }
  74.  
  75. add_filter( 'comment_text', 'modify_comment');
  76. function modify_comment( $text ){
  77.  
  78.     $plugin_url_path = WP_PLUGIN_URL;
  79.  
  80.     if( $commenttitle = get_comment_meta( get_comment_ID(), 'title', true ) ) {
  81.         $commenttitle = '<strong>' . esc_attr( $commenttitle ) . '</strong><br/>';
  82.         $text = $commenttitle . $text;
  83.     }
  84.  
  85.     if( $commentrating = get_comment_meta( get_comment_ID(), 'rating', true ) ) {
  86.         $commentrating = '<p class="comment-rating">    <img src="'. $plugin_url_path .
  87.         '/ExtendComment/images/'. $commentrating . 'star.gif"/><br/>Rating: <strong>'. $commentrating .' / 5</strong></p>';
  88.         $text = $text . $commentrating;
  89.         return $text;
  90.     } else {
  91.         return $text;
  92.     }
  93. }
  94.  
  95. // Add an edit option to comment editing screen  
  96.  
  97. add_action( 'add_meta_boxes_comment', 'extend_comment_add_meta_box' );
  98. function extend_comment_add_meta_box() {
  99.     add_meta_box( 'title', __( 'Comment Metadata - Extend Comment' ), 'extend_comment_meta_box', 'comment', 'normal', 'high' );
  100. }
  101.  
  102. function extend_comment_meta_box ( $comment ) {
  103.     $phone = get_comment_meta( $comment->comment_ID, 'phone', true );
  104.     $title = get_comment_meta( $comment->comment_ID, 'title', true );
  105.     $rating = get_comment_meta( $comment->comment_ID, 'rating', true );
  106.     wp_nonce_field( 'extend_comment_update', 'extend_comment_update', false );
  107.     ?>
  108.     <p>
  109.         <label for="phone"><?php _e( 'Phone' ); ?></label>
  110.         <input type="text" name="phone" value="<?php echo esc_attr( $phone ); ?>" class="widefat" />
  111.     </p>
  112.     <p>
  113.         <label for="title"><?php _e( 'Comment Title' ); ?></label>
  114.         <input type="text" name="title" value="<?php echo esc_attr( $title ); ?>" class="widefat" />
  115.     </p>
  116.     <p>
  117.         <label for="rating"><?php _e( 'Rating: ' ); ?></label>
  118.             <span class="commentratingbox">
  119.             <?php for( $i=1; $i <= 5; $i++ ) {
  120.                 echo '<span class="commentrating"><input type="radio" name="rating" id="rating" value="'. $i .'"';
  121.                 if ( $rating == $i ) echo ' checked="checked"';
  122.                 echo ' />'. $i .' </span>';
  123.                 }
  124.             ?>
  125.             </span>
  126.     </p>
  127.     <?php
  128. }
  129.  
  130. // Update comment meta data from comment editing screen
  131.  
  132. add_action( 'edit_comment', 'extend_comment_edit_metafields' );
  133.  
  134. function extend_comment_edit_metafields( $comment_id ) {
  135.     if( ! isset( $_POST['extend_comment_update'] ) || ! wp_verify_nonce( $_POST['extend_comment_update'], 'extend_comment_update' ) ) return;
  136.  
  137.     if ( ( isset( $_POST['phone'] ) ) && ( $_POST['phone'] != '') ) :
  138.     $phone = wp_filter_nohtml_kses($_POST['phone']);
  139.     update_comment_meta( $comment_id, 'phone', $phone );
  140.     else :
  141.     delete_comment_meta( $comment_id, 'phone');
  142.     endif;
  143.  
  144.     if ( ( isset( $_POST['title'] ) ) && ( $_POST['title'] != '') ):
  145.     $title = wp_filter_nohtml_kses($_POST['title']);
  146.     update_comment_meta( $comment_id, 'title', $title );
  147.     else :
  148.     delete_comment_meta( $comment_id, 'title');
  149.     endif;
  150.  
  151.     if ( ( isset( $_POST['rating'] ) ) && ( $_POST['rating'] != '') ):
  152.     $rating = wp_filter_nohtml_kses($_POST['rating']);
  153.     update_comment_meta( $comment_id, 'rating', $rating );
  154.     else :
  155.     delete_comment_meta( $comment_id, 'rating');
  156.     endif;
  157.  
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement