Remove “at” string from wordpress comment date

I’m using the following code to format the author’s comment date and remove the time:

function my_change_comment_date_format( $date, $date_format, $comment ) {
    return date( 'd M Y', strtotime( $comment->comment_date ) );
}
add_filter( 'get_comment_date', 'my_change_comment_date_format', 10, 3 );

function wpb_remove_comment_time($date, $d, $comment) { 
    if ( !is_admin() ) {
            return;
    } else { 
            return $date;
    }
}
add_filter( 'get_comment_time', 'wpb_remove_comment_time', 10, 3);

But now, what I got is in the following format:
14 Mar 2013 at

How can I remove the “at” string, at the end of the date?

1 Answer
1

Most likely, the ‘at’ is coming from the value of $comment->comment_date. If that is the case, and since we have to do with string, you could pass it from str_replace first, in order to remove the ‘ at’, like:

function my_change_comment_date_format( $date, $date_format, $comment ) {
    return date( 'd M Y', strtotime( str_replace(" at", "", $comment->comment_date) ); );
}

Edit: since the above was not the case, I searched a bit and got the following:

  • In your comments.php template you probably have a call to wp_list_comments(), which without parameters, runs with defaults . One of the default arguments is the 'walker' => new Walker_Comment() which construct the html output of the comments. Taking a look at the Walker_Comment class, arround line 300 is the culprit ‘at’.

More specifically: printf( __( '%1$s at %2$s' ), get_comment_date( '', $comment ), get_comment_time() );, thus getting rid of get_comment_time() will still print at.

To alter the output you can make use of a callback function, passed to the wp_list_comments()

e.g:1) in the comments.php (or in any other template file you want to display the comments)

<?php
// Show comments
wp_list_comments( array(
    'callback' => 'not_default_comments'
) );
?>

2) In your functions.php (or in a seperate file, which you will include in the functions), write the callback code:

<?php
function not_default_comments( $comment, $args, $depth ) {
    global $post;
    $author_id = $post->post_author;
    $GLOBALS['comment'] = $comment;
?>
    <li id="li-comment-<?php comment_ID(); ?>">
        <article id="comment-<?php comment_ID(); ?>" <?php comment_class('clr'); ?>>
            <div class="comment-author vcard">
                <?php echo get_avatar( $comment, 45 ); ?>
            </div><!-- .comment-author -->
            <div class="comment-details clr">
                <header class="comment-meta">
                    <cite class="fn"><?php comment_author_link(); ?></cite>
                    <span class="comment-date">
                    <?php printf( '<a href="https://wordpress.stackexchange.com/questions/331720/%1$s"><time datetime="%2$s">%3$s</time></a>',
                        esc_url( get_comment_link( $comment->comment_ID ) ),
                        get_comment_time( 'c' ),
                        sprintf( _x( "https://wordpress.stackexchange.com/questions/331720/%1$s", '1: date', 'twenties' ), get_comment_date() )
                    ); ?>
                    </span><!-- .comment-date -->
                </header><!-- .comment-meta -->
                <?php if ( '0' == $comment->comment_approved ) : ?>
                    <p class="comment-awaiting-moderation"><?php esc_html_e( 'Your comment is awaiting moderation.', 'twenties' ); ?></p>
                <?php endif; ?>
                <div class="comment-content entry clr">
                    <?php comment_text(); ?>
                </div><!-- .comment-content -->
                <div class="reply comment-reply-link">
                    <?php comment_reply_link( array_merge( $args, array(
                        'reply_text' => esc_html__( 'Reply to this message', 'twenties' ),
                        'depth'      => $depth,
                        'max_depth'  => $args['max_depth'] )
                    ) ); ?>
                </div><!-- .reply -->
            </div><!-- .comment-details -->
        </article><!-- #comment-## -->
    <?php
}
  • I got the above example callback function from wpexplorer.com, and sligtly modified it to exclude the ‘… at time’ part.

And that’s it. Now your comments will be displayed with your custom template from the callback you have provided.

Leave a Comment