Thêm, xoá trường trong comment của wordpress

Cập nhật lần cuối 03/12/2014 by trong WordPress vào 03/12/2014 có 474 Views

Thêm xoá trường comments wordpress

Code dưới đây sẽ giúp các bạn thêm hoặc xoá trường trong phần comment của wordpress.

Thêm trường vào comment

Thêm đoạn code sau vào file functions.php trong theme của bạn.


add_filter( 'comment_form_defaults', 'wpsites_comment_form_defaults' );
function wpsites_comment_form_defaults( $defaults ) {

 $defaults['title_reply'] = __( 'Add Your Comment' );
 $defaults['label_submit'] = __( 'Submit Comment', 'custom' );
 return $defaults;
}
/**
 * @author Brad Dalton
 * @example http://wpsites.net/web-design/add-remove-change-order-of-comment-form-default-fields/
 * @copyright 2014 WP Sites
 */

add_filter( 'genesis_title_comments', 'wpsites_title_comments' );
function wpsites_title_comments() {
 $title = '<h3>Your Stories</h3>';
 return $title;
}

add_filter( 'comment_form_default_fields', 'wpsites_comment_form_fields' );

function wpsites_comment_form_fields( $fields ) {

unset($fields['author']);
 unset($fields['email']);
 unset($fields['url']);

$fields['title'] = '<p class="comment-form-title">' . '<label for="title">' . __( 'Title' ) . ( $req ? ' <span class="required">*</span>' : '' ) . '</label> ' .
 '<input id="title" name="title" type="text" value="' . esc_attr( $commenter['comment_title'] ) . '" size="30"' . $aria_req . ' /></p>';

 $fields['industry'] = '<p class="comment-form-industry"><label for="industry">' . __( 'Industry' ) . ( $req ? ' <span class="required">*</span>' : '' ) . '</label> ' .
 '<input id="industry" name="industry" ' . ( $html5 ? 'type="industry"' : 'type="text"' ) . ' value="' . esc_attr( $commenter['comment_author_industry'] ) . '" size="30"' . $aria_req . ' /></p>';

$fields['author'] = '<p class="comment-form-author">' . '<label for="author">' . __( 'Name' ) . ( $req ? ' <span class="required">*</span>' : '' ) . '</label> ' .
 '<input id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30"' . $aria_req . ' /></p>';

 $fields['email'] = '<p class="comment-form-email"><label for="email">' . __( 'Email' ) . ( $req ? ' <span class="required">*</span>' : '' ) . '</label> ' .
 '<input id="email" name="email" ' . ( $html5 ? 'type="email"' : 'type="text"' ) . ' value="' . esc_attr( $commenter['comment_author_email'] ) . '" size="30"' . $aria_req . ' /></p>';

return $fields;
}

Sau khi thêm ta được kết quả như sau:

Thêm trường trong trang admin

Thêm đoạn code này vào file functions.php. Ngay sau đoạn code phía trên là ok

//* Save Custom Comment Form Field Meta Data
add_action( 'comment_post', 'save_custom_comment_field_data', 10, 1 );
function save_custom_comment_field_data( $comment_id ) {
 if ( ( isset( $_POST['industry'] ) ) && ( $_POST['industry'] != '') )
 $industry = wp_filter_nohtml_kses($_POST['industry']);
 add_comment_meta( $comment_id, 'industry', $industry );

if ( ( isset( $_POST['title'] ) ) && ( $_POST['title'] != '') )
 $title = wp_filter_nohtml_kses($_POST['title']);
 add_comment_meta( $comment_id, 'title', $title );

}
/**
 * @author Brad Dalton
 * @example http://wpsites.net/web-design/add-remove-change-order-of-comment-form-default-fields/
 * @copyright 2014 WP Sites
 */
//* Add Custom Meta Boxes On Edit Comment Screen
add_action( 'add_meta_boxes_comment', 'wpsites_add_custom_comment_field_meta_boxes' );
function wpsites_add_custom_comment_field_meta_boxes() {
 add_meta_box( 'title', __( 'Title' ), 'wpsites_custom_comment_title_field_meta_box', 'comment', 'normal', 'high' );
 add_meta_box( 'industry', __( 'Industry' ), 'wpsites_custom_comment_industry_field_meta_box', 'comment', 'normal', 'high' );
}

function wpsites_custom_comment_title_field_meta_box( $comment ) {
 $title = get_comment_meta( $comment->comment_ID, 'wpsites_title_comment_field_data', true );
 wp_nonce_field( 'update_comment_title', 'update_comment_title', false );
 ?>
 <p>
 <label for="title"><?php _e( 'Title' ); ?></label>
 <input type="text" name="title" value="<?php echo esc_attr( $title ); ?>" class="widefat" />
 </p>
 <?php
}

function wpsites_custom_comment_industry_field_meta_box( $comment ) {
 $industry = get_comment_meta( $comment->comment_ID, 'wpsites_industry_comment_field_data', true );
 wp_nonce_field( 'update_comment_industry', 'update_comment_industry', false );
 ?>
 <p>
 <label for="industry"><?php _e( 'Industry' ); ?></label>
 <input type="text" name="industry" value="<?php echo esc_attr( $industry ); ?>" class="form-table editcomment" />
 </p>
 <?php
}
add_action( 'edit_comment', 'update_edit_comment' );
function update_edit_comment( $comment_id ) {
 if( ! isset( $_POST['update_comment_title'] ) || ! wp_verify_nonce( $_POST['update_comment_title'], 'update_comment_title' ) ) return;
 if( isset( $_POST['title'] ) )
 update_comment_meta( $comment_id, 'title', esc_attr( $_POST['title'] ) );
 if( ! isset( $_POST['update_comment_industry'] ) || ! wp_verify_nonce( $_POST['update_comment_industry'], 'update_comment_industry' ) ) return;
 if( isset( $_POST['title'] ) )
 update_comment_meta( $comment_id, 'industry', esc_attr( $_POST['industry'] ) );
}

add_action('load-edit-comments.php', 'add_custom_fields_to_edit_comment_screen');
function add_custom_fields_to_edit_comment_screen() {
 $screen = get_current_screen();

add_filter("manage_{$screen->id}_columns", 'add_custom_comment_columns');
}

function add_custom_comment_columns($cols) {
 $cols['industry'] = __('Industry', 'wpsites');
 $cols['title'] = __('Title', 'wpsites');
 return $cols;
}

add_action( 'manage_comments_custom_column', 'custom_title_column', 10, 2 );
function custom_title_column($col, $comment_id) {

 switch($col) {
 case 'title':
 if($tit = get_comment_meta($comment_id, 'title', true)){
 echo esc_html($tit);
 } else {
 esc_html_e('No Title Submitted', 'wpsites');
 }
 }
}

add_action( 'manage_comments_custom_column', 'custom_industry_column', 10, 2 );
function custom_industry_column($col, $comment_id) {

 switch($col) {
 case 'industry':
 if($ind = get_comment_meta($comment_id, 'industry', true)){
 echo esc_html($ind);
 } else {
 esc_html_e('No Industry Submitted', 'wpsites');
 }
 }
}

//* Output Custom Comment Field Data On Comment Form Front End
add_filter( 'comment_text', 'output_title_field_data_comment_form');

function output_title_field_data_comment_form( $text ){
 if( $title = get_comment_meta( get_comment_ID(), 'title', true ) ) {
 $title = '<strong>' . esc_attr( $title ) . '</strong><br/>';
 $text = $title . $text;
 return $text;
 }
 }

add_filter( 'comment_text', 'output_industry_field_data_comment_form');
function output_industry_field_data_comment_form( $text ){
 if( $industry = get_comment_meta( get_comment_ID(), 'industry', true ) ) {
 $industry = '<strong>' . esc_attr( $industry ) . '</strong><br/>';
 $text = $industry . $text;
 return $text;
 }
 }

Chúc các bạn thành công!

Xem thêm: http://wpsites.net/web-design/add-remove-change-order-of-comment-form-default-fields/

Thêm, xoá trường trong comment của wordpress
5 (100%) 2 votes
  • Bình luận
Có thể bạn quan tâm
x