Add Custom Comment Fields
This snippet adds custom fields to the comment form, allowing you to collect additional information from commenters.
PHP
function add_comment_fields($fields) {
$fields['twitter'] = '<p class="comment-form-twitter"><label for="twitter">' . __('Twitter Username', 'textdomain') . '</label><input id="twitter" name="twitter" type="text" size="30" /></p>';
return $fields;
}
add_filter('comment_form_default_fields', 'add_comment_fields');
function save_comment_meta($comment_id) {
if (isset($_POST['twitter'])) {
$twitter = sanitize_text_field($_POST['twitter']);
add_comment_meta($comment_id, 'twitter', $twitter);
}
}
add_action('comment_post', 'save_comment_meta');
Snippet Feedback
Did this snippet work for you? Do you have any questions about this snippet? Leave some feedback below.
SHARED BY