1

I am trying to display each comment date on the comments list table on the post edit screen. Currently it only displays the Author name, IP address and comment content.

https://developer.wordpress.org/reference/classes/wp_post_comments_list_table/

Any clues?

Thanks

1

So, it doesn't appear you can do much "properly" to solve this issue, however, you can take some advice from this answer and apply it to your problem:

<?php

add_filter( 'get_comment_author_IP', function( $comment_author_IP, $comment_ID, $comment )
{
    global $pagenow;

    if( is_admin() && 'post.php' === $pagenow && isset( $_GET['post'] ) ) {
        $comment_date = do_something_to_get_comment_date($comment_ID);
        $comment_author_IP .= ' <span>Posted On:</span> ' . $comment_date;
    }

    return $comment_author_IP;
}, 10, 3 );

This allows you to modify the content returned with the comment's IP address. You should definitely make sure your if check only modifies the data returned in the one specific case that you need it, so you may need to play around with how to write that conditional to avoid your extra data leaking into places it shouldn't be.

Update with functional code

The previous answer was mostly a guess at what you'd need to do, but I did some testing on my own WP install. An issue with overriding comment IP is that it's used to form a link to search for comments from that IP address. Instead, we'll use comment_author since it's not a functional string in the UI.

Also, the gate I added was wrong since comments come in over AJAX when editing a post, so I've updated the checks as well:

add_filter( 'get_comment_author', function( $comment_author, $comment_ID, $comment )
{
    global $pagenow;

    if ( ! is_admin() ) {
        return $comment_author;
    }

    if ( ! isset( $_POST['action'] ) || 'get-comments' !== filter_var( $_POST['action'], FILTER_SANITIZE_STRING ) ) {
        return $comment_author;
    }

    $comment_author .= ' | ' . get_comment_date( '', $comment_ID );
    return $comment_author;
}, 10, 3 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.