Get Involved
No items added to cart
No items added to cart
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
Create the Plugin File:
Save and Upload:
Activate the Plugin:
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' );
?>
Are you sure you want to exit? Your current conversation will be lost.