Opened 8 years ago
Closed 8 years ago
#41025 closed defect (bug) (invalid)
get_comment_text and comment_text do not return the same result.
Reported by: | BackuPs | Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 4.8 |
Component: | Comments | Keywords: | |
Focuses: | Cc: |
Description
Hi
I was working on some changes to the comment code and came across a weird issue. I looked at the wp core code but could not figure out why.
Suppose a comment is added like this
Hello World This is a comment With a new line of text
If i call get_comment_text i get a comment that has been stripped from all empty lines (p-tags) in the comment when the comment was posted not being a admin or editor.
i get this
Hello World This is a comment With a new line of text
However if i call the function comment_text this does not happen.
i get this
Hello World This is a comment With a new line of text
It tried some modified code in the default wp twenty seventeen theme and i get similar results.
if you change the comments.php in that theme to this
<?php /** * The template for displaying comments * * This is the template that displays the area of the page that contains both the current comments * and the comment form. * * @link https://codex.wordpress.org/Template_Hierarchy * * @package WordPress * @subpackage Twenty_Seventeen * @since 1.0 * @version 1.0 */ /* * If the current post is protected by a password and * the visitor has not yet entered the password we will * return early without loading the comments. */ if ( post_password_required() ) { return; } function theme_comments($comment, $args, $depth) { $GLOBALS['comment'] = $comment; ?> <li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>"> <div id="comment-<?php comment_ID(); ?>" class="comment_wrap"> <div class="gravatar"><?php echo get_avatar($comment,$size='60',$default=''); ?></div> <div class='comment_content'> <div class="comment_meta"> <?php printf( '<cite class="comment_author">%s</cite>', get_comment_author_link()) ?><?php edit_comment_link(__('(Edit)', 'theme_front' ),' ','') ?> <time class="comment_time"><?php echo get_comment_date(); ?></time> </div> <div class='comment_text'> <?php echo get_comment_text();?> <?php if ($comment->comment_approved == '0') : ?> <span class="unapproved"><?php _e('Your comment is awaiting moderation.','theme_front') ?></span> <?php endif; ?> </div> <div class="reply"> <?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?> </div> </div> </div> <?php } ?> <div id="comments" class="comments-area"> <?php // You can start editing here -- including this comment! if ( have_comments() ) : ?> <h2 class="comments-title"> <?php $comments_number = get_comments_number(); if ( '1' === $comments_number ) { /* translators: %s: post title */ printf( _x( 'One Reply to “%s”', 'comments title', 'twentyseventeen' ), get_the_title() ); } else { printf( /* translators: 1: number of comments, 2: post title */ _nx( '%1$s Reply to “%2$s”', '%1$s Replies to “%2$s”', $comments_number, 'comments title', 'twentyseventeen' ), number_format_i18n( $comments_number ), get_the_title() ); } ?> </h2> <ol class="comment-list"> <?php wp_list_comments( array( 'callback' => 'theme_comments' ) ); ?> </ol> <?php the_comments_pagination( array( 'prev_text' => twentyseventeen_get_svg( array( 'icon' => 'arrow-left' ) ) . '<span class="screen-reader-text">' . __( 'Previous', 'twentyseventeen' ) . '</span>', 'next_text' => '<span class="screen-reader-text">' . __( 'Next', 'twentyseventeen' ) . '</span>' . twentyseventeen_get_svg( array( 'icon' => 'arrow-right' ) ), ) ); endif; // Check for have_comments(). // If comments are closed and there are comments, let's leave a little note, shall we? if ( ! comments_open() && get_comments_number() && post_type_supports( get_post_type(), 'comments' ) ) : ?> <p class="no-comments"><?php _e( 'Comments are closed.', 'twentyseventeen' ); ?></p> <?php endif; comment_form(); ?> </div><!-- #comments -->
all line breaks (p-tags are gone in the returned comments
if however you use this which is only one line of code difference
<?php /** * The template for displaying comments * * This is the template that displays the area of the page that contains both the current comments * and the comment form. * * @link https://codex.wordpress.org/Template_Hierarchy * * @package WordPress * @subpackage Twenty_Seventeen * @since 1.0 * @version 1.0 */ /* * If the current post is protected by a password and * the visitor has not yet entered the password we will * return early without loading the comments. */ if ( post_password_required() ) { return; } function theme_comments($comment, $args, $depth) { $GLOBALS['comment'] = $comment; ?> <li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>"> <div id="comment-<?php comment_ID(); ?>" class="comment_wrap"> <div class="gravatar"><?php echo get_avatar($comment,$size='60',$default=''); ?></div> <div class='comment_content'> <div class="comment_meta"> <?php printf( '<cite class="comment_author">%s</cite>', get_comment_author_link()) ?><?php edit_comment_link(__('(Edit)', 'theme_front' ),' ','') ?> <time class="comment_time"><?php echo get_comment_date(); ?></time> </div> <div class='comment_text'> <?php comment_text();?> <?php if ($comment->comment_approved == '0') : ?> <span class="unapproved"><?php _e('Your comment is awaiting moderation.','theme_front') ?></span> <?php endif; ?> </div> <div class="reply"> <?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?> </div> </div> </div> <?php } ?> <div id="comments" class="comments-area"> <?php // You can start editing here -- including this comment! if ( have_comments() ) : ?> <h2 class="comments-title"> <?php $comments_number = get_comments_number(); if ( '1' === $comments_number ) { /* translators: %s: post title */ printf( _x( 'One Reply to “%s”', 'comments title', 'twentyseventeen' ), get_the_title() ); } else { printf( /* translators: 1: number of comments, 2: post title */ _nx( '%1$s Reply to “%2$s”', '%1$s Replies to “%2$s”', $comments_number, 'comments title', 'twentyseventeen' ), number_format_i18n( $comments_number ), get_the_title() ); } ?> </h2> <ol class="comment-list"> <?php wp_list_comments( array( 'callback' => 'theme_comments' ) ); ?> </ol> <?php the_comments_pagination( array( 'prev_text' => twentyseventeen_get_svg( array( 'icon' => 'arrow-left' ) ) . '<span class="screen-reader-text">' . __( 'Previous', 'twentyseventeen' ) . '</span>', 'next_text' => '<span class="screen-reader-text">' . __( 'Next', 'twentyseventeen' ) . '</span>' . twentyseventeen_get_svg( array( 'icon' => 'arrow-right' ) ), ) ); endif; // Check for have_comments(). // If comments are closed and there are comments, let's leave a little note, shall we? if ( ! comments_open() && get_comments_number() && post_type_supports( get_post_type(), 'comments' ) ) : ?> <p class="no-comments"><?php _e( 'Comments are closed.', 'twentyseventeen' ); ?></p> <?php endif; comment_form(); ?> </div><!-- #comments -->
This does not happen. But I cannot seem to find the difference in the class-walker-comment.php causing this.
Can anybody point me into the right direction?
thank you
Change History (5)
#2
@
8 years ago
@swissspidy I dont understand what has running before hand of the filter comment_text todo with the fact the comment_text() returns
line 1 line 2 line 3
whereas get_comment_text() returns
line 1 line 2 line 3
The similar thing does not happen with the_content() and get_the_content()as they both return as far as i know
line 1 line 2 line 3
Can you explain more as both functions comment_text() and get_comment_text() call the filter and i still don't understand where the difference in result is coming from.
comment_text does the echo of the comment whereas using get_comment_text returns the string. But that string should be exactly the same without any difference in the text as comment_text. So why does get_comment_text strip all p-tags and comment_text does not?
#3
@
8 years ago
- Component changed from General to Comments
- Milestone Awaiting Review deleted
- Resolution set to invalid
- Status changed from new to closed
Can you explain more as both functions comment_text() and get_comment_text() call the filter and i still don't understand where the difference in result is coming from.
That's not the case. get_comment_text()
has the get_comment_text
filter in it and comment_text()
has the comment_text
filter.
If you look at wp-includes/default-filters.php
, you can see plenty of functions that are hooked to comment_text
, but not get_ comment_text
. These functions are for formatting and add the actual paragraphs etc.
comment_text does the echo of the comment whereas using get_comment_text returns the string. But that string should be exactly the same without any difference in the text as comment_text.
For historical reasons, those are not the same. get_comment_text()
is good for modifying the text before processing it. You don't really want automatically generated HTML to be in there.
So why does get_comment_text strip all p-tags and comment_text does not?
It's the other way around. In comment_text()
, the HTML is being added. If you want to have the same HTML in get_comment_text()
as well, you can add the needed functions to the filter.
I'm closing this because it's not really a bug and for BC reasons it's not something we should change.
#4
@
8 years ago
- Resolution invalid deleted
- Status changed from closed to reopened
Hi
I am not saying its a bug....
If you want to have the same HTML in get_comment_text() as well, you can add the needed functions to the filter.
I am asking politely for help not for being send into the code again. Can you give me the solution on how todo this? Thank you
#5
@
8 years ago
- Resolution set to invalid
- Status changed from reopened to closed
Sure.
Instead of adding the functions to the get_comment_text()
filter you can also use something like this:
$comment_text_with_paragraphs_and_stuff = apply_filters( 'comment_text', get_comment_text( $my_comment_id ) );
Please note that discussion can continue to happen on closed tickets. However, for further questions like this I'd recommend heading to wordpress.org/support.
That's because
comment_text()
displays the comment text instead of returning it and runs thecomment_text
filter beforehand. That's similar toget_the_content()
vsthe_content()
. Not really a bug.