Last active
March 10, 2021 14:18
-
-
Save braddalton/fb4cab21dcf06ac29123 to your computer and use it in GitHub Desktop.
Add custom comment form fields in WordPress ( Part 2 ) https://wpsites.net/web-design/add-remove-change-order-of-comment-form-default-fields/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//* 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; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment