Join the Voxel Guide Community!

Get Involved

Voxel Theme Version: 1.5.4 | Elementor Plugin Version: 3.28.0

You are now eligible to list your profile publicly!

Recent Reviews Snippet shortcode

Description/Instructions

The Recent Reviews Snippet shortcode is a handy tool for quickly showcasing snippets of recent feedback from your Voxel reviews. This shortcode retrieves the two most recent reviews from the Voxel timeline for the current (or specified) post—but only includes reviews that meet a minimum rating threshold. It then extracts and truncates the first sentence from each review (up to a specified number of words) so that you can display concise, impactful statements from your users.
This was meant to be added to the Preview Card template as seen in the screenshot, however you can add to any post template as per your needs.

IMPORTANT: If the code snippet provided in the Snippet Section below is broken, please get the code here 

Instructions

Create the Plugin File:

  • Open your favorite text editor.
  • Copy and paste the code below into a new file.

Save and Upload:

  • Save the file as voxel-recent-reviews-snippet.php.
  • Upload the file to your WordPress site's wp-content/plugins/ directory.

Activate the Plugin:

  • Log in to your WordPress admin dashboard.
  • Navigate to Plugins > Installed Plugins.
  • Locate “Voxel Recent Reviews Snippet” and click Activate.
  • Add the Shortcode to your preview template: [recent_reviews_snippet post_id="" word_count="10" min_rating="1"]

IMPORTANT: If the code snippet provided in the Snippet Section below is broken, please get the code here.

<?php
/**
* Plugin Name: Voxel Recent Reviews Snippet
* Description: Retrieves the two most recent reviews from the Voxel timeline and displays a very short sentence (truncated to a specified word count) from each review, filtered by a minimum rating.
* Version: 1.0
* Author: Miguel Gomes
*/

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* Shortcode: [recent_reviews_snippet post_id="" word_count="10" min_rating="1"]
*
* Retrieves the two most recent reviews from the Voxel timeline for the current (or specified) post
* that have a review_score greater than or equal to the provided min_rating, and displays a
* truncated first sentence from each review.
*
* Usage Examples:
* [recent_reviews_snippet]
* [recent_reviews_snippet post_id="123"]
* [recent_reviews_snippet post_id="123" word_count="10" min_rating="1"]
*/
function vre_recent_reviews_snippet_shortcode( $atts ) {
global $wpdb, $post;

$atts = shortcode_atts( array(
'post_id' => '',
'word_count' => 10,
'min_rating' => 1,
), $atts, 'recent_reviews_snippet' );

// Determine the target post ID.
$post_id = ! empty( $atts['post_id'] ) ? intval( $atts['post_id'] ) : ( isset( $post->ID ) ? $post->ID : 0 );
if ( ! $post_id ) {
return '

No post specified.

';
}

// Set the maximum word count.
$max_words = intval( $atts['word_count'] );
if ( $max_words <= 0 ) {
$max_words = 10;
}

// Set the minimum review score.
$min_rating = intval( $atts['min_rating'] );

// Query the two most recent reviews from the Voxel timeline table that meet the minimum rating.
$table_name = $wpdb->prefix . 'voxel_timeline';
$query = $wpdb->prepare(
"SELECT content FROM $table_name
WHERE details LIKE %s
AND post_id = %d
AND review_score >= %d
ORDER BY created_at DESC LIMIT 2",
'%"rating":%',
$post_id,
$min_rating
);
$reviews = $wpdb->get_results( $query );

if ( empty( $reviews ) ) {
return '

No reviews available for this post.

';
}

// Build the output.
$output = '

';

foreach ( $reviews as $review ) {
// Split the review text into sentences.
$sentences = preg_split( '/[.!?]\s+/', $review->content, -1, PREG_SPLIT_NO_EMPTY );

// Use the first sentence if available; otherwise, use the full review.
$short_sentence = ! empty( $sentences ) ? trim( $sentences[0] ) : trim( $review->content );

// Truncate the sentence to the specified maximum number of words.
$word_array = explode(' ', $short_sentence);
if ( count($word_array) > $max_words ) {
$short_sentence = implode(' ', array_slice($word_array, 0, $max_words)) . '...';
}

// Ensure the sentence ends with a period.
if ( substr( $short_sentence, -1 ) !== '.' ) {
$short_sentence .= '.';
}

$output .= '

' . esc_html( $short_sentence ) . '

';
}

$output .= '

';
return $output;
}
add_shortcode( 'recent_reviews_snippet', 'vre_recent_reviews_snippet_shortcode' );
?>

  • PHP
Copy Code

Let's Chat About this Snippet

Chat Toggle
Voxel Guide AI
Voxel Guide AI
Voxel Guide AI
Voxel Guide AI
Ask me anything about Voxel!
Send
Powered by AI24