comment_reply_link doesn’t show

I want to create my own template for comments, so I did run foreach and this came out:

<dl class="commentlist">
    <?php foreach ($comments as $comment) : ?>
        <dt><?php printf(__('%s'), get_comment_author_link()) ?> <em><?php echo human_time_diff( get_comment_time('U'), current_time('timestamp') ); ?> <?php echo get_locale() == 'pl_PL' ? 'temu' : 'ago'; ?></em></dt>
        <dd>
            <?php if ($comment->comment_approved == '0') : ?>
                <em>Komentarz czeka na zatwierdzenie</em><br />
            <?php endif; ?>

            <?php comment_text(); ?>
            <?php comment_reply_link( array ( 'reply_text' => 'Reply this comment' ) ); ?>
        </dd>
    <?php endforeach; ?>
</dl>

All well, but I can’t get comment_reply_link to work. I tried using solution found in WordPress Comment reply link does not appear, but it doesn’t work for me.

Read More
comment_reply_link( array('reply_text' => 'Reply this comment'), comment_ID(), the_ID() );

gives me some number (propably timestamp of the comment).

What can I do?

Related posts

Leave a Reply

3 comments

  1. If you are using not using wp_list_comments but for instance get_comments and then run a for each, as you did above, the problem with comment_reply_link is the following:

    In the wordpress comment-template.php the get_comment_reply_link used by comment_reply_link has a NULL value in $args['max_depth'] thus the below if statement:

    function get_comment_reply_link($args = array(), $comment = null, $post = null) {
    
    $defaults = array(
        'add_below'  => 'comment',
        'respond_id' => 'respond',
        'reply_text' => __('Reply'),
        'login_text' => __('Log in to Reply'),
        'depth'      => 0,
        'before'     => '',
        'after'      => ''
    );
    
    $args = wp_parse_args($args, $defaults);
    
    if ( 0 == $args['depth'] || $args['max_depth'] <= $args['depth'] )
        return;
    

    is always true and the function exits premature. Even if you set 'depth' => 1. Because NULL is always smaller than 0,1,etc..
    You can not write a custom filter for comment_reply_link because the hook is called later in the function.

    The only way around I have found without changing the comment-template.php file was to do the following in my comment.php:

    $post_id = get_the_ID();
    $comment_id =get_comment_ID();
    
    //get the setting configured in the admin panel under settings discussions "Enable threaded (nested) comments  levels deep"  
    $max_depth = get_option('thread_comments_depth');
    //add max_depth to the array and give it the value from above and set the depth to 1
    $default = array(
        'add_below'  => 'comment',
        'respond_id' => 'respond',
        'reply_text' => __('Reply'),
        'login_text' => __('Log in to Reply'),
        'depth'      => 1,
        'before'     => '',
        'after'      => '',
        'max_depth'  => $max_depth
        );
                        comment_reply_link($default,$comment_id,$post_id); 
    

    Then the link will show up.

  2. Ok this is going to be a long answer:)

    1.Create a commnets.php and add a following code:

    <?php if ( post_password_required() ): ?>   
    <p class="nopassword"><?php _e( 'This post is password protected. Enter the password to view and post comments.' , 'override' ); ?></p>  
    
     <?php   
     return;  
     endif;  
     ?>  
        <?php if(!empty($_SERVER['SCRIPT_FILENAME']) && 'comments.php' == basename($_SERVER['SCRIPT_FILENAME'])) : ?>
        <?php die('The comments template cannot be accessed outside of an entry. Nice try!'); ?>
        <?php endif; ?>
        <?php
        $required_text='';
        $aria_req='';
        ?>
        <?php $args = array(
    'id_form' => 'commentform',
    'id_submit' => 'submit',
    'title_reply' => __( 'Leave a Reply' ),
    'title_reply_to' => __( 'Leave a Reply to %s' ),
    'cancel_reply_link' => __( 'Cancel Reply' ),
    'label_submit' => __( 'Post Comment' ),
    'comment_field' => '<br/><p class="comment-form-comment"><label for="comment">' . _x( 'Comment post', 'noun' ) . '</label><br/><textarea id="comment" name="comment" cols="45" rows="8"></textarea></p>',//Removed from textarea aria-required="true" HTML5 fix
    '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( ) ) ) ) . '</p>',
    '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>' ), admin_url( 'profile.php' ), $user_identity, wp_logout_url( apply_filters( 'the_permalink', get_permalink( ) ) ) ) . '</p>',
    'comment_notes_before' => '<p class="comment-notes">' . __( 'Your email address will not be published.' ) . ( $req ? $required_text : '' ) . '</p>',
    'comment_notes_after' => '<p class="form-allowed-tags">' . sprintf( __( 'You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: %s' ), '<br/><code>' . allowed_tags() . '</code>' ) . '</p>',
    'fields' => apply_filters( 'comment_form_default_fields', array(
    'author' => '<div class="commnet_text_wrapper"><p class="comment-form-author">' . '<label for="author">' . __( 'Name', 'override' ) . '</label> ' . ( $req ? '<span class="required">*</span>' : '' ) . '<input class="author" id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30"' . $aria_req . ' /></p>',
    'email' => '<p class="comment-form-email"><label for="email">' . __( 'Email', 'override' ) . '</label> ' . ( $req ? '<span class="required">*</span>' : '' ) . '<input class="email" id="email" name="email" type="text" value="' . esc_attr(  $commenter['comment_author_email'] ) . '" size="30"' . $aria_req . ' /></p>',
    'url' => '<p class="comment-form-url"><label for="url">' . __( 'Website', 'override' ) . '</label>' . '<input id="url" class="website" name="url" type="text" value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="30" /></p></div>' ) ) );
    
     ?>
    
         <?php comment_form($args); ?>
         <ol class="commentlist">
         <?php wp_list_comments('type=comment&callback=mytheme_comment'); ?>
           </ol>
          <div class="paginate-com">
          <?php paginate_comments_links(
          array('prev_text' => __('&lsaquo; Previous comment','override'), 'next_text' => __('Next commnet &rsaquo;','override'))
          ); ?>
         </div>
    

    Upload that file to your theme root directory (where your theme index.php is located).

    2.Open your functions.php and add the following code:

          // Enable CUSTOM "REPLY" COMMENT FORM so we can style it
          function mytheme_comment($comment, $args, $depth) {
        $GLOBALS['comment'] = $comment;
        extract($args, EXTR_SKIP);
    
        if ( 'div' == $args['style'] ) {
            $tag = 'div';
            $add_below = 'comment';
        } else {
            $tag = 'li';
            $add_below = 'div-comment';
        }
                ?>
        <<?php echo $tag ?> <?php comment_class(empty( $args['has_children'] ) ? '' : 'parent') ?> id="comment-<?php comment_ID() ?>">
        <?php if ( 'div' != $args['style'] ) : ?>
        <div id="div-comment-<?php comment_ID() ?>" class="comment-body">
        <?php endif; ?>
        <div class="comment-author vcard">
        <?php if ($args['avatar_size'] != 0) echo get_avatar( $comment, $args['avatar_size'] ); ?>
        <br/><?php printf(__('<span class="saywrap"><span class="fn">%s</span> <span class="says">says:</span></span>','override'), get_comment_author_link()) ?>
        </div>
                <?php if ($comment->comment_approved == '0') : ?>
        <em class="comment-awaiting-moderation"><?php _e('Your comment is awaiting moderation.') ?></em>
        <br />
               <?php endif; ?>
    
    
    
        <?php comment_text() ?>
                <div class="comment-meta commentmetadata"><a class="commentlink" href="<?php echo htmlspecialchars( get_comment_link( $comment->comment_ID ) ) ?>">
            <?php
                /* translators: 1: date, 2: time */
                printf( __('%1$s at %2$s','override'), get_comment_date(),  get_comment_time()) ?></a><?php edit_comment_link(__('(Edit)','override'),'  ','' );
            ?>
        </div>
        <div class="reply">
        <?php comment_reply_link(array_merge( $args, array('add_below' => $add_below, 'depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
        </div>
        <?php if ( 'div' != $args['style'] ) : ?>
        </div>
        <?php endif; ?>
                <?php
                }
    

    3.Find your single.php file located also inside your theme root directory ,and call
    your commnet form like so:

    <div class="comments-template">     
    <?php comments_template(); ?>
    </div>
    
  3. There is no such thing as a depth 0 comment. This usually happens when the native comment walker has been replaced, and the new walker class does not increment the $depth variable correctly, so it defaults to 0. It is a common mistake. You might see this as the depth-0 class appearing, or the depth being mistakenly 1 less than it should be.

    To remedy this, adjust the start_el function of any walker to increment both $depth as well as the comment_depth global the same way the WP core implementation does. This may also resolve some related issues and PHP warnings.