Modify Comment Form Default Fields

Recently i was hired by a web designer to provide custom coding for their clients comment form.

The client wanted to change the order of the input fields, remove the website URL field and add 2 custom comment form fields named Title and Industry.

Here’s the result i achieved without hacking the WordPress core files and using 3 custom functions to filter the default output via a child theme:

custom comment form fields

And here’s the code i used to remove the default fields and return the custom fields in the order in the code which also includes 2 small functions to filter 2 of the comment form titles and modify the comment form button text.

Once the custom fields have been generated for the front end, there’s more code needed to save the custom data in the database and display it in different admin pages.

Comments admin

You can also display the data in the comment form on the front end.

Removing Fields

You can use the built in filters to remove fields.

This example removes the website URL field:

add_filter('comment_form_field_url', '__return_false');

This example removes the email field

add_filter('comment_form_field_email', '__return_false');

This example removes the name field

add_filter('comment_form_field_name', '__return_false');

Or you can use this code to unset each field:

add_filter( 'comment_form_default_fields', 'remove_comment_form_fields' );
function remove_comment_form_fields( $fields ) {
    unset($fields['author']);
    unset($fields['email']);
    unset($fields['url']);
return $fields;
}

Note: Removing fields may cause a problem if they are required*.

Notes

Non Genesis users will need to remove the second custom function which includes the genesis_title_comments filter.


Comments

4 responses to “Modify Comment Form Default Fields”

  1. Andrew Hateley-Browne Avatar
    Andrew Hateley-Browne

    I want to do something similar to this, but apply the changes to one category only. I assume I use the if(has category()) function but I haven’t been able to make it work.

  2. Hello Brad,
    Thanks for this useful code!
    If I modify my genesis theme with this code, will it help to enhance the comments with improved SEO feature?
    I have tons of comments in my site and have been looking ways to include a meta box in the comments and in the front end edit, which mightl hopefully help to enhance the existing wordpress comment system with better seo response.
    Kindly clarify this doubt, will greatly appreciate your expert view on this.

    1. I doubt it however comments which add value for the readers of your post might boost your rankings and make the post more popular.

Leave a Reply

Join 5000+ Followers

Get The Latest Free & Premium Tutorials Delivered The Second They’re Published.