Storing a Twitter username with comments in WordPress

Follow me on TwitterWe’ve all seen this before when commenting on a blog post we’ve just read. The standard comment form on a WordPress-driven website asks for a user’s name, email address (not published), website address and their comment. What if we could get some other information from the user*, and later integrate that into their comment? Why not get their Twitter username and link back to their Twitter profile as well as to their website? This tutorial will explain how to do just that.

* While this tutorial uses a Twitter username as an example, virtually any additional information supplied by the user can be stored along with their comment (a rating, a selection of their social media profiles, etc).

Asking for it by name (aka. creating the Twitter username textfield)

Here we’ll add a field to the comment form, in which the user can input their Twitter username. The snippet of code I’ve used for this is as follows:

<p><label for="twitter">Twitter</label><input type="text" name="twitter" id="twitter" value="" /></p>

The above code adds a field to the comment form of the theme in question. I’d recommend making a copy of the way the website field is generated and replacing all instances of the word “website” with the word “twitter”. This is an easy way to ensure consistency with the theme to which you are adding this functionality.

Okay, lets store this thing!

Right. The user has entered their Twitter username. Lets store it along with the comment, using the comment meta functionality provided by WordPress.

What we’ll be doing here is the following:

  1. Check if the user inputted a Twitter username.
  2. Sanitize the username.
  3. If there are any characters left after we’ve sanitised the username (it removes all unwanted characters), store the Twitter username along with the comment.

And here’s how that looks in code:

<?php
/**
* Storing the commenter's Twitter username.
*/
function matty_store_twitter_username ( $post_id ) {
$twitter_username = $_POST['twitter'];
if ( $twitter_username ) {
$twitter_username = sanitize_user( $twitter_username, true );
$twitter_username = str_replace( ' ', '', $twitter_username );
$twitter_username = str_replace( '.', '', $twitter_username );
} // End IF Statement
if ( $twitter_username ) {
add_comment_meta( $post_id, 'twitter', $twitter_username, true );
} // End IF Statement
} // End matty_store_twitter_username()
add_action( 'comment_post', 'matty_store_twitter_username', 1 );
?>

Pretty simple, right?

If you make use of the above or know of other ways of achieving this, please share your thoughts in the comments below.

In a follow-up tutorial, I’ll discuss how to integrate this stored Twitter username into comments on your WordPress website, using a custom comment callback function.

2010-05-03: Code updated thank to feedback from Ben in the comments below. Thanks man. πŸ™‚

WordPress tips in your inbox ✨

More WordPress thoughts and tips, right in your inbox.

We don’t spam! Read our privacy policy for more info.


Comments

15 responses to “Storing a Twitter username with comments in WordPress”

  1. Wow – I just finished writing almost exactly the same tutorial on my own website. I’ve scheduled it to publish on Thursday though, and my code is a little different.

    One thing you missed is that you’re not assigning sanitize_user to twitter_username so anything done to the username won’t be stored. Also the sanitize_user function is for WordPress usernames, and the Twitter usernames may have different rules (I don’t know if this is true or not).

    Otherwise it’s a good start. You should add it to your site so people can see it in action πŸ™‚

    1. Hey Ben,
      Thanks for your comment and for spotting that bug in the code with sanitize_user(). I’ll update this as soon as possible.

      In addition, I would most likely also remove all space and full-stops from the username, as this would conflict with the URL, being the main use of the URL in this context.

      Thanks again for your comment. I look forward to reading your tutorial on Thursday. πŸ™‚

      Cheers,
      Matty.

  2. I’m struggling with this, haven’t gotten it to work yet, and this is not the first tutorial on this matter that I’ve attempted.
    As a sanity check I’ve even implemented your tutorial exactly as you say, still doesn’t work.

    Do you have to add the row in mysql table wp_comments manually? I’ve tried your demo both with and without adding the row, neither works.

    What else could I be missing? The form submits fine, but when I look at the row in mysql, the twitter data is not populated.

    1. Hi Dan,
      Do comments themselves get stored, without the metadata?

      If this is the case, are you working in version 2.9 or later of WordPress? If not, the comment metadata feature was added to WordPress in version 2.9, thus, this tutorial is geared to version 2.9 and onward.

      Please let me know if this solves the issue. πŸ™‚

      All the best,
      Matty.

  3. I’m using wordpress 2.9.2. yes, comments are being stored, dunno which fields constitute metadata, date, web site? if those are metadata fields, then yes, those are being captured too. The only data not being captured is the new field I’m trying to add (in this case, twitter)

    1. Hi Dan,
      By metadata, I was referring to the Twitter username.

      I’ll take a look at the snippets again and see if I can spot why the Twitter username isn’t being stored. πŸ™‚

      Cheers,
      Matty.

  4. in what table/field is this supposed to get stored? Does the field need to be created in mysql manually?

    1. Hi Dan,
      The information is stored in the wp_commentmeta table (wp is the table prefix that was set at installation… default is wp). The information is stored as a key, value and the ID of the comment to which the metadata belongs.

      The wp_commentmeta table should be available to you in 2.9.2. I’ll open the snippet and take a look to see if there are any bugs. πŸ™‚

      Cheers,
      Matty.

    2. Hi Dan,
      I’ve copied the code, as it is in the tutorial, and all looks to be working correctly.

      This tutorial is just to get the information into the database. If the problem persists, please let me know. πŸ™‚

      Cheers,
      Matty.

  5. Thanks for your help. I’ve figure it out, I was looking in the wrong table for the data! I was looking in wp_comments instead of wp_commentsmeta. thanks again, great tutorial!

    1. Thanks Dan. I really appreciate your kind words. πŸ™‚

  6. Running through it now…fingers crossed.

    if($matty_tutorial = TRUE) {
    echo “Dude! Yes. Thank you.”;
    } else {
    echo $questions_array[0];
    }

    #nerdiestComment #ever

    1. Lol… thanks JoE. πŸ™‚

      How did your run-through go?

  7. Thanks! Could you please tell us, how these variables can be retrieved from DB and maybe how to modify them through WP admin panel? Again thanks πŸ™‚

  8. Hi, I’ve used the code and the data are in commentmeta right now but i can’t print it in comment page. I am using the code:

    $comment_email = get_comment_meta($comment_ID,’_comment_email’,true);
    echo $comment_email;

    $comment_name = get_comment_meta($comment_ID,’_comment_name’,true);
    echo $comment_name;

    Nothing will show up on page.
    why?
    Could you please help me?

Leave a Reply

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