3 * Comment template functions
5 * These functions are meant to live inside of the WordPress loop.
12 * Retrieve the author of the current comment.
14 * If the comment has an empty comment_author field, then 'Anonymous' person is
18 * @uses apply_filters() Calls 'get_comment_author' hook on the comment author
20 * @return string The comment author
22 function get_comment_author() {
24 if ( empty($comment->comment_author) ) {
25 if (!empty($comment->user_id)){
26 $user=get_userdata($comment->user_id);
27 $author=$user->user_login;
29 $author = __('Anonymous');
32 $author = $comment->comment_author;
34 return apply_filters('get_comment_author', $author);
38 * Displays the author of the current comment.
41 * @uses apply_filters() Calls 'comment_author' on comment author before displaying
43 function comment_author() {
44 $author = apply_filters('comment_author', get_comment_author() );
49 * Retrieve the email of the author of the current comment.
52 * @uses apply_filters() Calls the 'get_comment_author_email' hook on the comment author email
55 * @return string The current comment author's email
57 function get_comment_author_email() {
59 return apply_filters('get_comment_author_email', $comment->comment_author_email);
63 * Display the email of the author of the current global $comment.
65 * Care should be taken to protect the email address and assure that email
66 * harvesters do not capture your commentors' email address. Most assume that
67 * their email address will not appear in raw form on the blog. Doing so will
68 * enable anyone, including those that people don't want to get the email
69 * address and use it for their own means good and bad.
72 * @uses apply_filters() Calls 'author_email' hook on the author email
74 function comment_author_email() {
75 echo apply_filters('author_email', get_comment_author_email() );
79 * Display the html email link to the author of the current comment.
81 * Care should be taken to protect the email address and assure that email
82 * harvesters do not capture your commentors' email address. Most assume that
83 * their email address will not appear in raw form on the blog. Doing so will
84 * enable anyone, including those that people don't want to get the email
85 * address and use it for their own means good and bad.
88 * @uses apply_filters() Calls 'comment_email' hook for the display of the comment author's email
89 * @uses get_comment_author_email_link() For generating the link
90 * @global object $comment The current Comment row object
92 * @param string $linktext The text to display instead of the comment author's email address
93 * @param string $before The text or HTML to display before the email link.
94 * @param string $after The text or HTML to display after the email link.
96 function comment_author_email_link($linktext='', $before='', $after='') {
97 if ( $link = get_comment_author_email_link( $linktext, $before, $after ) )
102 * Return the html email link to the author of the current comment.
104 * Care should be taken to protect the email address and assure that email
105 * harvesters do not capture your commentors' email address. Most assume that
106 * their email address will not appear in raw form on the blog. Doing so will
107 * enable anyone, including those that people don't want to get the email
108 * address and use it for their own means good and bad.
111 * @uses apply_filters() Calls 'comment_email' hook for the display of the comment author's email
112 * @global object $comment The current Comment row object
114 * @param string $linktext The text to display instead of the comment author's email address
115 * @param string $before The text or HTML to display before the email link.
116 * @param string $after The text or HTML to display after the email link.
118 function get_comment_author_email_link($linktext='', $before='', $after='') {
120 $email = apply_filters('comment_email', $comment->comment_author_email);
121 if ((!empty($email)) && ($email != '@')) {
122 $display = ($linktext != '') ? $linktext : $email;
124 $return .= "<a href='mailto:$email'>$display</a>";
133 * Retrieve the html link to the url of the author of the current comment.
136 * @uses apply_filters() Calls 'get_comment_author_link' hook on the complete link HTML or author
138 * @return string Comment Author name or HTML link for author's URL
140 function get_comment_author_link() {
141 /** @todo Only call these functions when they are needed. Include in if... else blocks */
142 $url = get_comment_author_url();
143 $author = get_comment_author();
145 if ( empty( $url ) || 'http://' == $url )
148 $return = "<a href='$url' rel='external nofollow' class='url'>$author</a>";
149 return apply_filters('get_comment_author_link', $return);
153 * Display the html link to the url of the author of the current comment.
156 * @see get_comment_author_link() Echos result
158 function comment_author_link() {
159 echo get_comment_author_link();
163 * Retrieve the IP address of the author of the current comment.
167 * @uses apply_filters()
171 function get_comment_author_IP() {
173 return apply_filters('get_comment_author_IP', $comment->comment_author_IP);
177 * Display the IP address of the author of the current comment.
180 * @see get_comment_author_IP() Echos Result
182 function comment_author_IP() {
183 echo get_comment_author_IP();
187 * Retrieve the url of the author of the current comment.
190 * @uses apply_filters() Calls 'get_comment_author_url' hook on the comment author's URL
194 function get_comment_author_url() {
196 $url = ('http://' == $comment->comment_author_url) ? '' : $comment->comment_author_url;
197 $url = esc_url( $url, array('http', 'https') );
198 return apply_filters('get_comment_author_url', $url);
202 * Display the url of the author of the current comment.
205 * @uses apply_filters()
206 * @uses get_comment_author_url() Retrieves the comment author's URL
208 function comment_author_url() {
209 echo apply_filters('comment_url', get_comment_author_url());
213 * Retrieves the HTML link of the url of the author of the current comment.
215 * $linktext parameter is only used if the URL does not exist for the comment
216 * author. If the URL does exist then the URL will be used and the $linktext
219 * Encapsulate the HTML link between the $before and $after. So it will appear
220 * in the order of $before, link, and finally $after.
223 * @uses apply_filters() Calls the 'get_comment_author_url_link' on the complete HTML before returning.
225 * @param string $linktext The text to display instead of the comment author's email address
226 * @param string $before The text or HTML to display before the email link.
227 * @param string $after The text or HTML to display after the email link.
228 * @return string The HTML link between the $before and $after parameters
230 function get_comment_author_url_link( $linktext = '', $before = '', $after = '' ) {
231 $url = get_comment_author_url();
232 $display = ($linktext != '') ? $linktext : $url;
233 $display = str_replace( 'http://www.', '', $display );
234 $display = str_replace( 'http://', '', $display );
235 if ( '/' == substr($display, -1) )
236 $display = substr($display, 0, -1);
237 $return = "$before<a href='$url' rel='external'>$display</a>$after";
238 return apply_filters('get_comment_author_url_link', $return);
242 * Displays the HTML link of the url of the author of the current comment.
245 * @see get_comment_author_url_link() Echos result
247 * @param string $linktext The text to display instead of the comment author's email address
248 * @param string $before The text or HTML to display before the email link.
249 * @param string $after The text or HTML to display after the email link.
251 function comment_author_url_link( $linktext = '', $before = '', $after = '' ) {
252 echo get_comment_author_url_link( $linktext, $before, $after );
256 * Generates semantic classes for each comment element
260 * @param string|array $class One or more classes to add to the class list
261 * @param int $comment_id An optional comment ID
262 * @param int $post_id An optional post ID
263 * @param bool $echo Whether comment_class should echo or return
265 function comment_class( $class = '', $comment_id = null, $post_id = null, $echo = true ) {
266 // Separates classes with a single space, collates classes for comment DIV
267 $class = 'class="' . join( ' ', get_comment_class( $class, $comment_id, $post_id ) ) . '"';
275 * Returns the classes for the comment div as an array
279 * @param string|array $class One or more classes to add to the class list
280 * @param int $comment_id An optional comment ID
281 * @param int $post_id An optional post ID
282 * @return array Array of classes
284 function get_comment_class( $class = '', $comment_id = null, $post_id = null ) {
285 global $comment_alt, $comment_depth, $comment_thread_alt;
287 $comment = get_comment($comment_id);
291 // Get the comment type (comment, trackback),
292 $classes[] = ( empty( $comment->comment_type ) ) ? 'comment' : $comment->comment_type;
294 // If the comment author has an id (registered), then print the log in name
295 if ( $comment->user_id > 0 && $user = get_userdata($comment->user_id) ) {
296 // For all registered users, 'byuser'
297 $classes[] = 'byuser';
298 $classes[] = 'comment-author-' . sanitize_html_class($user->user_nicename, $comment->user_id);
299 // For comment authors who are the author of the post
300 if ( $post = get_post($post_id) ) {
301 if ( $comment->user_id === $post->post_author )
302 $classes[] = 'bypostauthor';
306 if ( empty($comment_alt) )
308 if ( empty($comment_depth) )
310 if ( empty($comment_thread_alt) )
311 $comment_thread_alt = 0;
313 if ( $comment_alt % 2 ) {
322 // Alt for top-level comments
323 if ( 1 == $comment_depth ) {
324 if ( $comment_thread_alt % 2 ) {
325 $classes[] = 'thread-odd';
326 $classes[] = 'thread-alt';
328 $classes[] = 'thread-even';
330 $comment_thread_alt++;
333 $classes[] = "depth-$comment_depth";
335 if ( !empty($class) ) {
336 if ( !is_array( $class ) )
337 $class = preg_split('#\s+#', $class);
338 $classes = array_merge($classes, $class);
341 return apply_filters('comment_class', $classes, $class, $comment_id, $post_id);
345 * Retrieve the comment date of the current comment.
348 * @uses apply_filters() Calls 'get_comment_date' hook with the formated date and the $d parameter respectively
351 * @param string $d The format of the date (defaults to user's config)
352 * @return string The comment's date
354 function get_comment_date( $d = '' ) {
357 $date = mysql2date(get_option('date_format'), $comment->comment_date);
359 $date = mysql2date($d, $comment->comment_date);
360 return apply_filters('get_comment_date', $date, $d);
364 * Display the comment date of the current comment.
368 * @param string $d The format of the date (defaults to user's config)
370 function comment_date( $d = '' ) {
371 echo get_comment_date( $d );
375 * Retrieve the excerpt of the current comment.
377 * Will cut each word and only output the first 20 words with '...' at the end.
378 * If the word count is less than 20, then no truncating is done and no '...'
383 * @uses apply_filters() Calls 'get_comment_excerpt' on truncated comment
385 * @return string The maybe truncated comment with 20 words or less
387 function get_comment_excerpt() {
389 $comment_text = strip_tags($comment->comment_content);
390 $blah = explode(' ', $comment_text);
391 if (count($blah) > 20) {
399 for ($i=0; $i<$k; $i++) {
400 $excerpt .= $blah[$i] . ' ';
402 $excerpt .= ($use_dotdotdot) ? '...' : '';
403 return apply_filters('get_comment_excerpt', $excerpt);
407 * Display the excerpt of the current comment.
410 * @uses apply_filters() Calls 'comment_excerpt' hook before displaying excerpt
412 function comment_excerpt() {
413 echo apply_filters('comment_excerpt', get_comment_excerpt() );
417 * Retrieve the comment id of the current comment.
421 * @uses apply_filters() Calls the 'get_comment_ID' hook for the comment ID
423 * @return int The comment ID
425 function get_comment_ID() {
427 return apply_filters('get_comment_ID', $comment->comment_ID);
431 * Displays the comment id of the current comment.
434 * @see get_comment_ID() Echos Result
436 function comment_ID() {
437 echo get_comment_ID();
441 * Retrieve the link to a given comment.
446 * @param object|string|int $comment Comment to retrieve.
447 * @param array $args Optional args.
448 * @return string The permalink to the given comment.
450 function get_comment_link( $comment = null, $args = array() ) {
451 global $wp_rewrite, $in_comment_loop;
453 $comment = get_comment($comment);
456 if ( !is_array($args) ) {
459 $args['page'] = $page;
462 $defaults = array( 'type' => 'all', 'page' => '', 'per_page' => '', 'max_depth' => '' );
463 $args = wp_parse_args( $args, $defaults );
465 if ( '' === $args['per_page'] && get_option('page_comments') )
466 $args['per_page'] = get_option('comments_per_page');
468 if ( empty($args['per_page']) ) {
469 $args['per_page'] = 0;
473 if ( $args['per_page'] ) {
474 if ( '' == $args['page'] )
475 $args['page'] = ( !empty($in_comment_loop) ) ? get_query_var('cpage') : get_page_of_comment( $comment->comment_ID, $args );
477 if ( $wp_rewrite->using_permalinks() )
478 $link = user_trailingslashit( trailingslashit( get_permalink( $comment->comment_post_ID ) ) . 'comment-page-' . $args['page'], 'comment' );
480 $link = add_query_arg( 'cpage', $args['page'], get_permalink( $comment->comment_post_ID ) );
482 $link = get_permalink( $comment->comment_post_ID );
485 return apply_filters( 'get_comment_link', $link . '#comment-' . $comment->comment_ID, $comment, $args );
489 * Retrieves the link to the current post comments.
493 * @return string The link to the comments
495 function get_comments_link() {
496 return get_permalink() . '#comments';
500 * Displays the link to the current post comments.
504 * @param string $deprecated Not Used
505 * @param bool $deprecated Not Used
507 function comments_link( $deprecated = '', $deprecated = '' ) {
508 echo get_comments_link();
512 * Retrieve the amount of comments a post has.
515 * @uses apply_filters() Calls the 'get_comments_number' hook on the number of comments
517 * @param int $post_id The Post ID
518 * @return int The number of comments a post has
520 function get_comments_number( $post_id = 0 ) {
522 $post_id = (int) $post_id;
525 $post_id = (int) $id;
527 $post = get_post($post_id);
528 if ( ! isset($post->comment_count) )
531 $count = $post->comment_count;
533 return apply_filters('get_comments_number', $count);
537 * Display the language string for the number of comments the current post has.
541 * @uses apply_filters() Calls the 'comments_number' hook on the output and number of comments respectively.
543 * @param string $zero Text for no comments
544 * @param string $one Text for one comment
545 * @param string $more Text for more than one comment
546 * @param string $deprecated Not used.
548 function comments_number( $zero = false, $one = false, $more = false, $deprecated = '' ) {
550 $number = get_comments_number($id);
553 $output = str_replace('%', number_format_i18n($number), ( false === $more ) ? __('% Comments') : $more);
554 elseif ( $number == 0 )
555 $output = ( false === $zero ) ? __('No Comments') : $zero;
557 $output = ( false === $one ) ? __('1 Comment') : $one;
559 echo apply_filters('comments_number', $output, $number);
563 * Retrieve the text of the current comment.
568 * @return string The comment content
570 function get_comment_text() {
572 return apply_filters('get_comment_text', $comment->comment_content);
576 * Displays the text of the current comment.
579 * @uses apply_filters() Passes the comment content through the 'comment_text' hook before display
580 * @uses get_comment_text() Gets the comment content
582 function comment_text() {
583 echo apply_filters('comment_text', get_comment_text() );
587 * Retrieve the comment time of the current comment.
591 * @uses apply_filter() Calls 'get_comment_time' hook with the formatted time, the $d parameter, and $gmt parameter passed.
593 * @param string $d Optional. The format of the time (defaults to user's config)
594 * @param bool $gmt Whether to use the GMT date
595 * @param bool $translate Whether to translate the time (for use in feeds)
596 * @return string The formatted time
598 function get_comment_time( $d = '', $gmt = false, $translate = true ) {
600 $comment_date = $gmt? $comment->comment_date_gmt : $comment->comment_date;
602 $date = mysql2date(get_option('time_format'), $comment_date, $translate);
604 $date = mysql2date($d, $comment_date, $translate);
605 return apply_filters('get_comment_time', $date, $d, $gmt);
609 * Display the comment time of the current comment.
613 * @param string $d Optional. The format of the time (defaults to user's config)
615 function comment_time( $d = '' ) {
616 echo get_comment_time($d);
620 * Retrieve the comment type of the current comment.
624 * @uses apply_filters() Calls the 'get_comment_type' hook on the comment type
626 * @return string The comment type
628 function get_comment_type() {
631 if ( '' == $comment->comment_type )
632 $comment->comment_type = 'comment';
634 return apply_filters('get_comment_type', $comment->comment_type);
638 * Display the comment type of the current comment.
642 * @param string $commenttxt The string to display for comment type
643 * @param string $trackbacktxt The string to display for trackback type
644 * @param string $pingbacktxt The string to display for pingback type
646 function comment_type($commenttxt = false, $trackbacktxt = false, $pingbacktxt = false) {
647 if ( false === $commenttxt ) $commenttxt = _x( 'Comment', 'noun' );
648 if ( false === $trackbacktxt ) $trackbacktxt = __( 'Trackback' );
649 if ( false === $pingbacktxt ) $pingbacktxt = __( 'Pingback' );
650 $type = get_comment_type();
664 * Retrieve The current post's trackback URL.
666 * There is a check to see if permalink's have been enabled and if so, will
667 * retrieve the pretty path. If permalinks weren't enabled, the ID of the
668 * current post is used and appended to the correct page to go to.
671 * @uses apply_filters() Calls 'trackback_url' on the resulting trackback URL
674 * @return string The trackback URL after being filtered
676 function get_trackback_url() {
678 if ( '' != get_option('permalink_structure') ) {
679 $tb_url = trailingslashit(get_permalink()) . user_trailingslashit('trackback', 'single_trackback');
681 $tb_url = get_option('siteurl') . '/wp-trackback.php?p=' . $id;
683 return apply_filters('trackback_url', $tb_url);
687 * Displays the current post's trackback URL.
690 * @uses get_trackback_url() Gets the trackback url for the current post
692 * @param bool $deprecated Remove backwards compat in 2.5
693 * @return void|string Should only be used to echo the trackback URL, use get_trackback_url() for the result instead.
695 function trackback_url($deprecated = true) {
696 if ($deprecated) echo get_trackback_url();
697 else return get_trackback_url();
701 * Generates and displays the RDF for the trackback information of current post.
705 * @param int $deprecated Not used (Was $timezone = 0)
707 function trackback_rdf($deprecated = '') {
708 if (stripos($_SERVER['HTTP_USER_AGENT'], 'W3C_Validator') === false) {
709 echo '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
710 xmlns:dc="http://purl.org/dc/elements/1.1/"
711 xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
712 <rdf:Description rdf:about="';
715 echo ' dc:identifier="';
718 echo ' dc:title="'.str_replace('--', '--', wptexturize(strip_tags(get_the_title()))).'"'."\n";
719 echo ' trackback:ping="'.get_trackback_url().'"'." />\n";
725 * Whether the current post is open for comments.
730 * @param int $post_id An optional post ID to check instead of the current post.
731 * @return bool True if the comments are open
733 function comments_open( $post_id=NULL ) {
735 $_post = get_post($post_id);
737 $open = ( 'open' == $_post->comment_status );
738 return apply_filters( 'comments_open', $open, $post_id );
742 * Whether the current post is open for pings.
747 * @param int $post_id An optional post ID to check instead of the current post.
748 * @return bool True if pings are accepted
750 function pings_open( $post_id = NULL ) {
752 $_post = get_post($post_id);
754 $open = ( 'open' == $_post->ping_status );
755 return apply_filters( 'pings_open', $open, $post_id );
759 * Displays form token for unfiltered comments.
761 * Will only display nonce token if the current user has permissions for
762 * unfiltered html. Won't display the token for other users.
764 * The function was backported to 2.0.10 and was added to versions 2.1.3 and
765 * above. Does not exist in versions prior to 2.0.10 in the 2.0 branch and in
766 * the 2.1 branch, prior to 2.1.3. Technically added in 2.2.0.
768 * Backported to 2.0.10.
771 * @uses $post Gets the ID of the current post for the token
773 function wp_comment_form_unfiltered_html_nonce() {
778 $post_id = $post->ID;
780 if ( current_user_can('unfiltered_html') )
781 wp_nonce_field('unfiltered-html-comment_' . $post_id, '_wp_unfiltered_html_comment', false);
785 * Loads the comment template specified in $file.
787 * Will not display the comments template if not on single post or page, or if
788 * the post does not have comments.
790 * Uses the WordPress database object to query for the comments. The comments
791 * are passed through the 'comments_array' filter hook with the list of comments
792 * and the post ID respectively.
794 * The $file path is passed through a filter hook called, 'comments_template'
795 * which includes the TEMPLATEPATH and $file combined. Tries the $filtered path
796 * first and if it fails it will require the default comment themplate from the
797 * default theme. If either does not exist, then the WordPress process will be
798 * halted. It is advised for that reason, that the default theme is not deleted.
801 * @global array $comment List of comment objects for the current post
805 * @uses $withcomments Will not try to get the comments if the post has none.
807 * @param string $file Optional, default '/comments.php'. The file to load
808 * @param bool $separate_comments Optional, whether to separate the comments by comment type. Default is false.
809 * @return null Returns null if no comments appear
811 function comments_template( $file = '/comments.php', $separate_comments = false ) {
812 global $wp_query, $withcomments, $post, $wpdb, $id, $comment, $user_login, $user_ID, $user_identity, $overridden_cpage;
814 if ( ! (is_single() || is_page() || $withcomments) )
818 $file = '/comments.php';
820 $req = get_option('require_name_email');
823 * Comment author information fetched from the comment cookies.
825 * @uses wp_get_current_commenter()
827 $commenter = wp_get_current_commenter();
830 * The name of the current comment author escaped for use in attributes.
832 $comment_author = $commenter['comment_author']; // Escaped by sanitize_comment_cookies()
835 * The email address of the current comment author escaped for use in attributes.
837 $comment_author_email = $commenter['comment_author_email']; // Escaped by sanitize_comment_cookies()
840 * The url of the current comment author escaped for use in attributes.
842 $comment_author_url = esc_url($commenter['comment_author_url']);
844 /** @todo Use API instead of SELECTs. */
846 $comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND (comment_approved = '1' OR ( user_id = %d AND comment_approved = '0' ) ) ORDER BY comment_date_gmt", $post->ID, $user_ID));
847 } else if ( empty($comment_author) ) {
848 $comments = get_comments( array('post_id' => $post->ID, 'status' => 'approve', 'order' => 'ASC') );
850 $comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND ( comment_approved = '1' OR ( comment_author = %s AND comment_author_email = %s AND comment_approved = '0' ) ) ORDER BY comment_date_gmt", $post->ID, wp_specialchars_decode($comment_author,ENT_QUOTES), $comment_author_email));
853 // keep $comments for legacy's sake
854 $wp_query->comments = apply_filters( 'comments_array', $comments, $post->ID );
855 $comments = &$wp_query->comments;
856 $wp_query->comment_count = count($wp_query->comments);
857 update_comment_cache($wp_query->comments);
859 if ( $separate_comments ) {
860 $wp_query->comments_by_type = &separate_comments($comments);
861 $comments_by_type = &$wp_query->comments_by_type;
864 $overridden_cpage = FALSE;
865 if ( '' == get_query_var('cpage') && get_option('page_comments') ) {
866 set_query_var( 'cpage', 'newest' == get_option('default_comments_page') ? get_comment_pages_count() : 1 );
867 $overridden_cpage = TRUE;
870 if ( !defined('COMMENTS_TEMPLATE') || !COMMENTS_TEMPLATE)
871 define('COMMENTS_TEMPLATE', true);
873 $include = apply_filters('comments_template', STYLESHEETPATH . $file );
874 if ( file_exists( $include ) )
876 elseif ( file_exists( TEMPLATEPATH . $file ) )
877 require( TEMPLATEPATH . $file );
879 require( get_theme_root() . '/default/comments.php');
883 * Displays the JS popup script to show a comment.
885 * If the $file parameter is empty, then the home page is assumed. The defaults
886 * for the window are 400px by 400px.
888 * For the comment link popup to work, this function has to be called or the
889 * normal comment link will be assumed.
892 * @global string $wpcommentspopupfile The URL to use for the popup window
893 * @global int $wpcommentsjavascript Whether to use JavaScript or not. Set when function is called
895 * @param int $width Optional. The width of the popup window
896 * @param int $height Optional. The height of the popup window
897 * @param string $file Optional. Sets the location of the popup window
899 function comments_popup_script($width=400, $height=400, $file='') {
900 global $wpcommentspopupfile, $wpcommentsjavascript;
903 $wpcommentspopupfile = ''; // Use the index.
905 $wpcommentspopupfile = $file;
908 $wpcommentsjavascript = 1;
909 $javascript = "<script type='text/javascript'>\nfunction wpopen (macagna) {\n window.open(macagna, '_blank', 'width=$width,height=$height,scrollbars=yes,status=yes');\n}\n</script>\n";
914 * Displays the link to the comments popup window for the current post ID.
916 * Is not meant to be displayed on single posts and pages. Should be used on the
921 * @uses $wpcommentspopupfile
922 * @uses $wpcommentsjavascript
925 * @param string $zero The string to display when no comments
926 * @param string $one The string to display when only one comment is available
927 * @param string $more The string to display when there are more than one comment
928 * @param string $css_class The CSS class to use for comments
929 * @param string $none The string to display when comments have been turned off
930 * @return null Returns null on single posts and pages.
932 function comments_popup_link( $zero = false, $one = false, $more = false, $css_class = '', $none = false ) {
933 global $id, $wpcommentspopupfile, $wpcommentsjavascript, $post;
935 if ( false === $zero ) $zero = __( 'No Comments' );
936 if ( false === $one ) $one = __( '1 Comment' );
937 if ( false === $more ) $more = __( '% Comments' );
938 if ( false === $none ) $none = __( 'Comments Off' );
940 $number = get_comments_number( $id );
942 if ( 0 == $number && !comments_open() && !pings_open() ) {
943 echo '<span' . ((!empty($css_class)) ? ' class="' . $css_class . '"' : '') . '>' . $none . '</span>';
947 if ( post_password_required() ) {
948 echo __('Enter your password to view comments');
953 if ( $wpcommentsjavascript ) {
954 if ( empty( $wpcommentspopupfile ) )
955 $home = get_option('home');
957 $home = get_option('siteurl');
958 echo $home . '/' . $wpcommentspopupfile . '?comments_popup=' . $id;
959 echo '" onclick="wpopen(this.href); return false"';
960 } else { // if comments_popup_script() is not in the template, display simple comment link
962 echo get_permalink() . '#respond';
968 if ( !empty( $css_class ) ) {
969 echo ' class="'.$css_class.'" ';
971 $title = esc_attr( get_the_title() );
973 echo apply_filters( 'comments_popup_link_attributes', '' );
975 echo ' title="' . sprintf( __('Comment on %s'), $title ) . '">';
976 comments_number( $zero, $one, $more, $number );
981 * Retrieve HTML content for reply to comment link.
983 * The default arguments that can be override are 'add_below', 'respond_id',
984 * 'reply_text', 'login_text', and 'depth'. The 'login_text' argument will be
985 * used, if the user must log in or register first before posting a comment. The
986 * 'reply_text' will be used, if they can post a reply. The 'add_below' and
987 * 'respond_id' arguments are for the JavaScript moveAddCommentForm() function
992 * @param array $args Optional. Override default options.
993 * @param int $comment Optional. Comment being replied to.
994 * @param int $post Optional. Post that the comment is going to be displayed on.
995 * @return string|bool|null Link to show comment form, if successful. False, if comments are closed.
997 function get_comment_reply_link($args = array(), $comment = null, $post = null) {
1000 $defaults = array('add_below' => 'comment', 'respond_id' => 'respond', 'reply_text' => __('Reply'),
1001 'login_text' => __('Log in to Reply'), 'depth' => 0, 'before' => '', 'after' => '');
1003 $args = wp_parse_args($args, $defaults);
1005 if ( 0 == $args['depth'] || $args['max_depth'] <= $args['depth'] )
1008 extract($args, EXTR_SKIP);
1010 $comment = get_comment($comment);
1011 $post = get_post($post);
1013 if ( !comments_open($post->ID) )
1018 if ( get_option('comment_registration') && !$user_ID )
1019 $link = '<a rel="nofollow" class="comment-reply-login" href="' . esc_url( wp_login_url( get_permalink() ) ) . '">' . $login_text . '</a>';
1021 $link = "<a rel='nofollow' class='comment-reply-link' href='" . esc_url( add_query_arg( 'replytocom', $comment->comment_ID ) ) . "#" . $respond_id . "' onclick='return addComment.moveForm(\"$add_below-$comment->comment_ID\", \"$comment->comment_ID\", \"$respond_id\", \"$post->ID\")'>$reply_text</a>";
1022 return apply_filters('comment_reply_link', $before . $link . $after, $args, $comment, $post);
1026 * Displays the HTML content for reply to comment link.
1029 * @see get_comment_reply_link() Echoes result
1031 * @param array $args Optional. Override default options.
1032 * @param int $comment Optional. Comment being replied to.
1033 * @param int $post Optional. Post that the comment is going to be displayed on.
1034 * @return string|bool|null Link to show comment form, if successful. False, if comments are closed.
1036 function comment_reply_link($args = array(), $comment = null, $post = null) {
1037 echo get_comment_reply_link($args, $comment, $post);
1041 * Retrieve HTML content for reply to post link.
1043 * The default arguments that can be override are 'add_below', 'respond_id',
1044 * 'reply_text', 'login_text', and 'depth'. The 'login_text' argument will be
1045 * used, if the user must log in or register first before posting a comment. The
1046 * 'reply_text' will be used, if they can post a reply. The 'add_below' and
1047 * 'respond_id' arguments are for the JavaScript moveAddCommentForm() function
1052 * @param array $args Optional. Override default options.
1053 * @param int|object $post Optional. Post that the comment is going to be displayed on. Defaults to current post.
1054 * @return string|bool|null Link to show comment form, if successful. False, if comments are closed.
1056 function get_post_reply_link($args = array(), $post = null) {
1059 $defaults = array('add_below' => 'post', 'respond_id' => 'respond', 'reply_text' => __('Leave a Comment'),
1060 'login_text' => __('Log in to leave a Comment'), 'before' => '', 'after' => '');
1062 $args = wp_parse_args($args, $defaults);
1063 extract($args, EXTR_SKIP);
1064 $post = get_post($post);
1066 if ( !comments_open($post->ID) )
1069 if ( get_option('comment_registration') && !$user_ID ) {
1070 $link = '<a rel="nofollow" href="' . wp_login_url( get_permalink() ) . '">' . $login_text . '</a>';
1072 $link = "<a rel='nofollow' class='comment-reply-link' href='" . get_permalink($post->ID) . "#$respond_id' onclick='return addComment.moveForm(\"$add_below-$post->ID\", \"0\", \"$respond_id\", \"$post->ID\")'>$reply_text</a>";
1074 return apply_filters('post_comments_link', $before . $link . $after, $post);
1078 * Displays the HTML content for reply to post link.
1080 * @see get_post_reply_link()
1082 * @param array $args Optional. Override default options.
1083 * @param int|object $post Optional. Post that the comment is going to be displayed on.
1084 * @return string|bool|null Link to show comment form, if successful. False, if comments are closed.
1086 function post_reply_link($args = array(), $post = null) {
1087 echo get_post_reply_link($args, $post);
1091 * Retrieve HTML content for cancel comment reply link.
1095 * @param string $text Optional. Text to display for cancel reply link.
1097 function get_cancel_comment_reply_link($text = '') {
1099 $text = __('Click here to cancel reply.');
1101 $style = isset($_GET['replytocom']) ? '' : ' style="display:none;"';
1102 $link = esc_html( remove_query_arg('replytocom') ) . '#respond';
1103 return apply_filters('cancel_comment_reply_link', '<a rel="nofollow" id="cancel-comment-reply-link" href="' . $link . '"' . $style . '>' . $text . '</a>', $link, $text);
1107 * Display HTML content for cancel comment reply link.
1111 * @param string $text Optional. Text to display for cancel reply link.
1113 function cancel_comment_reply_link($text = '') {
1114 echo get_cancel_comment_reply_link($text);
1118 * Output hidden input HTML for replying to comments.
1122 function comment_id_fields() {
1125 $replytoid = isset($_GET['replytocom']) ? (int) $_GET['replytocom'] : 0;
1126 echo "<input type='hidden' name='comment_post_ID' value='$id' id='comment_post_ID' />\n";
1127 echo "<input type='hidden' name='comment_parent' id='comment_parent' value='$replytoid' />\n";
1131 * Display text based on comment reply status. Only affects users with Javascript disabled.
1135 * @param string $noreplytext Optional. Text to display when not replying to a comment.
1136 * @param string $replytext Optional. Text to display when replying to a comment. Accepts "%s" for the author of the comment being replied to.
1137 * @param string $linktoparent Optional. Boolean to control making the author's name a link to their comment.
1139 function comment_form_title( $noreplytext = false, $replytext = false, $linktoparent = TRUE ) {
1142 if ( false === $noreplytext ) $noreplytext = __( 'Leave a Reply' );
1143 if ( false === $replytext ) $replytext = __( 'Leave a Reply to %s' );
1145 $replytoid = isset($_GET['replytocom']) ? (int) $_GET['replytocom'] : 0;
1147 if ( 0 == $replytoid )
1150 $comment = get_comment($replytoid);
1151 $author = ( $linktoparent ) ? '<a href="#comment-' . get_comment_ID() . '">' . get_comment_author() . '</a>' : get_comment_author();
1152 printf( $replytext, $author );
1157 * HTML comment list class.
1159 * @package WordPress
1163 class Walker_Comment extends Walker {
1165 * @see Walker::$tree_type
1169 var $tree_type = 'comment';
1172 * @see Walker::$db_fields
1176 var $db_fields = array ('parent' => 'comment_parent', 'id' => 'comment_ID');
1179 * @see Walker::start_lvl()
1182 * @param string $output Passed by reference. Used to append additional content.
1183 * @param int $depth Depth of comment.
1184 * @param array $args Uses 'style' argument for type of HTML list.
1186 function start_lvl(&$output, $depth, $args) {
1187 $GLOBALS['comment_depth'] = $depth + 1;
1189 switch ( $args['style'] ) {
1193 echo "<ol class='children'>\n";
1197 echo "<ul class='children'>\n";
1203 * @see Walker::end_lvl()
1206 * @param string $output Passed by reference. Used to append additional content.
1207 * @param int $depth Depth of comment.
1208 * @param array $args Will only append content if style argument value is 'ol' or 'ul'.
1210 function end_lvl(&$output, $depth, $args) {
1211 $GLOBALS['comment_depth'] = $depth + 1;
1213 switch ( $args['style'] ) {
1227 * @see Walker::start_el()
1230 * @param string $output Passed by reference. Used to append additional content.
1231 * @param object $comment Comment data object.
1232 * @param int $depth Depth of comment in reference to parents.
1233 * @param array $args
1235 function start_el(&$output, $comment, $depth, $args) {
1237 $GLOBALS['comment_depth'] = $depth;
1239 if ( !empty($args['callback']) ) {
1240 call_user_func($args['callback'], $comment, $args, $depth);
1244 $GLOBALS['comment'] = $comment;
1245 extract($args, EXTR_SKIP);
1247 if ( 'div' == $args['style'] ) {
1249 $add_below = 'comment';
1252 $add_below = 'div-comment';
1255 <<?php echo $tag ?> <?php comment_class(empty( $args['has_children'] ) ? '' : 'parent') ?> id="comment-<?php comment_ID() ?>">
1256 <?php if ( 'ul' == $args['style'] ) : ?>
1257 <div id="div-comment-<?php comment_ID() ?>" class="comment-body">
1259 <div class="comment-author vcard">
1260 <?php if ($args['avatar_size'] != 0) echo get_avatar( $comment, $args['avatar_size'] ); ?>
1261 <?php printf(__('<cite class="fn">%s</cite> <span class="says">says:</span>'), get_comment_author_link()) ?>
1263 <?php if ($comment->comment_approved == '0') : ?>
1264 <em><?php _e('Your comment is awaiting moderation.') ?></em>
1268 <div class="comment-meta commentmetadata"><a href="<?php echo htmlspecialchars( get_comment_link( $comment->comment_ID ) ) ?>"><?php printf(__('%1$s at %2$s'), get_comment_date(), get_comment_time()) ?></a><?php edit_comment_link(__('(Edit)'),' ','') ?></div>
1270 <?php comment_text() ?>
1273 <?php comment_reply_link(array_merge( $args, array('add_below' => $add_below, 'depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
1275 <?php if ( 'ul' == $args['style'] ) : ?>
1282 * @see Walker::end_el()
1285 * @param string $output Passed by reference. Used to append additional content.
1286 * @param object $comment
1287 * @param int $depth Depth of comment.
1288 * @param array $args
1290 function end_el(&$output, $comment, $depth, $args) {
1291 if ( !empty($args['end-callback']) ) {
1292 call_user_func($args['end-callback'], $comment, $args, $depth);
1295 if ( 'div' == $args['style'] )
1306 * Used in the comments.php template to list comments for a particular post
1309 * @uses Walker_Comment
1311 * @param string|array $args Formatting options
1312 * @param array $comments Optional array of comment objects. Defaults to $wp_query->comments
1314 function wp_list_comments($args = array(), $comments = null ) {
1315 global $wp_query, $comment_alt, $comment_depth, $comment_thread_alt, $overridden_cpage, $in_comment_loop;
1317 $in_comment_loop = true;
1319 $comment_alt = $comment_thread_alt = 0;
1322 $defaults = array('walker' => null, 'max_depth' => '', 'style' => 'ul', 'callback' => null, 'end-callback' => null, 'type' => 'all',
1323 'page' => '', 'per_page' => '', 'avatar_size' => 32, 'reverse_top_level' => null, 'reverse_children' => '');
1325 $r = wp_parse_args( $args, $defaults );
1327 // Figure out what comments we'll be looping through ($_comments)
1328 if ( null !== $comments ) {
1329 $comments = (array) $comments;
1330 if ( empty($comments) )
1332 if ( 'all' != $r['type'] ) {
1333 $comments_by_type = &separate_comments($comments);
1334 if ( empty($comments_by_type[$r['type']]) )
1336 $_comments = $comments_by_type[$r['type']];
1338 $_comments = $comments;
1341 if ( empty($wp_query->comments) )
1343 if ( 'all' != $r['type'] ) {
1344 if ( empty($wp_query->comments_by_type) )
1345 $wp_query->comments_by_type = &separate_comments($wp_query->comments);
1346 if ( empty($wp_query->comments_by_type[$r['type']]) )
1348 $_comments = $wp_query->comments_by_type[$r['type']];
1350 $_comments = $wp_query->comments;
1354 if ( '' === $r['per_page'] && get_option('page_comments') )
1355 $r['per_page'] = get_query_var('comments_per_page');
1357 if ( empty($r['per_page']) ) {
1362 if ( '' === $r['max_depth'] ) {
1363 if ( get_option('thread_comments') )
1364 $r['max_depth'] = get_option('thread_comments_depth');
1366 $r['max_depth'] = -1;
1369 if ( '' === $r['page'] ) {
1370 if ( empty($overridden_cpage) ) {
1371 $r['page'] = get_query_var('cpage');
1373 $threaded = ( -1 == $r['max_depth'] ) ? false : true;
1374 $r['page'] = ( 'newest' == get_option('default_comments_page') ) ? get_comment_pages_count($_comments, $r['per_page'], $threaded) : 1;
1375 set_query_var( 'cpage', $r['page'] );
1379 $r['page'] = intval($r['page']);
1380 if ( 0 == $r['page'] && 0 != $r['per_page'] )
1383 if ( null === $r['reverse_top_level'] )
1384 $r['reverse_top_level'] = ( 'desc' == get_option('comment_order') ) ? TRUE : FALSE;
1386 extract( $r, EXTR_SKIP );
1388 if ( empty($walker) )
1389 $walker = new Walker_Comment;
1391 $walker->paged_walk($_comments, $max_depth, $page, $per_page, $r);
1392 $wp_query->max_num_comment_pages = $walker->max_pages;
1394 $in_comment_loop = false;