Halloween Sale 15% off Ends in See the plans
PHP

Latest Product Reviews Dashboard Widget

Shows the five newest product reviews with star rating, product and approval status on the dashboard, so you can answer or approve them quickly.

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_latest_reviews', 'Latest product reviews', function () {
        $reviews = get_comments( array(
            'post_type' => 'product',
            'type'      => 'review',
            'status'    => 'all',
            'number'    => 5,
        ) );

        if ( ! $reviews ) {
            echo '<p>' . esc_html( 'No reviews yet.' ) . '</p>';
            return;
        }

        echo '<ul>';

        foreach ( $reviews as $review ) {
            $rating = (int) get_comment_meta( $review->comment_ID, 'rating', true );

            printf(
                '<li style="margin-bottom:10px"><span aria-label="%1$s" style="color:#dba617">%2$s</span> <a href="%3$s">%4$s</a>%5$s<br><em>%6$s</em> – %7$s</li>',
                esc_attr( sprintf( 'Rated %d out of 5', $rating ) ),
                esc_html( str_repeat( '★', $rating ) . str_repeat( '☆', 5 - $rating ) ),
                esc_url( get_edit_post_link( $review->comment_post_ID ) ),
                esc_html( get_the_title( $review->comment_post_ID ) ),
                '0' === $review->comment_approved ? ' <strong style="color:#d63638">' . esc_html( '(pending)' ) . '</strong>' : '',
                esc_html( wp_trim_words( $review->comment_content, 18 ) ),
                esc_html( $review->comment_author )
            );
        }

        echo '</ul>';
    } );
} );

Notes

Pending reviews are marked so you can moderate them from Products > Reviews.

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.