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.

Tags

Language

Did it work?

Save Snippet

Embed Snippet

To embed this snippet on your site, copy this html code and paste into your webpage. Learn more
By embedding snippets on your site, you are agreeing to our terms and conditions.

Embed Snippet

To embed this snippet on your site, copy this html code and paste into your webpage. Learn more
By embedding snippets on your site, you are agreeing to our terms and conditions.

Embed Snippet

To embed this snippet on your site, copy this html code and paste into your webpage. Learn more
By embedding snippets on your site, you are agreeing to our terms and conditions.