How to add an extra field to comment.php

Today I’ve searched the whole internet for how to add an extra field to the comments.php file which is visible in a comment in my admin. I have a comments.php file with a name, e-mail and textfield. But I want to add an extra field for location.

There are lots of tutorials for adjusting the default WordPress comment_form but I am searching for a piece of code for a custom made comment form.

If you can give me a step in the right direction, let me know in the comments please.


Ultimate WordPress comments guide

There are many things you can do with WordPress comments. You can use for example default themes with default settings, Use linear or nested/threaded commenting. You can use social implementation, email verification, spam protection, show your comment form in a popup or add many extra features to optimise it exactly the way you want. You also have many ways to call the functions and show your comments and comment box.

In this guide I will show you all my tips and tricks that I have found during developing my WordPress projects and make one big guide with as much as possible information about WordPress commenting.

Play with WordPress comments

In WordPress comments you can choose for 2 options.

  1. Use default theme comments with save admin control and
  2. Play with your template and functions, to make it your own!

If you chose for the second I will show you how you can do it. (if you have better options? Please comment down)

First you need to make a file named comments.php. And call it with:

<?php comments_template(); ?>

This piece of code says to WordPress: “Take comments.php and show it”.

Then you need a comment template for example:

 * @package WordPress
 * @subpackage Default_Theme

// Do not delete these lines
	if (!empty($_SERVER['SCRIPT_FILENAME']) && 'comments.php' == basename($_SERVER['SCRIPT_FILENAME']))
		die ('Please do not load this page directly. Thanks!');

	if ( post_password_required() ) { ?>
		<p>This post is password protected. Enter the password to view comments.</p>

<!-- You can start editing here. -->

<?php if ( have_comments() ) : ?>
	<h3 id="comments"><?php comments_number('No Responses', 'One Response', '% Responses' );?> to “<?php the_title(); ?>”</h3>

		<?php wp_list_comments('type=comment&callback=advanced_comment'); //this is the important part that ensures we call our custom comment layout defined above 
		<div><?php previous_comments_link() ?></div>
		<div><?php next_comments_link() ?></div>
 <?php else : // this is displayed if there are no comments so far ?>

	<?php if ( comments_open() ) : ?>
		<!-- If comments are open, but there are no comments. -->

	 <?php else : // comments are closed ?>
		<!-- If comments are closed. -->
		<p class="nocomments">Comments are closed.</p>

	<?php endif; ?>
<?php endif; ?>

<?php if ( comments_open() ) : ?>

<div id="respond">

<h3><?php comment_form_title( 'Leave a Reply', 'Leave a Reply to %s' ); ?></h3>

	<small><?php cancel_comment_reply_link(); ?></small>

<?php if ( get_option('comment_registration') && !is_user_logged_in() ) : ?>
<p>You must be <a href="<?php echo wp_login_url( get_permalink() ); ?>">logged in</a> to post a comment.</p>
<?php else : ?>

<form action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" id="commentform">

<?php if ( is_user_logged_in() ) : ?>

<p>Logged in as <a href="<?php echo get_option('siteurl'); ?>/wp-admin/profile.php"><?php echo $user_identity; ?></a>. <a href="<?php echo wp_logout_url(get_permalink()); ?>" title="Log out of this account">Log out &raquo;</a></p>

<?php else : //this is where we setup the comment input forums ?>

<p><input type="text" name="author" id="author" value="<?php echo esc_attr($comment_author); ?>" size="22" tabindex="1" <?php if ($req) echo "aria-required='true'"; ?> />
<label for="author"><small>Name <?php if ($req) echo "(required)"; ?></small></label></p>

<p><input type="text" name="email" id="email" value="<?php echo esc_attr($comment_author_email); ?>" size="22" tabindex="2" <?php if ($req) echo "aria-required='true'"; ?> />
<label for="email"><small>Mail (will not be published) <?php if ($req) echo "(required)"; ?></small></label></p>

<p><input type="text" name="url" id="url" value="<?php echo esc_attr($comment_author_url); ?>" size="22" tabindex="3" />
<label for="url"><small>Website</small></label></p>

<?php endif; ?>

<!--<p><small><strong>XHTML:</strong> You can use these tags: <code><?php echo allowed_tags(); ?></code></small></p>-->

<p><textarea name="comment" id="comment" cols="100%" rows="10" tabindex="4"></textarea></p>

<p><input name="submit" type="submit" id="submit" tabindex="5" value="Post" />
<?php comment_id_fields(); ?>
<?php do_action('comment_form', $post->ID); ?>


<?php endif; // If registration required and not logged in ?>

<?php endif; // if you delete this the sky will fall on your head ?>

In this template are sevaral options, functions and if statements to make it a save and dynamic template. You can expand or minimalise it but I gave this as an example/base template.

If you look closely you see the comments being shown by

<?php wp_list_comments('type=comment&callback=advanced_comment');

In this function you see a “callback” function named “advanced_comment”. With this function we can say in functions.php how to show the comments.

Extra: There are more options for the <?php wp_list_comments(); ?> function:

<?php $args = array(
	'walker'            => null,
	'max_depth'         => '',
	'style'             => 'ul',
	'callback'          => null,
	'end-callback'      => null,
	'type'              => 'all',
	'reply_text'        => 'Reply',
	'page'              => '',
	'per_page'          => '',
	'avatar_size'       => 32,
	'reverse_top_level' => null,
	'reverse_children'  => ''
); ?>

So we make a function in functions.php called: “advanced_comment” and use for example a template like this:

function advanced_comment($comment, $args, $depth) {
   $GLOBALS['comment'] = $comment; ?>

<li <?php comment_class(); ?> id="comment-<?php comment_ID() ?>">
     <?php echo get_avatar($comment,$size='48',$default='<path_to_url>' ); ?>
       <div<a href="<?php the_author_meta( 'user_url'); ?>"><?php printf(__('%s'), get_comment_author_link()) ?></a></div>
       <small><?php printf(__('%1$s at %2$s'), get_comment_date(),  get_comment_time()) ?><?php edit_comment_link(__('(Edit)'),'  ','') ?></small>

     <?php if ($comment->comment_approved == '0') : ?>
       <em><?php _e('Your comment is awaiting moderation.') ?></em>
       <br />
     <?php endif; ?>

         <?php comment_text() ?>

      <?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
<?php } ?>

In this template you see:

  • comment_class(); This helps WordPress giving classes to the comment like: comment, even (or odd), thread-even, depth-1, etc.. so you can easy style them different ways.
  • get_avatar This shows an avatar picture of the author.
  • get_comment_date Gives the date when the comment is placed.
  • edit_comment_link This shows a link where the admin user can edit the comment directly.
  • if($comment->comment_approved == ‘0’) : This checks of the comment is approved and if not it gived the waiting for moderation text.
  • comment_text This shows the content of the comment.
  • comment_reply_link Shows a link for replying.

Important WordPress comment template tags

Template Tag Description
<?php comment_ID(); ?> The ID of a comment
<?php comment_author(); ?> The author of a comment
<?php comment_author_link(); ?> The author of a comment, wrapped with a link to his website if he specified one
<?php comment_type(); ?> The type of comment; pingback, trackback or a comment
<?php comment_text(); ?> The actual comment
<?php comment_date(); ?> The date it was posted
<?php comment_time(); ?> The time it was posted

Decide whether a comment is parent or child

If you want decide whether a comment is parent or child (for example if you want to style it another way) then you can check it by the following code:

WordPress comments custom post type

If you want to use comments in custom post types you first need to add “comments” to your custom post type.

Comment e-mail notification

If you want your visitors notificated after submitting a comment  you can use the following plugins:

Comment e-mail moderation

If you want to do other thing with your comment moderation e-mails you can use the following plugins:

Other Helpful comment plugins

Simple Questions and Answers

How to turn on nested/threaded comments in WordPress admin?

To turn on nested/threaded comment in your WordPress admin you go to settings->Discussion->Other comment settings->Enable threaded (nested) comments X levels deep.


Modify wp_list_comments

To modify the wp_list_comments() function from WordPress. I’ve found the following snippet for your functions.php.

function mytheme_comment($comment, $args, $depth) {
$GLOBALS['comment'] = $comment; ?>
<div class="post">
<strong><?php printf(__(the_title().' your title %s'), get_comment_author_link()) ?></strong>
<?php if ($comment->comment_approved == '0') : ?>
<span class="error"><?php _e('Your comment is awaiting moderation.') ?></span>
<?php endif; ?>
<div class="comment-meta commentmetadata">
<p><?php comment_text() ?></p>
</div><!--End post-->

I’ve used this code to modify the title for a review (comment) from “… says” to “the_title() review from … “.

To call this function i’ve used the “callback” function for WordPress comments as follows:

<?php wp_list_comments('callback=mytheme_comment'); ?>

The WordPress Developer documentatiom from the wp_list_comments function: