Advertisement
Guest User

Untitled

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