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
19 * @param int $comment_ID Optional. The ID of the comment for which to retrieve the author. Default current comment.
20 * @return string The comment author
22 function get_comment_author( $comment_ID = 0 ) {
23 $comment = get_comment( $comment_ID );
25 if ( empty( $comment->comment_author ) ) {
26 if ( $comment->user_id && $user = get_userdata( $comment->user_id ) )
27 $author = $user->display_name;
29 $author = __('Anonymous');
31 $author = $comment->comment_author;
35 * Filter the returned comment author name.
38 * @since 4.1.0 The `$comment_ID` and `$comment` parameters were added.
40 * @param string $author The comment author's username.
41 * @param int $comment_ID The comment ID.
42 * @param object $comment The comment object.
44 return apply_filters( 'get_comment_author', $author, $comment_ID, $comment );
48 * Displays the author of the current comment.
52 * @param int $comment_ID Optional. The ID of the comment for which to print the author. Default current comment.
54 function comment_author( $comment_ID = 0 ) {
55 $author = get_comment_author( $comment_ID );
58 * Filter the comment author's name for display.
61 * @since 4.1.0 The `$comment_ID` parameter was added.
63 * @param string $author The comment author's username.
64 * @param int $comment_ID The comment ID.
66 $author = apply_filters( 'comment_author', $author, $comment_ID );
71 * Retrieve the email of the author of the current comment.
75 * @param int $comment_ID Optional. The ID of the comment for which to get the author's email. Default current comment.
76 * @return string The current comment author's email
78 function get_comment_author_email( $comment_ID = 0 ) {
79 $comment = get_comment( $comment_ID );
82 * Filter the comment author's returned email address.
85 * @since 4.1.0 The `$comment_ID` and `$comment` parameters were added.
87 * @param string $comment_author_email The comment author's email address.
88 * @param int $comment_ID The comment ID.
89 * @param object $comment The comment object.
91 return apply_filters( 'get_comment_author_email', $comment->comment_author_email, $comment_ID, $comment );
95 * Display the email of the author of the current global $comment.
97 * Care should be taken to protect the email address and assure that email
98 * harvesters do not capture your commentors' email address. Most assume that
99 * their email address will not appear in raw form on the blog. Doing so will
100 * enable anyone, including those that people don't want to get the email
101 * address and use it for their own means good and bad.
105 * @param int $comment_ID Optional. The ID of the comment for which to print the author's email. Default current comment.
107 function comment_author_email( $comment_ID = 0 ) {
108 $author_email = get_comment_author_email( $comment_ID );
111 * Filter the comment author's email for display.
114 * @since 4.1.0 The `$comment_ID` parameter was added.
116 * @param string $author_email The comment author's email address.
117 * @param int $comment_ID The comment ID.
119 echo apply_filters( 'author_email', $author_email, $comment_ID );
123 * Display the html email link to the author of the current comment.
125 * Care should be taken to protect the email address and assure that email
126 * harvesters do not capture your commentors' email address. Most assume that
127 * their email address will not appear in raw form on the blog. Doing so will
128 * enable anyone, including those that people don't want to get the email
129 * address and use it for their own means good and bad.
133 * @param string $linktext Optional. Text to display instead of the comment author's email address.
135 * @param string $before Optional. Text or HTML to display before the email link. Default empty.
136 * @param string $after Optional. Text or HTML to display after the email link. Default empty.
138 function comment_author_email_link( $linktext = '', $before = '', $after = '' ) {
139 if ( $link = get_comment_author_email_link( $linktext, $before, $after ) )
144 * Return the html email link to the author of the current comment.
146 * Care should be taken to protect the email address and assure that email
147 * harvesters do not capture your commentors' email address. Most assume that
148 * their email address will not appear in raw form on the blog. Doing so will
149 * enable anyone, including those that people don't want to get the email
150 * address and use it for their own means good and bad.
152 * @global object $comment The current Comment row object.
156 * @param string $linktext Optional. Text to display instead of the comment author's email address.
158 * @param string $before Optional. Text or HTML to display before the email link. Default empty.
159 * @param string $after Optional. Text or HTML to display after the email link. Default empty.
161 function get_comment_author_email_link( $linktext = '', $before = '', $after = '' ) {
165 * Filter the comment author's email for display.
167 * Care should be taken to protect the email address and assure that email
168 * harvesters do not capture your commenter's email address.
171 * @since 4.1.0 The `$comment` parameter was added.
173 * @param string $comment_author_email The comment author's email address.
174 * @param object $comment The comment object.
176 $email = apply_filters( 'comment_email', $comment->comment_author_email, $comment );
177 if ((!empty($email)) && ($email != '@')) {
178 $display = ($linktext != '') ? $linktext : $email;
180 $return .= "<a href='mailto:$email'>$display</a>";
189 * Retrieve the HTML link to the URL of the author of the current comment.
191 * Both get_comment_author_url() and get_comment_author() rely on get_comment(),
192 * which falls back to the global comment variable if the $comment_ID argument is empty.
196 * @param int $comment_ID ID of the comment for which to get the author's link.
197 * Default current comment.
198 * @return string The comment author name or HTML link for author's URL.
200 function get_comment_author_link( $comment_ID = 0 ) {
201 $url = get_comment_author_url( $comment_ID );
202 $author = get_comment_author( $comment_ID );
204 if ( empty( $url ) || 'http://' == $url )
207 $return = "<a href='$url' rel='external nofollow' class='url'>$author</a>";
210 * Filter the comment author's link for display.
213 * @since 4.1.0 The `$author` and `$comment_ID` parameters were added.
215 * @param string $return The HTML-formatted comment author link.
216 * Empty for an invalid URL.
217 * @param string $author The comment author's username.
218 * @param int $comment_ID The comment ID.
220 return apply_filters( 'get_comment_author_link', $return, $author, $comment_ID );
224 * Display the html link to the url of the author of the current comment.
228 * @see get_comment_author_link() Echoes result
230 * @param int $comment_ID ID of the comment for which to print the author's
231 * link. Default current comment.
233 function comment_author_link( $comment_ID = 0 ) {
234 echo get_comment_author_link( $comment_ID );
238 * Retrieve the IP address of the author of the current comment.
242 * @param int $comment_ID ID of the comment for which to get the author's IP
243 * address. Default current comment.
244 * @return string Comment author's IP address.
246 function get_comment_author_IP( $comment_ID = 0 ) {
247 $comment = get_comment( $comment_ID );
250 * Filter the comment author's returned IP address.
253 * @since 4.1.0 The `$comment_ID` and `$comment` parameters were added.
255 * @param string $comment_author_IP The comment author's IP address.
256 * @param int $comment_ID The comment ID.
257 * @param object $comment The comment object.
259 return apply_filters( 'get_comment_author_IP', $comment->comment_author_IP, $comment_ID, $comment );
263 * Display the IP address of the author of the current comment.
267 * @param int $comment_ID ID of the comment for which to print the author's IP
268 * address. Default current comment.
270 function comment_author_IP( $comment_ID = 0 ) {
271 echo get_comment_author_IP( $comment_ID );
275 * Retrieve the url of the author of the current comment.
279 * @param int $comment_ID ID of the comment for which to get the author's URL.
280 * Default current comment.
283 function get_comment_author_url( $comment_ID = 0 ) {
284 $comment = get_comment( $comment_ID );
285 $url = ('http://' == $comment->comment_author_url) ? '' : $comment->comment_author_url;
286 $url = esc_url( $url, array('http', 'https') );
289 * Filter the comment author's URL.
292 * @since 4.1.0 The `$comment_ID` and `$comment` parameters were added.
294 * @param string $url The comment author's URL.
295 * @param int $comment_ID The comment ID.
296 * @param object $comment The comment object.
298 return apply_filters( 'get_comment_author_url', $url, $comment_ID, $comment );
302 * Display the url of the author of the current comment.
306 * @param int $comment_ID ID of the comment for which to print the author's URL.
307 * Default current comment.
309 function comment_author_url( $comment_ID = 0 ) {
310 $author_url = get_comment_author_url( $comment_ID );
313 * Filter the comment author's URL for display.
316 * @since 4.1.0 The `$comment_ID` parameter was added.
318 * @param string $author_url The comment author's URL.
319 * @param int $comment_ID The comment ID.
321 echo apply_filters( 'comment_url', $author_url, $comment_ID );
325 * Retrieves the HTML link of the url of the author of the current comment.
327 * $linktext parameter is only used if the URL does not exist for the comment
328 * author. If the URL does exist then the URL will be used and the $linktext
331 * Encapsulate the HTML link between the $before and $after. So it will appear
332 * in the order of $before, link, and finally $after.
336 * @param string $linktext Optional. The text to display instead of the comment
337 * author's email address. Default empty.
338 * @param string $before Optional. The text or HTML to display before the email link.
340 * @param string $after Optional. The text or HTML to display after the email link.
342 * @return string The HTML link between the $before and $after parameters.
344 function get_comment_author_url_link( $linktext = '', $before = '', $after = '' ) {
345 $url = get_comment_author_url();
346 $display = ($linktext != '') ? $linktext : $url;
347 $display = str_replace( 'http://www.', '', $display );
348 $display = str_replace( 'http://', '', $display );
350 if ( '/' == substr($display, -1) ) {
351 $display = substr($display, 0, -1);
354 $return = "$before<a href='$url' rel='external'>$display</a>$after";
357 * Filter the comment author's returned URL link.
361 * @param string $return The HTML-formatted comment author URL link.
363 return apply_filters( 'get_comment_author_url_link', $return );
367 * Displays the HTML link of the url of the author of the current comment.
371 * @param string $linktext Optional. Text to display instead of the comment author's
372 * email address. Default empty.
373 * @param string $before Optional. Text or HTML to display before the email link.
375 * @param string $after Optional. Text or HTML to display after the email link.
378 function comment_author_url_link( $linktext = '', $before = '', $after = '' ) {
379 echo get_comment_author_url_link( $linktext, $before, $after );
383 * Generates semantic classes for each comment element.
387 * @param string|array $class Optional. One or more classes to add to the class list.
389 * @param int $comment_id Comment ID. Default current comment.
390 * @param int|WP_Post $post_id Post ID or WP_Post object. Default current post.
391 * @param bool $echo Optional. Whether to cho or return the output.
394 function comment_class( $class = '', $comment_id = null, $post_id = null, $echo = true ) {
395 // Separates classes with a single space, collates classes for comment DIV
396 $class = 'class="' . join( ' ', get_comment_class( $class, $comment_id, $post_id ) ) . '"';
404 * Returns the classes for the comment div as an array.
408 * @param string|array $class Optional. One or more classes to add to the class list. Default empty.
409 * @param int $comment_id Comment ID. Default current comment.
410 * @param int|WP_Post $post_id Post ID or WP_Post object. Default current post.
411 * @return array An array of classes.
413 function get_comment_class( $class = '', $comment_id = null, $post_id = null ) {
414 global $comment_alt, $comment_depth, $comment_thread_alt;
416 $comment = get_comment($comment_id);
420 // Get the comment type (comment, trackback),
421 $classes[] = ( empty( $comment->comment_type ) ) ? 'comment' : $comment->comment_type;
423 // If the comment author has an id (registered), then print the log in name
424 if ( $comment->user_id > 0 && $user = get_userdata($comment->user_id) ) {
425 // For all registered users, 'byuser'
426 $classes[] = 'byuser';
427 $classes[] = 'comment-author-' . sanitize_html_class($user->user_nicename, $comment->user_id);
428 // For comment authors who are the author of the post
429 if ( $post = get_post($post_id) ) {
430 if ( $comment->user_id === $post->post_author )
431 $classes[] = 'bypostauthor';
435 if ( empty($comment_alt) )
437 if ( empty($comment_depth) )
439 if ( empty($comment_thread_alt) )
440 $comment_thread_alt = 0;
442 if ( $comment_alt % 2 ) {
451 // Alt for top-level comments
452 if ( 1 == $comment_depth ) {
453 if ( $comment_thread_alt % 2 ) {
454 $classes[] = 'thread-odd';
455 $classes[] = 'thread-alt';
457 $classes[] = 'thread-even';
459 $comment_thread_alt++;
462 $classes[] = "depth-$comment_depth";
464 if ( !empty($class) ) {
465 if ( !is_array( $class ) )
466 $class = preg_split('#\s+#', $class);
467 $classes = array_merge($classes, $class);
470 $classes = array_map('esc_attr', $classes);
473 * Filter the returned CSS classes for the current comment.
477 * @param array $classes An array of comment classes.
478 * @param string $class A comma-separated list of additional classes added to the list.
479 * @param int $comment_id The comment id.
480 * @param object $comment The comment
481 * @param int|WP_Post $post_id The post ID or WP_Post object.
483 return apply_filters( 'comment_class', $classes, $class, $comment_id, $comment, $post_id );
487 * Retrieve the comment date of the current comment.
491 * @param string $d Optional. The format of the date. Default user's setting.
492 * @param int $comment_ID ID of the comment for which to get the date. Default current comment.
493 * @return string The comment's date.
495 function get_comment_date( $d = '', $comment_ID = 0 ) {
496 $comment = get_comment( $comment_ID );
498 $date = mysql2date(get_option('date_format'), $comment->comment_date);
500 $date = mysql2date($d, $comment->comment_date);
502 * Filter the returned comment date.
506 * @param string|int $date Formatted date string or Unix timestamp.
507 * @param string $d The format of the date.
508 * @param object $comment The comment object.
510 return apply_filters( 'get_comment_date', $date, $d, $comment );
514 * Display the comment date of the current comment.
518 * @param string $d Optional. The format of the date. Default user's settings.
519 * @param int $comment_ID ID of the comment for which to print the date. Default current comment.
521 function comment_date( $d = '', $comment_ID = 0 ) {
522 echo get_comment_date( $d, $comment_ID );
526 * Retrieve the excerpt of the current comment.
528 * Will cut each word and only output the first 20 words with '…' at the end.
529 * If the word count is less than 20, then no truncating is done and no '…'
534 * @param int $comment_ID ID of the comment for which to get the excerpt.
535 * Default current comment.
536 * @return string The maybe truncated comment with 20 words or less.
538 function get_comment_excerpt( $comment_ID = 0 ) {
539 $comment = get_comment( $comment_ID );
540 $comment_text = strip_tags($comment->comment_content);
541 $blah = explode(' ', $comment_text);
543 if (count($blah) > 20) {
552 for ($i=0; $i<$k; $i++) {
553 $excerpt .= $blah[$i] . ' ';
555 $excerpt .= ($use_dotdotdot) ? '…' : '';
558 * Filter the retrieved comment excerpt.
561 * @since 4.1.0 The `$comment_ID` and `$comment` parameters were added.
563 * @param string $excerpt The comment excerpt text.
564 * @param int $comment_ID The comment ID.
565 * @param object $comment The comment object.
567 return apply_filters( 'get_comment_excerpt', $excerpt, $comment_ID, $comment );
571 * Display the excerpt of the current comment.
575 * @param int $comment_ID ID of the comment for which to print the excerpt.
576 * Default current comment.
578 function comment_excerpt( $comment_ID = 0 ) {
579 $comment_excerpt = get_comment_excerpt($comment_ID);
582 * Filter the comment excerpt for display.
585 * @since 4.1.0 The `$comment_ID` parameter was added.
587 * @param string $comment_excerpt The comment excerpt text.
588 * @param int $comment_ID The comment ID.
590 echo apply_filters( 'comment_excerpt', $comment_excerpt, $comment_ID );
594 * Retrieve the comment id of the current comment.
598 * @return int The comment ID.
600 function get_comment_ID() {
604 * Filter the returned comment ID.
607 * @since 4.1.0 The `$comment_ID` parameter was added.
609 * @param int $comment_ID The current comment ID.
610 * @param object $comment The comment object.
612 return apply_filters( 'get_comment_ID', $comment->comment_ID, $comment );
616 * Display the comment id of the current comment.
620 function comment_ID() {
621 echo get_comment_ID();
625 * Retrieve the link to a given comment.
629 * @see get_page_of_comment()
631 * @param mixed $comment Comment to retrieve. Default current comment.
632 * @param array $args Optional. An array of arguments to override the defaults.
633 * @return string The permalink to the given comment.
635 function get_comment_link( $comment = null, $args = array() ) {
636 global $wp_rewrite, $in_comment_loop;
638 $comment = get_comment($comment);
641 if ( ! is_array( $args ) ) {
642 $args = array( 'page' => $args );
645 $defaults = array( 'type' => 'all', 'page' => '', 'per_page' => '', 'max_depth' => '' );
646 $args = wp_parse_args( $args, $defaults );
648 if ( '' === $args['per_page'] && get_option('page_comments') )
649 $args['per_page'] = get_option('comments_per_page');
651 if ( empty($args['per_page']) ) {
652 $args['per_page'] = 0;
656 if ( $args['per_page'] ) {
657 if ( '' == $args['page'] )
658 $args['page'] = ( !empty($in_comment_loop) ) ? get_query_var('cpage') : get_page_of_comment( $comment->comment_ID, $args );
660 if ( $wp_rewrite->using_permalinks() )
661 $link = user_trailingslashit( trailingslashit( get_permalink( $comment->comment_post_ID ) ) . 'comment-page-' . $args['page'], 'comment' );
663 $link = add_query_arg( 'cpage', $args['page'], get_permalink( $comment->comment_post_ID ) );
665 $link = get_permalink( $comment->comment_post_ID );
668 $link = $link . '#comment-' . $comment->comment_ID;
670 * Filter the returned single comment permalink.
674 * @see get_page_of_comment()
676 * @param string $link The comment permalink with '#comment-$id' appended.
677 * @param object $comment The current comment object.
678 * @param array $args An array of arguments to override the defaults.
680 return apply_filters( 'get_comment_link', $link, $comment, $args );
684 * Retrieve the link to the current post comments.
688 * @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is global $post.
689 * @return string The link to the comments.
691 function get_comments_link( $post_id = 0 ) {
692 $comments_link = get_permalink( $post_id ) . '#comments';
694 * Filter the returned post comments permalink.
698 * @param string $comments_link Post comments permalink with '#comments' appended.
699 * @param int|WP_Post $post_id Post ID or WP_Post object.
701 return apply_filters( 'get_comments_link', $comments_link, $post_id );
705 * Display the link to the current post comments.
709 * @param string $deprecated Not Used.
710 * @param string $deprecated_2 Not Used.
712 function comments_link( $deprecated = '', $deprecated_2 = '' ) {
713 if ( !empty( $deprecated ) )
714 _deprecated_argument( __FUNCTION__, '0.72' );
715 if ( !empty( $deprecated_2 ) )
716 _deprecated_argument( __FUNCTION__, '1.3' );
717 echo esc_url( get_comments_link() );
721 * Retrieve the amount of comments a post has.
725 * @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is global $post.
726 * @return int The number of comments a post has.
728 function get_comments_number( $post_id = 0 ) {
729 $post = get_post( $post_id );
734 $count = $post->comment_count;
735 $post_id = $post->ID;
739 * Filter the returned comment count for a post.
743 * @param int $count Number of comments a post has.
744 * @param int $post_id Post ID.
746 return apply_filters( 'get_comments_number', $count, $post_id );
750 * Display the language string for the number of comments the current post has.
754 * @param string $zero Optional. Text for no comments. Default false.
755 * @param string $one Optional. Text for one comment. Default false.
756 * @param string $more Optional. Text for more than one comment. Default false.
757 * @param string $deprecated Not used.
759 function comments_number( $zero = false, $one = false, $more = false, $deprecated = '' ) {
760 if ( ! empty( $deprecated ) ) {
761 _deprecated_argument( __FUNCTION__, '1.3' );
763 echo get_comments_number_text( $zero, $one, $more );
767 * Display the language string for the number of comments the current post has.
771 * @param string $zero Optional. Text for no comments. Default false.
772 * @param string $one Optional. Text for one comment. Default false.
773 * @param string $more Optional. Text for more than one comment. Default false.
775 function get_comments_number_text( $zero = false, $one = false, $more = false ) {
776 $number = get_comments_number();
779 $output = str_replace( '%', number_format_i18n( $number ), ( false === $more ) ? __( '% Comments' ) : $more );
780 } elseif ( $number == 0 ) {
781 $output = ( false === $zero ) ? __( 'No Comments' ) : $zero;
782 } else { // must be one
783 $output = ( false === $one ) ? __( '1 Comment' ) : $one;
786 * Filter the comments count for display.
792 * @param string $output A translatable string formatted based on whether the count
793 * is equal to 0, 1, or 1+.
794 * @param int $number The number of post comments.
796 return apply_filters( 'comments_number', $output, $number );
800 * Retrieve the text of the current comment.
804 * @see Walker_Comment::comment()
806 * @param int $comment_ID ID of the comment for which to get the text. Default current comment.
807 * @param array $args Optional. An array of arguments. Default empty.
808 * @return string The comment content.
810 function get_comment_text( $comment_ID = 0, $args = array() ) {
811 $comment = get_comment( $comment_ID );
814 * Filter the text of a comment.
818 * @see Walker_Comment::comment()
820 * @param string $comment_content Text of the comment.
821 * @param object $comment The comment object.
822 * @param array $args An array of arguments.
824 return apply_filters( 'get_comment_text', $comment->comment_content, $comment, $args );
828 * Display the text of the current comment.
832 * @see Walker_Comment::comment()
834 * @param int $comment_ID ID of the comment for which to print the text. Default 0.
835 * @param array $args Optional. An array of arguments. Default empty array. Default empty.
837 function comment_text( $comment_ID = 0, $args = array() ) {
838 $comment = get_comment( $comment_ID );
840 $comment_text = get_comment_text( $comment_ID , $args );
842 * Filter the text of a comment to be displayed.
846 * @see Walker_Comment::comment()
848 * @param string $comment_text Text of the current comment.
849 * @param object $comment The comment object.
850 * @param array $args An array of arguments.
852 echo apply_filters( 'comment_text', $comment_text, $comment, $args );
856 * Retrieve the comment time of the current comment.
860 * @param string $d Optional. The format of the time. Default user's settings.
861 * @param bool $gmt Optional. Whether to use the GMT date. Default false.
862 * @param bool $translate Optional. Whether to translate the time (for use in feeds).
864 * @return string The formatted time.
866 function get_comment_time( $d = '', $gmt = false, $translate = true ) {
868 $comment_date = $gmt ? $comment->comment_date_gmt : $comment->comment_date;
870 $date = mysql2date(get_option('time_format'), $comment_date, $translate);
872 $date = mysql2date($d, $comment_date, $translate);
875 * Filter the returned comment time.
879 * @param string|int $date The comment time, formatted as a date string or Unix timestamp.
880 * @param string $d Date format.
881 * @param bool $gmt Whether the GMT date is in use.
882 * @param bool $translate Whether the time is translated.
883 * @param object $comment The comment object.
885 return apply_filters( 'get_comment_time', $date, $d, $gmt, $translate, $comment );
889 * Display the comment time of the current comment.
893 * @param string $d Optional. The format of the time. Default user's settings.
895 function comment_time( $d = '' ) {
896 echo get_comment_time($d);
900 * Retrieve the comment type of the current comment.
904 * @param int $comment_ID ID of the comment for which to get the type. Default current comment.
905 * @return string The comment type.
907 function get_comment_type( $comment_ID = 0 ) {
908 $comment = get_comment( $comment_ID );
909 if ( '' == $comment->comment_type )
910 $comment->comment_type = 'comment';
913 * Filter the returned comment type.
916 * @since 4.1.0 The `$comment_ID` and `$comment` parameters were added.
918 * @param string $comment_type The type of comment, such as 'comment', 'pingback', or 'trackback'.
919 * @param int $comment_ID The comment ID.
920 * @param object $comment The comment object.
922 return apply_filters( 'get_comment_type', $comment->comment_type, $comment_ID, $comment );
926 * Display the comment type of the current comment.
930 * @param string $commenttxt Optional. String to display for comment type. Default false.
931 * @param string $trackbacktxt Optional. String to display for trackback type. Default false.
932 * @param string $pingbacktxt Optional. String to display for pingback type. Default false.
934 function comment_type( $commenttxt = false, $trackbacktxt = false, $pingbacktxt = false ) {
935 if ( false === $commenttxt ) $commenttxt = _x( 'Comment', 'noun' );
936 if ( false === $trackbacktxt ) $trackbacktxt = __( 'Trackback' );
937 if ( false === $pingbacktxt ) $pingbacktxt = __( 'Pingback' );
938 $type = get_comment_type();
952 * Retrieve The current post's trackback URL.
954 * There is a check to see if permalink's have been enabled and if so, will
955 * retrieve the pretty path. If permalinks weren't enabled, the ID of the
956 * current post is used and appended to the correct page to go to.
960 * @return string The trackback URL after being filtered.
962 function get_trackback_url() {
963 if ( '' != get_option('permalink_structure') )
964 $tb_url = trailingslashit(get_permalink()) . user_trailingslashit('trackback', 'single_trackback');
966 $tb_url = get_option('siteurl') . '/wp-trackback.php?p=' . get_the_ID();
969 * Filter the returned trackback URL.
973 * @param string $tb_url The trackback URL.
975 return apply_filters( 'trackback_url', $tb_url );
979 * Display the current post's trackback URL.
983 * @param bool $deprecated_echo Not used.
984 * @return void|string Should only be used to echo the trackback URL, use get_trackback_url()
985 * for the result instead.
987 function trackback_url( $deprecated_echo = true ) {
988 if ( $deprecated_echo !== true )
989 _deprecated_argument( __FUNCTION__, '2.5', __('Use <code>get_trackback_url()</code> instead if you do not want the value echoed.') );
990 if ( $deprecated_echo )
991 echo get_trackback_url();
993 return get_trackback_url();
997 * Generate and display the RDF for the trackback information of current post.
999 * Deprecated in 3.0.0, and restored in 3.0.1.
1003 * @param int $deprecated Not used (Was $timezone = 0).
1005 function trackback_rdf( $deprecated = '' ) {
1006 if ( ! empty( $deprecated ) ) {
1007 _deprecated_argument( __FUNCTION__, '2.5' );
1010 if ( isset( $_SERVER['HTTP_USER_AGENT'] ) && false !== stripos( $_SERVER['HTTP_USER_AGENT'], 'W3C_Validator' ) ) {
1014 echo '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
1015 xmlns:dc="http://purl.org/dc/elements/1.1/"
1016 xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
1017 <rdf:Description rdf:about="';
1020 echo ' dc:identifier="';
1023 echo ' dc:title="'.str_replace('--', '--', wptexturize(strip_tags(get_the_title()))).'"'."\n";
1024 echo ' trackback:ping="'.get_trackback_url().'"'." />\n";
1029 * Whether the current post is open for comments.
1033 * @param int|WP_Post $post_id Post ID or WP_Post object. Default current post.
1034 * @return bool True if the comments are open.
1036 function comments_open( $post_id = null ) {
1038 $_post = get_post($post_id);
1040 $open = ( 'open' == $_post->comment_status );
1043 * Filter whether the current post is open for comments.
1047 * @param bool $open Whether the current post is open for comments.
1048 * @param int|WP_Post $post_id The post ID or WP_Post object.
1050 return apply_filters( 'comments_open', $open, $post_id );
1054 * Whether the current post is open for pings.
1058 * @param int|WP_Post $post_id Post ID or WP_Post object. Default current post.
1059 * @return bool True if pings are accepted
1061 function pings_open( $post_id = null ) {
1063 $_post = get_post($post_id);
1065 $open = ( 'open' == $_post->ping_status );
1068 * Filter whether the current post is open for pings.
1072 * @param bool $open Whether the current post is open for pings.
1073 * @param int|WP_Post $post_id The post ID or WP_Post object.
1075 return apply_filters( 'pings_open', $open, $post_id );
1079 * Display form token for unfiltered comments.
1081 * Will only display nonce token if the current user has permissions for
1082 * unfiltered html. Won't display the token for other users.
1084 * The function was backported to 2.0.10 and was added to versions 2.1.3 and
1085 * above. Does not exist in versions prior to 2.0.10 in the 2.0 branch and in
1086 * the 2.1 branch, prior to 2.1.3. Technically added in 2.2.0.
1088 * Backported to 2.0.10.
1092 function wp_comment_form_unfiltered_html_nonce() {
1094 $post_id = $post ? $post->ID : 0;
1096 if ( current_user_can( 'unfiltered_html' ) ) {
1097 wp_nonce_field( 'unfiltered-html-comment_' . $post_id, '_wp_unfiltered_html_comment_disabled', false );
1098 echo "<script>(function(){if(window===window.parent){document.getElementById('_wp_unfiltered_html_comment_disabled').name='_wp_unfiltered_html_comment';}})();</script>\n";
1103 * Load the comment template specified in $file.
1105 * Will not display the comments template if not on single post or page, or if
1106 * the post does not have comments.
1108 * Uses the WordPress database object to query for the comments. The comments
1109 * are passed through the 'comments_array' filter hook with the list of comments
1110 * and the post ID respectively.
1112 * The $file path is passed through a filter hook called, 'comments_template'
1113 * which includes the TEMPLATEPATH and $file combined. Tries the $filtered path
1114 * first and if it fails it will require the default comment template from the
1115 * default theme. If either does not exist, then the WordPress process will be
1116 * halted. It is advised for that reason, that the default theme is not deleted.
1118 * @todo Document globals
1119 * @uses $withcomments Will not try to get the comments if the post has none.
1123 * @param string $file Optional. The file to load. Default '/comments.php'.
1124 * @param bool $separate_comments Optional. Whether to separate the comments by comment type.
1126 * @return null Returns null if no comments appear.
1128 function comments_template( $file = '/comments.php', $separate_comments = false ) {
1129 global $wp_query, $withcomments, $post, $wpdb, $id, $comment, $user_login, $user_ID, $user_identity, $overridden_cpage;
1131 if ( !(is_single() || is_page() || $withcomments) || empty($post) )
1135 $file = '/comments.php';
1137 $req = get_option('require_name_email');
1140 * Comment author information fetched from the comment cookies.
1141 * Uuses wp_get_current_commenter().
1143 $commenter = wp_get_current_commenter();
1146 * The name of the current comment author escaped for use in attributes.
1147 * Escaped by sanitize_comment_cookies().
1149 $comment_author = $commenter['comment_author'];
1152 * The email address of the current comment author escaped for use in attributes.
1153 * Escaped by sanitize_comment_cookies().
1155 $comment_author_email = $commenter['comment_author_email'];
1158 * The url of the current comment author escaped for use in attributes.
1160 $comment_author_url = esc_url($commenter['comment_author_url']);
1162 $comment_args = array(
1164 'orderby' => 'comment_date_gmt',
1165 'status' => 'approve',
1166 'post_id' => $post->ID,
1170 $comment_args['include_unapproved'] = array( $user_ID );
1171 } else if ( ! empty( $comment_author_email ) ) {
1172 $comment_args['include_unapproved'] = array( $comment_author_email );
1175 $comments = get_comments( $comment_args );
1178 * Filter the comments array.
1182 * @param array $comments Array of comments supplied to the comments template.
1183 * @param int $post_ID Post ID.
1185 $wp_query->comments = apply_filters( 'comments_array', $comments, $post->ID );
1186 $comments = &$wp_query->comments;
1187 $wp_query->comment_count = count($wp_query->comments);
1188 update_comment_cache($wp_query->comments);
1190 if ( $separate_comments ) {
1191 $wp_query->comments_by_type = separate_comments($comments);
1192 $comments_by_type = &$wp_query->comments_by_type;
1195 $overridden_cpage = false;
1196 if ( '' == get_query_var('cpage') && get_option('page_comments') ) {
1197 set_query_var( 'cpage', 'newest' == get_option('default_comments_page') ? get_comment_pages_count() : 1 );
1198 $overridden_cpage = true;
1201 if ( !defined('COMMENTS_TEMPLATE') )
1202 define('COMMENTS_TEMPLATE', true);
1204 $theme_template = STYLESHEETPATH . $file;
1206 * Filter the path to the theme template file used for the comments template.
1210 * @param string $theme_template The path to the theme template file.
1212 $include = apply_filters( 'comments_template', $theme_template );
1213 if ( file_exists( $include ) )
1214 require( $include );
1215 elseif ( file_exists( TEMPLATEPATH . $file ) )
1216 require( TEMPLATEPATH . $file );
1217 else // Backward compat code will be removed in a future release
1218 require( ABSPATH . WPINC . '/theme-compat/comments.php');
1222 * Display the JS popup script to show a comment.
1224 * If the $file parameter is empty, then the home page is assumed. The defaults
1225 * for the window are 400px by 400px.
1227 * For the comment link popup to work, this function has to be called or the
1228 * normal comment link will be assumed.
1230 * @global string $wpcommentspopupfile The URL to use for the popup window.
1231 * @global int $wpcommentsjavascript Whether to use JavaScript. Set when function is called.
1235 * @param int $width Optional. The width of the popup window. Default 400.
1236 * @param int $height Optional. The height of the popup window. Default 400.
1237 * @param string $file Optional. Sets the location of the popup window.
1239 function comments_popup_script( $width = 400, $height = 400, $file = '' ) {
1240 global $wpcommentspopupfile, $wpcommentsjavascript;
1242 if (empty ($file)) {
1243 $wpcommentspopupfile = ''; // Use the index.
1245 $wpcommentspopupfile = $file;
1248 $wpcommentsjavascript = 1;
1249 $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";
1254 * Displays the link to the comments popup window for the current post ID.
1256 * Is not meant to be displayed on single posts and pages. Should be used
1257 * on the lists of posts
1259 * @global string $wpcommentspopupfile The URL to use for the popup window.
1260 * @global int $wpcommentsjavascript Whether to use JavaScript. Set when function is called.
1264 * @param string $zero Optional. String to display when no comments. Default false.
1265 * @param string $one Optional. String to display when only one comment is available.
1267 * @param string $more Optional. String to display when there are more than one comment.
1269 * @param string $css_class Optional. CSS class to use for comments. Default empty.
1270 * @param string $none Optional. String to display when comments have been turned off.
1272 * @return null Returns null on single posts and pages.
1274 function comments_popup_link( $zero = false, $one = false, $more = false, $css_class = '', $none = false ) {
1275 global $wpcommentspopupfile, $wpcommentsjavascript;
1279 if ( false === $zero ) $zero = __( 'No Comments' );
1280 if ( false === $one ) $one = __( '1 Comment' );
1281 if ( false === $more ) $more = __( '% Comments' );
1282 if ( false === $none ) $none = __( 'Comments Off' );
1284 $number = get_comments_number( $id );
1286 if ( 0 == $number && !comments_open() && !pings_open() ) {
1287 echo '<span' . ((!empty($css_class)) ? ' class="' . esc_attr( $css_class ) . '"' : '') . '>' . $none . '</span>';
1291 if ( post_password_required() ) {
1292 echo __('Enter your password to view comments.');
1297 if ( $wpcommentsjavascript ) {
1298 if ( empty( $wpcommentspopupfile ) )
1301 $home = get_option('siteurl');
1302 echo $home . '/' . $wpcommentspopupfile . '?comments_popup=' . $id;
1303 echo '" onclick="wpopen(this.href); return false"';
1304 } else { // if comments_popup_script() is not in the template, display simple comment link
1306 echo get_permalink() . '#respond';
1312 if ( !empty( $css_class ) ) {
1313 echo ' class="'.$css_class.'" ';
1315 $title = the_title_attribute( array('echo' => 0 ) );
1319 * Filter the comments popup link attributes for display.
1323 * @param string $attributes The comments popup link attributes. Default empty.
1325 echo apply_filters( 'comments_popup_link_attributes', $attributes );
1327 echo ' title="' . esc_attr( sprintf( __('Comment on %s'), $title ) ) . '">';
1328 comments_number( $zero, $one, $more );
1333 * Retrieve HTML content for reply to comment link.
1337 * @param array $args {
1338 * Optional. Override default arguments.
1340 * @type string $add_below The first part of the selector used to identify the comment to respond below.
1341 * The resulting value is passed as the first parameter to addComment.moveForm(),
1342 * concatenated as $add_below-$comment->comment_ID. Default 'comment'.
1343 * @type string $respond_id The selector identifying the responding comment. Passed as the third parameter
1344 * to addComment.moveForm(), and appended to the link URL as a hash value.
1345 * Default 'respond'.
1346 * @type string $reply_text The text of the Reply link. Default 'Reply'.
1347 * @type string $login_text The text of the link to reply if logged out. Default 'Log in to Reply'.
1348 * @type int $depth' The depth of the new comment. Must be greater than 0 and less than the value
1349 * of the 'thread_comments_depth' option set in Settings > Discussion. Default 0.
1350 * @type string $before The text or HTML to add before the reply link. Default empty.
1351 * @type string $after The text or HTML to add after the reply link. Default empty.
1353 * @param int $comment Comment being replied to. Default current comment.
1354 * @param int|WP_Post $post Post ID or WP_Post object the comment is going to be displayed on.
1355 * Default current post.
1356 * @return null|false|string Link to show comment form, if successful. False, if comments are closed.
1358 function get_comment_reply_link( $args = array(), $comment = null, $post = null ) {
1361 'add_below' => 'comment',
1362 'respond_id' => 'respond',
1363 'reply_text' => __( 'Reply' ),
1364 'reply_to_text' => __( 'Reply to %s' ),
1365 'login_text' => __( 'Log in to Reply' ),
1371 $args = wp_parse_args( $args, $defaults );
1373 if ( 0 == $args['depth'] || $args['max_depth'] <= $args['depth'] ) {
1377 $comment = get_comment( $comment );
1379 if ( empty( $post ) ) {
1380 $post = $comment->comment_post_ID;
1383 $post = get_post( $post );
1385 if ( ! comments_open( $post->ID ) ) {
1390 * Filter the comment reply link arguments.
1394 * @param array $args Comment reply link arguments. See {@see get_comment_reply_link()}
1395 * for more information on accepted arguments.
1396 * @param object $comment The object of the comment being replied to.
1397 * @param WP_Post $post The {@see WP_Post} object.
1399 $args = apply_filters( 'comment_reply_link_args', $args, $comment, $post );
1401 if ( get_option( 'comment_registration' ) && ! is_user_logged_in() ) {
1402 $link = sprintf( '<a rel="nofollow" class="comment-reply-login" href="%s">%s</a>',
1403 esc_url( wp_login_url( get_permalink() ) ),
1407 $onclick = sprintf( 'return addComment.moveForm( "%1$s-%2$s", "%2$s", "%3$s", "%4$s" )',
1408 $args['add_below'], $comment->comment_ID, $args['respond_id'], $post->ID
1411 $link = sprintf( "<a class='comment-reply-link' href='%s' onclick='%s' aria-label='%s'>%s</a>",
1412 esc_url( add_query_arg( 'replytocom', $comment->comment_ID ) ) . "#" . $args['respond_id'],
1414 esc_attr( sprintf( $args['reply_to_text'], $comment->comment_author ) ),
1419 * Filter the comment reply link.
1423 * @param string $link The HTML markup for the comment reply link.
1424 * @param array $args An array of arguments overriding the defaults.
1425 * @param object $comment The object of the comment being replied.
1426 * @param WP_Post $post The WP_Post object.
1428 return apply_filters( 'comment_reply_link', $args['before'] . $link . $args['after'], $args, $comment, $post );
1432 * Displays the HTML content for reply to comment link.
1436 * @see get_comment_reply_link()
1438 * @param array $args Optional. Override default options.
1439 * @param int $comment Comment being replied to. Default current comment.
1440 * @param int|WP_Post $post Post ID or WP_Post object the comment is going to be displayed on.
1441 * Default current post.
1442 * @return mixed Link to show comment form, if successful. False, if comments are closed.
1444 function comment_reply_link($args = array(), $comment = null, $post = null) {
1445 echo get_comment_reply_link($args, $comment, $post);
1449 * Retrieve HTML content for reply to post link.
1453 * @param array $args {
1454 * Optional. Override default arguments.
1456 * @type string $add_below The first part of the selector used to identify the comment to respond below.
1457 * The resulting value is passed as the first parameter to addComment.moveForm(),
1458 * concatenated as $add_below-$comment->comment_ID. Default is 'post'.
1459 * @type string $respond_id The selector identifying the responding comment. Passed as the third parameter
1460 * to addComment.moveForm(), and appended to the link URL as a hash value.
1461 * Default 'respond'.
1462 * @type string $reply_text Text of the Reply link. Default is 'Leave a Comment'.
1463 * @type string $login_text Text of the link to reply if logged out. Default is 'Log in to leave a Comment'.
1464 * @type string $before Text or HTML to add before the reply link. Default empty.
1465 * @type string $after Text or HTML to add after the reply link. Default empty.
1467 * @param int|WP_Post $post Optional. Post ID or WP_Post object the comment is going to be displayed on.
1468 * Default current post.
1469 * @return false|null|string Link to show comment form, if successful. False, if comments are closed.
1471 function get_post_reply_link($args = array(), $post = null) {
1473 'add_below' => 'post',
1474 'respond_id' => 'respond',
1475 'reply_text' => __('Leave a Comment'),
1476 'login_text' => __('Log in to leave a Comment'),
1481 $args = wp_parse_args($args, $defaults);
1483 $post = get_post($post);
1485 if ( ! comments_open( $post->ID ) ) {
1489 if ( get_option('comment_registration') && ! is_user_logged_in() ) {
1490 $link = sprintf( '<a rel="nofollow" href="%s">%s</a>',
1491 wp_login_url( get_permalink() ),
1495 $onclick = sprintf( 'return addComment.moveForm( "%1$s-%2$s", "0", "%3$s", "%2$s" )',
1496 $args['add_below'], $post->ID, $args['respond_id']
1499 $link = sprintf( "<a rel='nofollow' class='comment-reply-link' href='%s' onclick='%s'>%s</a>",
1500 get_permalink( $post->ID ) . '#' . $args['respond_id'],
1505 $formatted_link = $args['before'] . $link . $args['after'];
1507 * Filter the formatted post comments link HTML.
1511 * @param string $formatted The HTML-formatted post comments link.
1512 * @param int|WP_Post $post The post ID or WP_Post object.
1514 return apply_filters( 'post_comments_link', $formatted_link, $post );
1518 * Displays the HTML content for reply to post link.
1522 * @see get_post_reply_link()
1524 * @param array $args Optional. Override default options,
1525 * @param int|WP_Post $post Post ID or WP_Post object the comment is going to be displayed on.
1526 * Default current post.
1527 * @return string|bool|null Link to show comment form, if successful. False, if comments are closed.
1529 function post_reply_link($args = array(), $post = null) {
1530 echo get_post_reply_link($args, $post);
1534 * Retrieve HTML content for cancel comment reply link.
1538 * @param string $text Optional. Text to display for cancel reply link. Default empty.
1540 function get_cancel_comment_reply_link( $text = '' ) {
1542 $text = __('Click here to cancel reply.');
1544 $style = isset($_GET['replytocom']) ? '' : ' style="display:none;"';
1545 $link = esc_html( remove_query_arg('replytocom') ) . '#respond';
1547 $formatted_link = '<a rel="nofollow" id="cancel-comment-reply-link" href="' . $link . '"' . $style . '>' . $text . '</a>';
1549 * Filter the cancel comment reply link HTML.
1553 * @param string $formatted_link The HTML-formatted cancel comment reply link.
1554 * @param string $link Cancel comment reply link URL.
1555 * @param string $text Cancel comment reply link text.
1557 return apply_filters( 'cancel_comment_reply_link', $formatted_link, $link, $text );
1561 * Display HTML content for cancel comment reply link.
1565 * @param string $text Optional. Text to display for cancel reply link. Default empty.
1567 function cancel_comment_reply_link( $text = '' ) {
1568 echo get_cancel_comment_reply_link($text);
1572 * Retrieve hidden input HTML for replying to comments.
1576 * @param int $id Optional. Post ID. Default current post ID.
1577 * @return string Hidden input HTML for replying to comments
1579 function get_comment_id_fields( $id = 0 ) {
1583 $replytoid = isset($_GET['replytocom']) ? (int) $_GET['replytocom'] : 0;
1584 $result = "<input type='hidden' name='comment_post_ID' value='$id' id='comment_post_ID' />\n";
1585 $result .= "<input type='hidden' name='comment_parent' id='comment_parent' value='$replytoid' />\n";
1588 * Filter the returned comment id fields.
1592 * @param string $result The HTML-formatted hidden id field comment elements.
1593 * @param int $id The post ID.
1594 * @param int $replytoid The id of the comment being replied to.
1596 return apply_filters( 'comment_id_fields', $result, $id, $replytoid );
1600 * Output hidden input HTML for replying to comments.
1604 * @param int $id Optional. Post ID. Default current post ID.
1606 function comment_id_fields( $id = 0 ) {
1607 echo get_comment_id_fields( $id );
1611 * Display text based on comment reply status.
1613 * Only affects users with JavaScript disabled.
1617 * @param string $noreplytext Optional. Text to display when not replying to a comment.
1619 * @param string $replytext Optional. Text to display when replying to a comment.
1620 * Default false. Accepts "%s" for the author of the comment
1622 * @param string $linktoparent Optional. Boolean to control making the author's name a link
1623 * to their comment. Default true.
1625 function comment_form_title( $noreplytext = false, $replytext = false, $linktoparent = true ) {
1628 if ( false === $noreplytext ) $noreplytext = __( 'Leave a Reply' );
1629 if ( false === $replytext ) $replytext = __( 'Leave a Reply to %s' );
1631 $replytoid = isset($_GET['replytocom']) ? (int) $_GET['replytocom'] : 0;
1633 if ( 0 == $replytoid )
1636 $comment = get_comment($replytoid);
1637 $author = ( $linktoparent ) ? '<a href="#comment-' . get_comment_ID() . '">' . get_comment_author() . '</a>' : get_comment_author();
1638 printf( $replytext, $author );
1643 * HTML comment list class.
1648 class Walker_Comment extends Walker {
1650 * What the class handles.
1652 * @see Walker::$tree_type
1657 public $tree_type = 'comment';
1662 * @see Walker::$db_fields
1667 public $db_fields = array ('parent' => 'comment_parent', 'id' => 'comment_ID');
1670 * Start the list before the elements are added.
1672 * @see Walker::start_lvl()
1676 * @param string $output Passed by reference. Used to append additional content.
1677 * @param int $depth Depth of comment.
1678 * @param array $args Uses 'style' argument for type of HTML list.
1680 public function start_lvl( &$output, $depth = 0, $args = array() ) {
1681 $GLOBALS['comment_depth'] = $depth + 1;
1683 switch ( $args['style'] ) {
1687 $output .= '<ol class="children">' . "\n";
1691 $output .= '<ul class="children">' . "\n";
1697 * End the list of items after the elements are added.
1699 * @see Walker::end_lvl()
1703 * @param string $output Passed by reference. Used to append additional content.
1704 * @param int $depth Depth of comment.
1705 * @param array $args Will only append content if style argument value is 'ol' or 'ul'.
1707 public function end_lvl( &$output, $depth = 0, $args = array() ) {
1708 $GLOBALS['comment_depth'] = $depth + 1;
1710 switch ( $args['style'] ) {
1714 $output .= "</ol><!-- .children -->\n";
1718 $output .= "</ul><!-- .children -->\n";
1724 * Traverse elements to create list from elements.
1726 * This function is designed to enhance Walker::display_element() to
1727 * display children of higher nesting levels than selected inline on
1728 * the highest depth level displayed. This prevents them being orphaned
1729 * at the end of the comment list.
1731 * Example: max_depth = 2, with 5 levels of nested content.
1742 * @see Walker::display_element()
1743 * @see wp_list_comments()
1747 * @param object $element Data object.
1748 * @param array $children_elements List of elements to continue traversing.
1749 * @param int $max_depth Max depth to traverse.
1750 * @param int $depth Depth of current element.
1751 * @param array $args An array of arguments.
1752 * @param string $output Passed by reference. Used to append additional content.
1753 * @return null Null on failure with no changes to parameters.
1755 public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) {
1760 $id_field = $this->db_fields['id'];
1761 $id = $element->$id_field;
1763 parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
1765 // If we're at the max depth, and the current element still has children, loop over those and display them at this level
1766 // This is to prevent them being orphaned to the end of the list.
1767 if ( $max_depth <= $depth + 1 && isset( $children_elements[$id]) ) {
1768 foreach ( $children_elements[ $id ] as $child )
1769 $this->display_element( $child, $children_elements, $max_depth, $depth, $args, $output );
1771 unset( $children_elements[ $id ] );
1777 * Start the element output.
1781 * @see Walker::start_el()
1782 * @see wp_list_comments()
1784 * @param string $output Passed by reference. Used to append additional content.
1785 * @param object $comment Comment data object.
1786 * @param int $depth Depth of comment in reference to parents.
1787 * @param array $args An array of arguments.
1789 public function start_el( &$output, $comment, $depth = 0, $args = array(), $id = 0 ) {
1791 $GLOBALS['comment_depth'] = $depth;
1792 $GLOBALS['comment'] = $comment;
1794 if ( !empty( $args['callback'] ) ) {
1796 call_user_func( $args['callback'], $comment, $args, $depth );
1797 $output .= ob_get_clean();
1801 if ( ( 'pingback' == $comment->comment_type || 'trackback' == $comment->comment_type ) && $args['short_ping'] ) {
1803 $this->ping( $comment, $depth, $args );
1804 $output .= ob_get_clean();
1805 } elseif ( 'html5' === $args['format'] ) {
1807 $this->html5_comment( $comment, $depth, $args );
1808 $output .= ob_get_clean();
1811 $this->comment( $comment, $depth, $args );
1812 $output .= ob_get_clean();
1817 * Ends the element output, if needed.
1821 * @see Walker::end_el()
1822 * @see wp_list_comments()
1824 * @param string $output Passed by reference. Used to append additional content.
1825 * @param object $comment The comment object. Default current comment.
1826 * @param int $depth Depth of comment.
1827 * @param array $args An array of arguments.
1829 public function end_el( &$output, $comment, $depth = 0, $args = array() ) {
1830 if ( !empty( $args['end-callback'] ) ) {
1832 call_user_func( $args['end-callback'], $comment, $args, $depth );
1833 $output .= ob_get_clean();
1836 if ( 'div' == $args['style'] )
1837 $output .= "</div><!-- #comment-## -->\n";
1839 $output .= "</li><!-- #comment-## -->\n";
1843 * Output a pingback comment.
1848 * @see wp_list_comments()
1850 * @param object $comment The comment object.
1851 * @param int $depth Depth of comment.
1852 * @param array $args An array of arguments.
1854 protected function ping( $comment, $depth, $args ) {
1855 $tag = ( 'div' == $args['style'] ) ? 'div' : 'li';
1857 <<?php echo $tag; ?> id="comment-<?php comment_ID(); ?>" <?php comment_class(); ?>>
1858 <div class="comment-body">
1859 <?php _e( 'Pingback:' ); ?> <?php comment_author_link(); ?> <?php edit_comment_link( __( 'Edit' ), '<span class="edit-link">', '</span>' ); ?>
1865 * Output a single comment.
1870 * @see wp_list_comments()
1872 * @param object $comment Comment to display.
1873 * @param int $depth Depth of comment.
1874 * @param array $args An array of arguments.
1876 protected function comment( $comment, $depth, $args ) {
1877 if ( 'div' == $args['style'] ) {
1879 $add_below = 'comment';
1882 $add_below = 'div-comment';
1885 <<?php echo $tag; ?> <?php comment_class( $this->has_children ? 'parent' : '' ); ?> id="comment-<?php comment_ID(); ?>">
1886 <?php if ( 'div' != $args['style'] ) : ?>
1887 <div id="div-comment-<?php comment_ID(); ?>" class="comment-body">
1889 <div class="comment-author vcard">
1890 <?php if ( 0 != $args['avatar_size'] ) echo get_avatar( $comment, $args['avatar_size'] ); ?>
1891 <?php printf( __( '<cite class="fn">%s</cite> <span class="says">says:</span>' ), get_comment_author_link() ); ?>
1893 <?php if ( '0' == $comment->comment_approved ) : ?>
1894 <em class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.' ) ?></em>
1898 <div class="comment-meta commentmetadata"><a href="<?php echo esc_url( get_comment_link( $comment->comment_ID, $args ) ); ?>">
1900 /* translators: 1: date, 2: time */
1901 printf( __( '%1$s at %2$s' ), get_comment_date(), get_comment_time() ); ?></a><?php edit_comment_link( __( '(Edit)' ), ' ', '' );
1905 <?php comment_text( get_comment_id(), array_merge( $args, array( 'add_below' => $add_below, 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
1908 comment_reply_link( array_merge( $args, array(
1909 'add_below' => $add_below,
1911 'max_depth' => $args['max_depth'],
1912 'before' => '<div class="reply">',
1917 <?php if ( 'div' != $args['style'] ) : ?>
1924 * Output a comment in the HTML5 format.
1929 * @see wp_list_comments()
1931 * @param object $comment Comment to display.
1932 * @param int $depth Depth of comment.
1933 * @param array $args An array of arguments.
1935 protected function html5_comment( $comment, $depth, $args ) {
1936 $tag = ( 'div' === $args['style'] ) ? 'div' : 'li';
1938 <<?php echo $tag; ?> id="comment-<?php comment_ID(); ?>" <?php comment_class( $this->has_children ? 'parent' : '' ); ?>>
1939 <article id="div-comment-<?php comment_ID(); ?>" class="comment-body">
1940 <footer class="comment-meta">
1941 <div class="comment-author vcard">
1942 <?php if ( 0 != $args['avatar_size'] ) echo get_avatar( $comment, $args['avatar_size'] ); ?>
1943 <?php printf( __( '%s <span class="says">says:</span>' ), sprintf( '<b class="fn">%s</b>', get_comment_author_link() ) ); ?>
1944 </div><!-- .comment-author -->
1946 <div class="comment-metadata">
1947 <a href="<?php echo esc_url( get_comment_link( $comment->comment_ID, $args ) ); ?>">
1948 <time datetime="<?php comment_time( 'c' ); ?>">
1949 <?php printf( _x( '%1$s at %2$s', '1: date, 2: time' ), get_comment_date(), get_comment_time() ); ?>
1952 <?php edit_comment_link( __( 'Edit' ), '<span class="edit-link">', '</span>' ); ?>
1953 </div><!-- .comment-metadata -->
1955 <?php if ( '0' == $comment->comment_approved ) : ?>
1956 <p class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.' ); ?></p>
1958 </footer><!-- .comment-meta -->
1960 <div class="comment-content">
1961 <?php comment_text(); ?>
1962 </div><!-- .comment-content -->
1965 comment_reply_link( array_merge( $args, array(
1966 'add_below' => 'div-comment',
1968 'max_depth' => $args['max_depth'],
1969 'before' => '<div class="reply">',
1973 </article><!-- .comment-body -->
1981 * Used in the comments.php template to list comments for a particular post.
1985 * @see WP_Query->comments
1987 * @param string|array $args {
1988 * Optional. Formatting options.
1990 * @type object $walker Instance of a Walker class to list comments. Default null.
1991 * @type int $max_depth The maximum comments depth. Default empty.
1992 * @type string $style The style of list ordering. Default 'ul'. Accepts 'ul', 'ol'.
1993 * @type string $callback Callback function to use. Default null.
1994 * @type string $end-callback Callback function to use at the end. Default null.
1995 * @type string $type Type of comments to list.
1996 * Default 'all'. Accepts 'all', 'comment', 'pingback', 'trackback', 'pings'.
1997 * @type int $page Page ID to list comments for. Default empty.
1998 * @type int $per_page Number of comments to list per page. Default empty.
1999 * @type int $avatar_size Height and width dimensions of the avatar size. Default 32.
2000 * @type string $reverse_top_level Ordering of the listed comments. Default null. Accepts 'desc', 'asc'.
2001 * @type bool $reverse_children Whether to reverse child comments in the list. Default null.
2002 * @type string $format How to format the comments list.
2003 * Default 'html5' if the theme supports it. Accepts 'html5', 'xhtml'.
2004 * @type bool $short_ping Whether to output short pings. Default false.
2005 * @type bool $echo Whether to echo the output or return it. Default true.
2007 * @param array $comments Optional. Array of comment objects.
2009 function wp_list_comments( $args = array(), $comments = null ) {
2010 global $wp_query, $comment_alt, $comment_depth, $comment_thread_alt, $overridden_cpage, $in_comment_loop;
2012 $in_comment_loop = true;
2014 $comment_alt = $comment_thread_alt = 0;
2022 'end-callback' => null,
2026 'avatar_size' => 32,
2027 'reverse_top_level' => null,
2028 'reverse_children' => '',
2029 'format' => current_theme_supports( 'html5', 'comment-list' ) ? 'html5' : 'xhtml',
2030 'short_ping' => false,
2034 $r = wp_parse_args( $args, $defaults );
2037 * Filter the arguments used in retrieving the comment list.
2041 * @see wp_list_comments()
2043 * @param array $r An array of arguments for displaying comments.
2045 $r = apply_filters( 'wp_list_comments_args', $r );
2047 // Figure out what comments we'll be looping through ($_comments)
2048 if ( null !== $comments ) {
2049 $comments = (array) $comments;
2050 if ( empty($comments) )
2052 if ( 'all' != $r['type'] ) {
2053 $comments_by_type = separate_comments($comments);
2054 if ( empty($comments_by_type[$r['type']]) )
2056 $_comments = $comments_by_type[$r['type']];
2058 $_comments = $comments;
2061 if ( empty($wp_query->comments) )
2063 if ( 'all' != $r['type'] ) {
2064 if ( empty($wp_query->comments_by_type) )
2065 $wp_query->comments_by_type = separate_comments($wp_query->comments);
2066 if ( empty($wp_query->comments_by_type[$r['type']]) )
2068 $_comments = $wp_query->comments_by_type[$r['type']];
2070 $_comments = $wp_query->comments;
2074 if ( '' === $r['per_page'] && get_option('page_comments') )
2075 $r['per_page'] = get_query_var('comments_per_page');
2077 if ( empty($r['per_page']) ) {
2082 if ( '' === $r['max_depth'] ) {
2083 if ( get_option('thread_comments') )
2084 $r['max_depth'] = get_option('thread_comments_depth');
2086 $r['max_depth'] = -1;
2089 if ( '' === $r['page'] ) {
2090 if ( empty($overridden_cpage) ) {
2091 $r['page'] = get_query_var('cpage');
2093 $threaded = ( -1 != $r['max_depth'] );
2094 $r['page'] = ( 'newest' == get_option('default_comments_page') ) ? get_comment_pages_count($_comments, $r['per_page'], $threaded) : 1;
2095 set_query_var( 'cpage', $r['page'] );
2099 $r['page'] = intval($r['page']);
2100 if ( 0 == $r['page'] && 0 != $r['per_page'] )
2103 if ( null === $r['reverse_top_level'] )
2104 $r['reverse_top_level'] = ( 'desc' == get_option('comment_order') );
2106 if ( empty( $r['walker'] ) ) {
2107 $walker = new Walker_Comment;
2109 $walker = $r['walker'];
2112 $output = $walker->paged_walk( $_comments, $r['max_depth'], $r['page'], $r['per_page'], $r );
2113 $wp_query->max_num_comment_pages = $walker->max_pages;
2115 $in_comment_loop = false;
2125 * Output a complete commenting form for use within a template.
2127 * Most strings and form fields may be controlled through the $args array passed
2128 * into the function, while you may also choose to use the comment_form_default_fields
2129 * filter to modify the array of default fields if you'd just like to add a new
2130 * one or remove a single field. All fields are also individually passed through
2131 * a filter of the form comment_form_field_$name where $name is the key used
2132 * in the array of fields.
2136 * @param array $args {
2137 * Optional. Default arguments and form fields to override.
2139 * @type array $fields {
2140 * Default comment fields, filterable by default via the 'comment_form_default_fields' hook.
2142 * @type string $author Comment author field HTML.
2143 * @type string $email Comment author email field HTML.
2144 * @type string $url Comment author URL field HTML.
2146 * @type string $comment_field The comment textarea field HTML.
2147 * @type string $must_log_in HTML element for a 'must be logged in to comment' message.
2148 * @type string $logged_in_as HTML element for a 'logged in as [user]' message.
2149 * @type string $comment_notes_before HTML element for a message displayed before the comment form.
2150 * Default 'Your email address will not be published.'.
2151 * @type string $comment_notes_after HTML element for a message displayed after the comment form.
2152 * Default 'You may use these HTML tags and attributes ...'.
2153 * @type string $id_form The comment form element id attribute. Default 'commentform'.
2154 * @type string $id_submit The comment submit element id attribute. Default 'submit'.
2155 * @type string $class_submit The comment submit element class attribute. Default 'submit'.
2156 * @type string $name_submit The comment submit element name attribute. Default 'submit'.
2157 * @type string $title_reply The translatable 'reply' button label. Default 'Leave a Reply'.
2158 * @type string $title_reply_to The translatable 'reply-to' button label. Default 'Leave a Reply to %s',
2159 * where %s is the author of the comment being replied to.
2160 * @type string $cancel_reply_link The translatable 'cancel reply' button label. Default 'Cancel reply'.
2161 * @type string $label_submit The translatable 'submit' button label. Default 'Post a comment'.
2162 * @type string $format The comment form format. Default 'xhtml'. Accepts 'xhtml', 'html5'.
2164 * @param int|WP_Post $post_id Post ID or WP_Post object to generate the form for. Default current post.
2166 function comment_form( $args = array(), $post_id = null ) {
2167 if ( null === $post_id )
2168 $post_id = get_the_ID();
2170 $commenter = wp_get_current_commenter();
2171 $user = wp_get_current_user();
2172 $user_identity = $user->exists() ? $user->display_name : '';
2174 $args = wp_parse_args( $args );
2175 if ( ! isset( $args['format'] ) )
2176 $args['format'] = current_theme_supports( 'html5', 'comment-form' ) ? 'html5' : 'xhtml';
2178 $req = get_option( 'require_name_email' );
2179 $aria_req = ( $req ? " aria-required='true'" : '' );
2180 $html5 = 'html5' === $args['format'];
2182 'author' => '<p class="comment-form-author">' . '<label for="author">' . __( 'Name' ) . ( $req ? ' <span class="required">*</span>' : '' ) . '</label> ' .
2183 '<input id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30"' . $aria_req . ' /></p>',
2184 'email' => '<p class="comment-form-email"><label for="email">' . __( 'Email' ) . ( $req ? ' <span class="required">*</span>' : '' ) . '</label> ' .
2185 '<input id="email" name="email" ' . ( $html5 ? 'type="email"' : 'type="text"' ) . ' value="' . esc_attr( $commenter['comment_author_email'] ) . '" size="30" aria-describedby="email-notes"' . $aria_req . ' /></p>',
2186 'url' => '<p class="comment-form-url"><label for="url">' . __( 'Website' ) . '</label> ' .
2187 '<input id="url" name="url" ' . ( $html5 ? 'type="url"' : 'type="text"' ) . ' value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="30" /></p>',
2190 $required_text = sprintf( ' ' . __('Required fields are marked %s'), '<span class="required">*</span>' );
2193 * Filter the default comment form fields.
2197 * @param array $fields The default comment fields.
2199 $fields = apply_filters( 'comment_form_default_fields', $fields );
2201 'fields' => $fields,
2202 'comment_field' => '<p class="comment-form-comment"><label for="comment">' . _x( 'Comment', 'noun' ) . '</label> <textarea id="comment" name="comment" cols="45" rows="8" aria-describedby="form-allowed-tags" aria-required="true"></textarea></p>',
2203 /** This filter is documented in wp-includes/link-template.php */
2204 'must_log_in' => '<p class="must-log-in">' . sprintf( __( 'You must be <a href="%s">logged in</a> to post a comment.' ), wp_login_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) ) ) . '</p>',
2205 /** This filter is documented in wp-includes/link-template.php */
2206 'logged_in_as' => '<p class="logged-in-as">' . sprintf( __( 'Logged in as <a href="%1$s">%2$s</a>. <a href="%3$s" title="Log out of this account">Log out?</a>' ), get_edit_user_link(), $user_identity, wp_logout_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) ) ) . '</p>',
2207 'comment_notes_before' => '<p class="comment-notes"><span id="email-notes">' . __( 'Your email address will not be published.' ) . '</span>'. ( $req ? $required_text : '' ) . '</p>',
2208 'comment_notes_after' => '<p class="form-allowed-tags" id="form-allowed-tags">' . sprintf( __( 'You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: %s' ), ' <code>' . allowed_tags() . '</code>' ) . '</p>',
2209 'id_form' => 'commentform',
2210 'id_submit' => 'submit',
2211 'class_submit' => 'submit',
2212 'name_submit' => 'submit',
2213 'title_reply' => __( 'Leave a Reply' ),
2214 'title_reply_to' => __( 'Leave a Reply to %s' ),
2215 'cancel_reply_link' => __( 'Cancel reply' ),
2216 'label_submit' => __( 'Post Comment' ),
2217 'format' => 'xhtml',
2221 * Filter the comment form default arguments.
2223 * Use 'comment_form_default_fields' to filter the comment fields.
2227 * @param array $defaults The default comment form arguments.
2229 $args = wp_parse_args( $args, apply_filters( 'comment_form_defaults', $defaults ) );
2232 <?php if ( comments_open( $post_id ) ) : ?>
2235 * Fires before the comment form.
2239 do_action( 'comment_form_before' );
2241 <div id="respond" class="comment-respond">
2242 <h3 id="reply-title" class="comment-reply-title"><?php comment_form_title( $args['title_reply'], $args['title_reply_to'] ); ?> <small><?php cancel_comment_reply_link( $args['cancel_reply_link'] ); ?></small></h3>
2243 <?php if ( get_option( 'comment_registration' ) && !is_user_logged_in() ) : ?>
2244 <?php echo $args['must_log_in']; ?>
2247 * Fires after the HTML-formatted 'must log in after' message in the comment form.
2251 do_action( 'comment_form_must_log_in_after' );
2254 <form action="<?php echo site_url( '/wp-comments-post.php' ); ?>" method="post" id="<?php echo esc_attr( $args['id_form'] ); ?>" class="comment-form"<?php echo $html5 ? ' novalidate' : ''; ?>>
2257 * Fires at the top of the comment form, inside the form tag.
2261 do_action( 'comment_form_top' );
2263 <?php if ( is_user_logged_in() ) : ?>
2266 * Filter the 'logged in' message for the comment form for display.
2270 * @param string $args_logged_in The logged-in-as HTML-formatted message.
2271 * @param array $commenter An array containing the comment author's
2272 * username, email, and URL.
2273 * @param string $user_identity If the commenter is a registered user,
2274 * the display name, blank otherwise.
2276 echo apply_filters( 'comment_form_logged_in', $args['logged_in_as'], $commenter, $user_identity );
2280 * Fires after the is_user_logged_in() check in the comment form.
2284 * @param array $commenter An array containing the comment author's
2285 * username, email, and URL.
2286 * @param string $user_identity If the commenter is a registered user,
2287 * the display name, blank otherwise.
2289 do_action( 'comment_form_logged_in_after', $commenter, $user_identity );
2292 <?php echo $args['comment_notes_before']; ?>
2295 * Fires before the comment fields in the comment form.
2299 do_action( 'comment_form_before_fields' );
2300 foreach ( (array) $args['fields'] as $name => $field ) {
2302 * Filter a comment form field for display.
2304 * The dynamic portion of the filter hook, `$name`, refers to the name
2305 * of the comment form field. Such as 'author', 'email', or 'url'.
2309 * @param string $field The HTML-formatted output of the comment form field.
2311 echo apply_filters( "comment_form_field_{$name}", $field ) . "\n";
2314 * Fires after the comment fields in the comment form.
2318 do_action( 'comment_form_after_fields' );
2323 * Filter the content of the comment textarea field for display.
2327 * @param string $args_comment_field The content of the comment textarea field.
2329 echo apply_filters( 'comment_form_field_comment', $args['comment_field'] );
2331 <?php echo $args['comment_notes_after']; ?>
2332 <p class="form-submit">
2333 <input name="<?php echo esc_attr( $args['name_submit'] ); ?>" type="submit" id="<?php echo esc_attr( $args['id_submit'] ); ?>" class="<?php echo esc_attr( $args['class_submit'] ); ?>" value="<?php echo esc_attr( $args['label_submit'] ); ?>" />
2334 <?php comment_id_fields( $post_id ); ?>
2338 * Fires at the bottom of the comment form, inside the closing </form> tag.
2342 * @param int $post_id The post ID.
2344 do_action( 'comment_form', $post_id );
2348 </div><!-- #respond -->
2351 * Fires after the comment form.
2355 do_action( 'comment_form_after' );
2358 * Fires after the comment form if comments are closed.
2362 do_action( 'comment_form_comments_closed' );