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:
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.
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.
Leave a Reply
You must be logged in to post a comment.