Support » Fixing WordPress » 'Like' feature for comments… not a Facebook plugin. Please feedback!

  • Resolved lacroixca


    Hello all,

    I would like feedback on my work, because I know this is not perfect at all. I got it working, so it’s already fine, but I’m pretty sure there are better way of doing it.

    So what I’ve done is a basic Facebook-like feature which allows anyone to simply click on the Like button, the pages refreshes and the Like button then becomes Like (1). Then Like (2)… You know.

    An example, you will ask? VoilĂ
    Don’t worry, it’s in French, but I try to code in English so I can get help from you guys. Thus, you should actually look for J’aime (1) instead.

    Here’s how I did.

    1. Created a ‘comment_like’ Integer field in ‘wp_comments’ table.
    2. Added in my theme’s ‘functions.php’, the following form:
      <form action="<?php echo home_url( '/wp-content/themes/twentyten/wp-likes-post.php' ); ?>" method="post" class="like-button" name="like-button">
      <input type="hidden" id="liked_comment_ID" name="liked_comment_ID" value="<?php comment_ID(); ?>" />
      <input type="submit" class="like-button" id="submit" name="submit" value="Like<?php $comment_ID = get_comment_ID(); $likes = get_comment($comment_ID)->comment_likes; if ($likes==0) echo ''; else echo ' (' . $likes . ')'; ?>" />
    3. Created the form action file ‘wp-likes-post.php’:
      $liked_comment_ID = $_POST['liked_comment_ID'];
      $db = mysql_connect('localhost', 'LOGIN', 'PASSWORD');
      $sql = "UPDATE wp_comments SET comment_likes = comment_likes +1 WHERE comment_ID = $liked_comment_ID";
      $req = mysql_query($sql) or die('SQL Error!'.$sql.''.mysql_error());
      $ref = $_SERVER['HTTP_REFERER'] . '#comment-' . $liked_comment_ID;
      header( 'refresh: 0; url='.$ref); ?>

    I’m happy that it works, but unsatisfied with my methods.

    Few questions.

    First, how do I use $wpdb to connect to the database and update the field for the specified comment? Please don’t tell me to look at the Codex; I assure you, I did. However, having the form action script in the themes folder doesn’t not seem to recognize the $wpdb… What I mean here is that when I put the script (test version) in the header, it worked, but the same script in my theme’s folder did not.

    So, what exact code should replace the UPDATE mySQL part of my form action script?

    Second, what should I write to get a quick redirect after my script? Here again, I read the wp_redirect() Codex page, but to no avail. The current PHP redirect takes a bit of time to reload… I don’t know anything about AJAX, would someone know the answer on how to do the database update right on the click (no refresh)?

    That’s pretty much it for now. Let me know what you think of it.

    Thank you for your cooperation.


    PS: I’m not good in PHP.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Also, one thing I would love this feature to have, is a ‘Sorry, you need to be registered to Like this comment’.

    People that are not registered would still be able to see, say, Like (2), but wouldn’t be able to click it.

    AND, for those that would be registered and logged in, the script would write in the database, in a comment_like_users_ID table, all the ID numbers of the members who Liked the comment. Very much just like Facebook.

    Thank youuuuuuuu.

    Moderator Ipstenu (Mika Epstein)


    Lead Plugin Wrangler can do it I think.

    Well, I simply don’t want any Facebook thing everywhere. And I want it in my database. So that’s not really what I want.

    The second plugin looks, like you said, a bit overkill.


    I got it working with some AJAX, but now there’s just one little javacript/PHP error.

    So please, forget about the previous post and code. Here’s the new thing.

    • AJAX/Javascript in <head></head>, onclick:
      <script language="javascript" type="text/javascript">
      //Browser Support Code
      function ajaxFunction(liked_comment_ID, like-button-ID){
      var ajaxRequest;  // The variable that makes Ajax possible!
      // Opera 8.0+, Firefox, Safari
      ajaxRequest = new XMLHttpRequest();
      } catch (e){
      // Internet Explorer Browsers
      ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
      ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e){
      // Something went wrong
      alert("Your browser broke!");
      return false;
      // Create a function that will receive data sent from the server
      ajaxRequest.onreadystatechange = function(){
      if(ajaxRequest.readyState == 4){
      var ajaxDisplay = document.getElementById(like-button-ID);
      ajaxDisplay.innerHTML = ajaxRequest.responseText;
      var queryString = "?liked_comment_ID=" + liked_comment_ID;"GET", "<?php echo home_url( '/' ); ?>wp-content/themes/twentyten/getlikes.php" + queryString, true);
    • HTML form, in my theme’s (Twentyten) comment template:
      <input type="button" onclick='ajaxFunction(<?php comment_ID(); ?>, <?php echo "ajaxLikes-"; comment_ID(); ?>)' class="like-button" value="J'aime" />
      <div class="ajaxLikes" id="ajaxLikes-<?php comment_ID(); ?>"> <?php $comment_ID = get_comment_ID(); $likes = get_comment($comment_ID)->comment_likes; if ($likes==0) echo ''; else echo ' (' . $likes . ')'; ?></div>
    • PHP action, activated by AJAX section:
      //ini_set("display_errors", 1);
      //Connect to MySQL Server
      mysql_connect('localhost', 'login', 'password');
      //Select Database
      mysql_select_db('db') or die(mysql_error());
      //Retrieve data from Query String
      $liked_comment_ID = $_GET['liked_comment_ID'];
      //Escape User Input to help prevent SQL Injection
      $liked_comment_ID = mysql_real_escape_string($liked_comment_ID);
      //Build query_update
      $query_update = " UPDATE wp_comments SET comment_likes = comment_likes +1 WHERE comment_ID = '$liked_comment_ID' ";
      //Execute query_update
      //Build query_select
      $query_select = " SELECT comment_likes FROM wp_comments WHERE comment_ID = '$liked_comment_ID' ";
      //Execute query_select
      $query_result_select = mysql_query($query_select);
      //Display query_result_select
      $new_comment_likes = mysql_result($query_result_select, 0);
      echo "(" . $new_comment_likes . ")";

    Right now, when you click on J’aime, it doens’t work. Check it out: BUT, I know for sure that the PHP action DOES WORK when activated. There’s a problem with the AJAX call…

    Here again, what should I do to replace the mysql_connect by $wpdb?



    It WORKS !

    in the HTML form section, when I call the ajaxFunction, the variables should be in between “”.

    onclick="ajaxFunction('<?php comment_ID(); ?>', '<?php echo "ajaxLikes-"; comment_ID(); ?>')"

    It looks goood! Now I need to add the user restriction.

    Found everything I needed.

    BUT I still don’t know how to replace mysql_connect part from my code.

    Can you use $wpdb anywhere? Even in a folder inside the theme’s folder?


    But the rest works!

    I don’t know if I completely understand your question about using $wpdb, but I will try to give you a few directions.

    1. If you are calling a file that does not include any of the logic from WordPress, then you can’t use any of the functions. The solution could be to include wp-load.php in your file (see

    2. If you are still not able to use $wpdb you maybe need to make it global like this

    global $wbdb;
    //your code here

    I hope this can help you

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘'Like' feature for comments… not a Facebook plugin. Please feedback!’ is closed to new replies.