1. function dp_recent_comments_ajax() {
  2. $no_comments = 5;
  3. $comment_len = 300;
  4. global $wpdb;
  5.  
  6. $request = "SELECT * FROM $wpdb->comments";
  7. $request .= " JOIN $wpdb->posts ON ID = comment_post_ID";
  8. $request .= " WHERE comment_approved = '1' AND post_status = 'publish' AND post_password =''";
  9. $request .= " ORDER BY comment_date DESC LIMIT $no_comments";
  10.  
  11. $comments = $wpdb->get_results($request);
  12. if ($comments) {
  13. foreach ($comments as $comment) {
  14. ob_start();
  15. ?>
  16. <li>
  17. <a style="font-size:12px "href="<?php echo get_permalink( $comment->comment_post_ID ) . '#comment-' . $comment->comment_ID; ?>"><?php echo dp_get_author($comment); ?> on <?php echo $comment->comment_date; ?> :</a><br />
  18. <span class="recent-comment-text" style="font-size:11px;font-style:italic"><?php echo strip_tags(substr(apply_filters('get_comment_text', $comment->comment_content), 0, $comment_len)); ?>..</span>
  19. </li>
  20. <?php
  21. ob_end_flush();
  22. }
  23. } else {
  24. echo '<li>'.__('No comments', 'my-first-wp-theme').'</li>';
  25. }
  26. die(); // this is needed for ajax to work properly
  27. }
  28. function dp_get_author($comment) {
  29. $author = "";
  30.  
  31. if ( empty($comment->comment_author) )
  32. $author = __('Anonymous', 'my-first-wp-theme');
  33. else
  34. $author = $comment->comment_author;
  35.  
  36. return $author;
  37. }
  38. add_action('wp_ajax_my_recent_comments', 'dp_recent_comments_ajax');
  39. add_action('wp_ajax_nopriv_my_recent_comments', 'dp_recent_comments_ajax'); //for users that are not logged in