Halloween Sale 15% off Ends in See the plans
PHP

Unanswered Comments Widget

Adds a dashboard widget listing recent approved comments from readers that nobody on your team has replied to yet, so questions do not go unanswered.

Don’t have WPIDE yet? Install it on your site

Code

add_action( 'wp_dashboard_setup', function () {
    if ( ! current_user_can( 'moderate_comments' ) ) {
        return;
    }

    wp_add_dashboard_widget( 'wpide_unanswered_comments', 'Unanswered comments', function () {
        global $wpdb;

        $staff = get_users( array( 'capability' => 'edit_posts', 'fields' => 'ID' ) );
        $staff = $staff ? implode( ',', array_map( 'absint', $staff ) ) : '0';

        $comments = $wpdb->get_results( $wpdb->prepare(
            "SELECT c.comment_ID, c.comment_post_ID, c.comment_author, c.comment_content, c.comment_date_gmt FROM {$wpdb->comments} c
            WHERE c.comment_approved = '1' AND c.comment_type IN ('comment','') AND c.user_id NOT IN ($staff) AND c.comment_date_gmt >= %s
            AND NOT EXISTS ( SELECT 1 FROM {$wpdb->comments} r WHERE r.comment_parent = c.comment_ID AND r.user_id IN ($staff) )
            ORDER BY c.comment_date_gmt DESC LIMIT 8",
            gmdate( 'Y-m-d H:i:s', time() - 60 * DAY_IN_SECONDS )
        ) );

        if ( ! $comments ) {
            echo '<p>Every recent comment has a reply.</p>';
            return;
        }

        echo '<ul>';
        foreach ( $comments as $comment ) {
            printf(
                '<li style="margin-bottom:10px"><strong>%s</strong> on <a href="%s">%s</a><br>%s<br><a href="%s">Reply</a> <small>· %s ago</small></li>',
                esc_html( $comment->comment_author ),
                esc_url( get_permalink( $comment->comment_post_ID ) ),
                esc_html( get_the_title( $comment->comment_post_ID ) ),
                esc_html( wp_trim_words( $comment->comment_content, 18 ) ),
                esc_url( admin_url( 'edit-comments.php?p=' . (int) $comment->comment_post_ID . '#comment-' . (int) $comment->comment_ID ) ),
                esc_html( human_time_diff( strtotime( $comment->comment_date_gmt . ' UTC' ) ) )
            );
        }
        echo '</ul>';
    } );
} );

Notes

A reply from any user who can edit posts counts as answered; looks at the last 60 days.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.