How To Add Custom Fields for Comment Form in WordPress
We need to add more fields for the comment form. we are adding for “phone number” field. this code for creating a text box for the comment form.
1. Add field for comment form.
add_filter('comment_form_default_fields', 'custom_fields'); function custom_fields($fields) { $commenter = wp_get_current_commenter(); $req = get_option( 'require_name_email' ); $aria_req = ( $req ? " aria-required='true'" : ’ ); $fields[ 'phone' ] = '<p class="comment-form-phone">'. '<label for="phone">' . __( 'Phone' ) . '</label>'. '<input id="phone" name="phone" type="text" size="30" tabindex="4" /></p>'; return $fields; }
2. Save the comment meta data along with comment.
add_action( 'comment_post', 'save_comment_meta_data' ); function save_comment_meta_data( $comment_id ) { if ( ( isset( $_POST['phone'] ) ) && ( $_POST['phone'] != ’) ) $phone = wp_filter_nohtml_kses($_POST['phone']); add_comment_meta( $comment_id, 'phone', $phone ); }
3 Add an edit option to comment editing screen in admin panel
add_action( 'add_meta_boxes_comment', 'extend_comment_add_meta_box' ); function extend_comment_add_meta_box() { add_meta_box( 'title', __( 'Comment Metadata - Extend Comment' ), 'extend_comment_meta_box', 'comment', 'normal', 'high' ); } function extend_comment_meta_box ( $comment ) { $phone = get_comment_meta( $comment->comment_ID, 'phone', true ); $title = get_comment_meta( $comment->comment_ID, 'title', true ); $rating = get_comment_meta( $comment->comment_ID, 'rating', true ); wp_nonce_field( 'extend_comment_update', 'extend_comment_update', false ); ?> <p> <label for="phone"><?php _e( 'Phone' ); ?></label> <input type="text" name="phone" value="<?php echo esc_attr( $phone ); ?>" class="widefat" /> </p> <?php }
5. Update comment meta data from comment editing screen in admin panel
add_action( 'edit_comment', 'extend_comment_edit_metafields' ); function extend_comment_edit_metafields( $comment_id ) { if( ! isset( $_POST['extend_comment_update'] ) || ! wp_verify_nonce( $_POST['extend_comment_update'], 'extend_comment_update' ) ) return; if ( ( isset( $_POST['phone'] ) ) && ( $_POST['phone'] != ’) ) : $phone = wp_filter_nohtml_kses($_POST['phone']); update_comment_meta( $comment_id, 'phone', $phone ); else : delete_comment_meta( $comment_id, 'phone'); endif; }
Author: Learn Tutorials Point
We Well organized and easy-to-understand Interactive tutorials With lots of examples of how to use Tutorials WordPress, PHP, Bootstrap, Bootstrap 4, HTML, CSS, AJAX, jQuery, Woocommerce, Post, Database, Javascript, Theme, Responsive, Templates.
Trending Posts
Tags
ACF
Ading custom columns
admin
Bootstrap
Cart Price
categories
Change Price
class
Code
comments
Create
Creating custom post Type
CSS
current
custom
Customizing
Custom Post
Custom Post Type
Custom Taxonomy
database
Date
Fatch
form
function
HTML
Image
Install
javascript
jQuery
Menu
PHP
Post
Post Type
Price
product
responsive
Resular price
Single product
Table
taxonomy
Theme
Title
woocommerce
Wordpress
Wordpress Code
I like the hеlpful information you provide in your articles.
I’ll bookmark your blog and checк again here regularlү.
I am quite certain I will learn lots of new stuff
right heгe! Good luck for the next!
Thank you.
I recommend to you to look in google.com
In my opinion you are not right. I can prove it. Write to me in PM, we will talk.
Hi, unfortunately, I faced challenges with the slow loading speed of your website, leading to frustration. I recommend a service, linked below, that I’ve used personally to significantly improve my website speed. I really love your website…