[wp-trac] [WordPress Trac] #51189: comments_pagination_base missing in get_comment_reply_link() function

WordPress Trac noreply at wordpress.org
Sun Aug 30 09:48:23 UTC 2020


#51189: comments_pagination_base missing in get_comment_reply_link() function
-------------------------+-------------------------------------------------
 Reporter:  MrPauloEn    |      Owner:  (none)
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  Comments     |    Version:  5.5
 Severity:  normal       |   Keywords:  has-screenshots needs-patch needs-
  Focuses:               |  testing
  accessibility          |
-------------------------+-------------------------------------------------
 Hi guys.

 I noticed that **''get_comment_reply_link()''** does not create a valid
 reply link when comments are paginated.

 When I say: "not valid" I mean without comments_pagination_base part like
 this is in **''previous_comments_link''** or **''next_comments_link''** or
 **''get_comment_link''** function.

 I reffer exactly to this line:
 [https://core.trac.wordpress.org/browser/tags/5.5/src/wp-includes/comment-
 template.php#L1738]

 This issue is hard to notice until javacsript is disabled or the
 **''comment-reply''** script is not loaded or works incorrectly.

 Only then this link is an ordinary clickable link. If you click it, the
 page reloads as usually to show us such a reply form:

 [[Image(https://i.imgur.com/sDKsmNTg.png)]]
 with:

 == ''You reply to: Jane Doe:''

 Username is then linked to his comment. So when you click it, it should
 move you to this comment. It works correctly if you have only one comment
 page.


 == But...

 It won't work, if you split comments into pages.
 Option: ''page_comments'' is enabled an there are more than one comment
 page.

 It won't move you, if comment is on another page.

 == Why?

 Because there is ''comments_pagination_base'' missing in that url.

 == Consider this with an example.

 Now my comment reply link is this:

 [http://example.com/about/page-with-comments/?replytocom=179#respond]

 And the comment has this link:

 [http://example.com/about/page-with-comments/comment-page-2/#comment-179]


 If I want to jump for a moment to this comment, I can't. It just produce
 this link and nothing happens:

 [http://example.com/about/page-with-comments/?replytocom=179#comment-179]


 But when I slightly modify it:


 [http://example.com/about/page-with-comments/comment-
 page-2/?replytocom=182#respond]

 So, the mentioned author anchor has this link:
 [http://example.com/about/page-with-comments/comment-
 page-2/?replytocom=179#comment-179]

 And so, the comment has this link:
 [http://example.com/about/page-with-comments/comment-page-2/#comment-179]

 It skip to this comment when I clik user name anchor and come back when I
 click Reply link, becaouse username anchor has comment pagination url
 part.


 So whan I did.

 I looked into ''get_comment_reply_link()'' function, I copied some of a
 piece of code and I made a new filter, hooked to ''comment_reply_link''
 filter.

 I just added this to url in **''add_query_arg''** function:


 {{{ get_permalink( $post->ID ) . $wp_rewrite->comments_pagination_base .
 '-' . $page . '/' }}}

 Whole filter looks like this:

 {{{
 add_filter( 'comment_reply_link',
 'the_bootstrap_blog__filter_comment_reply_link', 10, 4 );
 function the_bootstrap_blog__filter_comment_reply_link( $link, $args,
 $comment, $post ){
         global $wp_rewrite;

         $page = get_page_of_comment( $comment->comment_ID );

 // Copied from get_comment_reply_link() function
         if ( get_option( 'comment_registration' ) && ! is_user_logged_in()
 ) {
                 $link = sprintf(
                         '<a rel="nofollow" class="comment-reply-login"
 href="%s">%s</a>',
                         esc_url( wp_login_url( get_permalink() ) ),
                         $args['login_text']
                 );
         } else {
                 $data_attributes = array(
                         'commentid'      => $comment->comment_ID,
                         'postid'         => $post->ID,
                         'belowelement'   => $args['add_below'] . '-' .
 $comment->comment_ID,
                         'respondelement' => $args['respond_id'],
                         'replyto'        => sprintf(
 $args['reply_to_text'], $comment->comment_author ),
                 );

                 $data_attribute_string = '';

                 foreach ( $data_attributes as $name => $value ) {
                         $data_attribute_string .= " data-${name}=\"" .
 esc_attr( $value ) . '"';
                 }

                 $data_attribute_string = trim( $data_attribute_string );

                 $link = sprintf(
                         "<a rel='nofollow' class='comment-reply-link'
 href='%s' %s aria-label='%s'>%s</a>",
                         esc_url(
                                 add_query_arg(
                                         array(
                                                 'replytocom'      =>
 $comment->comment_ID,
                                                 'unapproved'      =>
 false,
                                                 'moderation-hash' =>
 false,
                                         ),
                                         get_permalink( $post->ID ) .
 $wp_rewrite->comments_pagination_base . '-' . $page . '/'
                                 )
                         ) . '#' . $args['respond_id'],
                         $data_attribute_string,
                         esc_attr( sprintf( $args['reply_to_text'],
 $comment->comment_author ) ),
                         $args['reply_text']
                 );
         }
          return $args['before'] . $link . $args['after'];
 }
 }}}


 And it works fine.


 I know that this is only for my testing purposes, but if I have to put the
 theme in the WordPress repository, it should be more like in
 [https://developer.wordpress.org/reference/functions/get_comment_link/
 this] (''get_comment_link()'') function.


 I use:
 * WordPress v5.5
 I noticed this issue a long time ago, but somehow I ignored it, didn't
 know how to fix it.

 I tested this on default theme (twentytwenty), with all plugins
 deactivated and on my theme with all plugins activated. In each cases is
 the same.

 Only when I add that filter, thoses links works fine.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/51189>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list