Custom Fields In WordPress Comment Form

I hope you already seen those extra fields in some of the WordPress powered blogs’ comment box (where you can enter your social profile and that shows up with your comment). Those are the custom fields in WordPress comment form, not a default WordPress functionality.

There are already some plugins available to get that functionality, but if you prefer codes over the plugins (or, if you want to implement the feature in your own theme), you can use this to get started.

Custom Fields in WordPress Comment Form

It is actually a three step process to add custom fields in WordPress comment form, means you first need to add the fields to the form, then validate and save the values with the comment and lastly show the values with the comment. I am going with adding a custom field where people can optionally add their Twitter handle with their name.

1. Add the Custom Fields in WordPress Comment Form

To add the custom fields, we need to create a new function and hook it at comment_form_after_fields and comment_form_logged_in_after action hooks that gets processed with call.

So, here is the function;

function comment_twitter_field() { ?>
	

That function will add the custom fields in WordPress comment form after the last default field in the comment form for guests and after the Name for logged in users. Here’s a screenshot.

custom fields in wordpress comment form

2. Save the Custom Field Value

Now we need to save the value in the comment meta table. We again need a function to do that.

function save_comment_twitter_field( $comment_id ) {
add_comment_meta( $comment_id, 'comment_twitter', $_POST[ 'comment_twitter' ] );
}
add_action( 'comment_post', 'save_comment_twitter_field' );

Simple. Now if someone enters the twitter handle, the data got saved with the comment id for retrieving later.

3. Retrieving the value

To retrieve the value, we can use the get_comment_meta filter within the loop. You can manually place the code within your theme’s template file.

If you prefer not to touch the template files, you can use a function to do that. All you need is ‘extend’ the comment author name by applying a filter to the get_comment_author_link hook.

function show_twitter_beside_author( $author ) {
    $comment_twitter = get_comment_meta( get_comment_ID(), 'comment_twitter', true );
    if ( $comment_twitter )
    $author .= "  → $comment_twitter";
    return $author;
    }
add_filter( 'get_comment_author_link', 'show_twitter_beside_author' );;

The Result

Now when commentators enter their twitter with the other details, it gets saved as the comment meta data and shows up beside their name in their comments. Here is the screenshot:

twitter comments

Get Updates In Your Inbox

Get the WordPress Tips and latst Internet Marketing ideas delivered directly to your email inbox, plus access to our FREE Pro Blogging Model blogging guide.

You need to confirm your email once. Don't worry, we hate SPAMs as much as you do

Comments

  1. Manuel says

    Hi Abhik,

    great tutorial … and a while ago already.
    So I’m hoping you are still checking the comments :-)

    In the beginning you are writung about “validating and saving the values”.
    So which part of the saving step does the validation?
    Isn’t there something missing?

    I would appreciate your answer very much, I’m not so familiar with PHP (but learning :-)) but I will make sure that its not possible to put in malicious stuff into my new fields.

    THANKS!!!
    Manuel

  2. mahyar says

    I added Those Functions But i Dont have Result in Data base

    Meta key has been added but with empty Result

  3. Dov Campbell says

    Thanks for the clear explanation. It helped me a lot.

    I’m wondering how can get the extra field added with the code above to shop up on the Edit Content form. (comment.php?action=editcommen)

    Thanks

  4. Liat says

    Can you upload the code to modify the comment form to allow subscribers like this one:
    “Notify me of new posts by email.”
    ?
    Thanks

    • says

      Something like this will help you to get started:

      < ?php function my_checkbox() { ?>
      /**
      Your Check Boxes goes here
      **/
      
      < ?php }
      add_action('comment_form','my_checkbox');
      ?>
      

Speak Your Mind

Your email address will not be published. Required fields are marked *

You may use these HTML Tags and Attributes

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>