Advertisement
firoze

Add extra field in wp comment form

Jun 23rd, 2015
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.89 KB | None | 0 0
  1. // source link: http://www.smashingmagazine.com/2012/05/08/adding-custom-fields-in-wordpress-comment-form/
  2.  
  3. // plugin link: media.mediatemple.netdna-cdn.com/wp-content/uploads/2012/05/ExtendComment.zip
  4.  
  5.  
  6. // Add extra field in wp comment form like phone, comment title, rating out of 5 star
  7.  
  8. *************************************************************************************************************************
  9.  
  10. // call this code into functions.php
  11.  
  12.  
  13. // add additional form in comment form
  14.  
  15. add_filter('comment_form_default_fields','custom_fields');
  16. function custom_fields($fields) {
  17.  
  18. $commenter = wp_get_current_commenter();
  19. $req = get_option( 'require_name_email' );
  20. $aria_req = ( $req ? " aria-required='true'" : '' );
  21.  
  22. $fields[ 'author' ] = '<p class="comment-form-author">'.
  23. '<label for="author">' . __( 'Name' ) . '</label>'.
  24. ( $req ? '<span class="required">*</span>' : '' ).
  25. '<input id="author" name="author" type="text" value="'. esc_attr( $commenter['comment_author'] ) .
  26. '" size="30" tabindex="1"' . $aria_req . ' /></p>';
  27.  
  28. $fields[ 'email' ] = '<p class="comment-form-email">'.
  29. '<label for="email">' . __( 'Email' ) . '</label>'.
  30. ( $req ? '<span class="required">*</span>' : '' ).
  31. '<input id="email" name="email" type="text" value="'. esc_attr( $commenter['comment_author_email'] ) .
  32. '" size="30" tabindex="2"' . $aria_req . ' /></p>';
  33.  
  34. $fields[ 'url' ] = '<p class="comment-form-url">'.
  35. '<label for="url">' . __( 'Website' ) . '</label>'.
  36. '<input id="url" name="url" type="text" value="'. esc_attr( $commenter['comment_author_url'] ) .
  37. '" size="30" tabindex="3" /></p>';
  38.  
  39. $fields[ 'phone' ] = '<p class="comment-form-phone">'.
  40. '<label for="phone">' . __( 'Phone' ) . '</label>'.
  41. '<input id="phone" name="phone" type="text" size="30" tabindex="4" /></p>';
  42.  
  43. return $fields;
  44. }
  45.  
  46. // Add fields after default fields above the comment box, always visible
  47.  
  48. add_action( 'comment_form_logged_in_after', 'additional_fields' );
  49. add_action( 'comment_form_after_fields', 'additional_fields' );
  50.  
  51. function additional_fields () {
  52. echo '<p class="comment-form-title">'.
  53. '<label for="title">' . __( 'Comment Title' ) . '</label>'.
  54. '<input id="title" name="title" type="text" size="30" tabindex="5" /></p>';
  55.  
  56. echo '<p class="comment-form-rating">'.
  57. '<label for="rating">'. __('Rating') . '<span class="required">*</span></label>
  58. <span class="commentratingbox">';
  59.  
  60. for( $i=1; $i <= 5; $i++ )
  61. echo '<span class="commentrating"><input type="radio" name="rating" id="rating" value="'. $i .'"/>'. $i .'</span>';
  62.  
  63. echo'</span></p>';
  64.  
  65. }
  66.  
  67. // Save the comment meta data along with comment
  68.  
  69. add_action( 'comment_post', 'save_comment_meta_data' );
  70. function save_comment_meta_data( $comment_id ) {
  71. if ( ( isset( $_POST['phone'] ) ) && ( $_POST['phone'] != '') )
  72. $phone = wp_filter_nohtml_kses($_POST['phone']);
  73. add_comment_meta( $comment_id, 'phone', $phone );
  74.  
  75. if ( ( isset( $_POST['title'] ) ) && ( $_POST['title'] != '') )
  76. $title = wp_filter_nohtml_kses($_POST['title']);
  77. add_comment_meta( $comment_id, 'title', $title );
  78.  
  79. if ( ( isset( $_POST['rating'] ) ) && ( $_POST['rating'] != '') )
  80. $rating = wp_filter_nohtml_kses($_POST['rating']);
  81. add_comment_meta( $comment_id, 'rating', $rating );
  82. }
  83.  
  84.  
  85. // Add the filter to check if the comment meta data has been filled or not
  86.  
  87. add_filter( 'preprocess_comment', 'verify_comment_meta_data' );
  88. function verify_comment_meta_data( $commentdata ) {
  89. if ( ! isset( $_POST['rating'] ) )
  90. wp_die( __( 'Error: You did not add your rating. Hit the BACK button of your Web browser and resubmit your comment with rating.' ) );
  91. return $commentdata;
  92. }
  93.  
  94. //Add an edit option in comment edit screen
  95.  
  96. add_action( 'add_meta_boxes_comment', 'extend_comment_add_meta_box' );
  97. function extend_comment_add_meta_box() {
  98. add_meta_box( 'title', __( 'Comment Metadata - Extend Comment' ), 'extend_comment_meta_box', 'comment', 'normal', 'high' );
  99. }
  100.  
  101. function extend_comment_meta_box ( $comment ) {
  102. $phone = get_comment_meta( $comment->comment_ID, 'phone', true );
  103. $title = get_comment_meta( $comment->comment_ID, 'title', true );
  104. $rating = get_comment_meta( $comment->comment_ID, 'rating', true );
  105. wp_nonce_field( 'extend_comment_update', 'extend_comment_update', false );
  106. ?>
  107. <p>
  108. <label for="phone"><?php _e( 'Phone' ); ?></label>
  109. <input type="text" name="phone" value="<?php echo esc_attr( $phone ); ?>" class="widefat" />
  110. </p>
  111. <p>
  112. <label for="title"><?php _e( 'Comment Title' ); ?></label>
  113. <input type="text" name="title" value="<?php echo esc_attr( $title ); ?>" class="widefat" />
  114. </p>
  115. <p>
  116. <label for="rating"><?php _e( 'Rating: ' ); ?></label>
  117. <span class="commentratingbox">
  118. <?php for( $i=1; $i <= 5; $i++ ) {
  119. echo '<span class="commentrating"><input type="radio" name="rating" id="rating" value="'. $i .'"';
  120. if ( $rating == $i ) echo ' checked="checked"';
  121. echo ' />'. $i .' </span>';
  122. }
  123. ?>
  124. </span>
  125. </p>
  126. <?php
  127. }
  128.  
  129. // Update comment meta data from comment edit screen
  130.  
  131. add_action( 'edit_comment', 'extend_comment_edit_metafields' );
  132. function extend_comment_edit_metafields( $comment_id ) {
  133. if( ! isset( $_POST['extend_comment_update'] ) || ! wp_verify_nonce( $_POST['extend_comment_update'], 'extend_comment_update' ) ) return;
  134.  
  135. if ( ( isset( $_POST['phone'] ) ) && ( $_POST['phone'] != '') ) :
  136. $phone = wp_filter_nohtml_kses($_POST['phone']);
  137. update_comment_meta( $comment_id, 'phone', $phone );
  138. else :
  139. delete_comment_meta( $comment_id, 'phone');
  140. endif;
  141.  
  142. if ( ( isset( $_POST['title'] ) ) && ( $_POST['title'] != '') ):
  143. $title = wp_filter_nohtml_kses($_POST['title']);
  144. update_comment_meta( $comment_id, 'title', $title );
  145. else :
  146. delete_comment_meta( $comment_id, 'title');
  147. endif;
  148.  
  149. if ( ( isset( $_POST['rating'] ) ) && ( $_POST['rating'] != '') ):
  150. $rating = wp_filter_nohtml_kses($_POST['rating']);
  151. update_comment_meta( $comment_id, 'rating', $rating );
  152. else :
  153. delete_comment_meta( $comment_id, 'rating');
  154. endif;
  155.  
  156. }
  157.  
  158. // Add the comment meta (saved earlier) to the comment text
  159. // You can also output the comment meta values directly in comments template
  160.  
  161. add_filter( 'comment_text', 'modify_comment');
  162. function modify_comment( $text ){
  163.  
  164. $plugin_url_path = WP_PLUGIN_URL;
  165.  
  166. if( $commenttitle = get_comment_meta( get_comment_ID(), 'title', true ) ) {
  167. $commenttitle = '<strong>' . esc_attr( $commenttitle ) . '</strong><br/>';
  168. $text = $commenttitle . $text;
  169. }
  170.  
  171. if( $commentrating = get_comment_meta( get_comment_ID(), 'rating', true ) ) {
  172. $commentrating = '<p class="comment-rating"> <img src="'. $plugin_url_path .
  173. '/ExtendComment/images/'. $commentrating . 'star.gif"/><br/>Rating: <strong>'. $commentrating .' / 5</strong></p>';
  174. $text = $text . $commentrating;
  175. return $text;
  176. } else {
  177. return $text;
  178. }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement