Below, you’ll find a detailed guide on how to add the Strong Testimonials Shortcodes to your WordPress website, including their parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Strong Testimonials Plugin shortcodes not to show or not to work correctly.
Before starting, here is an overview of the Strong Testimonials Plugin and the shortcodes it provides:
"Strong Testimonials is a robust WordPress plugin designed to help you seamlessly collect and display testimonials on your website. Its user-friendly interface makes testimonial management easy and effective."
- [testimonial_average_rating_shortcode]
- [testimonial_count_shortcode]
- [testimonial_view_shortcode]
Strong Testimonials [testimonial_average_rating_shortcode] Shortcode
The Strong Testimonials shortcode is designed to display the average rating of testimonials on your WordPress site. This shortcode fetches all testimonials, calculates the average rating, and displays it along with the total count of ratings. It also includes options to customize the display style and HTML elements used. The shortcode allows you to filter testimonials by category, round off ratings, and control the number of decimal places shown.
Shortcode: [testimonial_average_rating_shortcode]
Parameters
Here is a list of all possible testimonial_average_rating_shortcode shortcode parameters and attributes:
average
– displays the average rating of testimonialscount
– shows the total number of testimonialsstars
– presents the average rating in star formatblock
– makes the rating display in block stylecentered
– centers the rating displayelement
– determines the HTML element for the rating wrapperclass
– allows additional CSS classes for the rating wrappercategory
– filters testimonials by categoryrounded
– rounds off the average ratingfield
– defines the field for calculating the average ratingdecimals
– sets the number of decimal places for the average rating
Examples and Usage
Basic example – Display the average rating of all testimonials.
[testimonial_average_rating /]
Advanced examples
Display the average rating of all testimonials in a specific category. Here, the category can be identified by its ID or slug.
[testimonial_average_rating category="5" /]
Display the average rating of all testimonials, rounded to the nearest whole number.
[testimonial_average_rating rounded="true" /]
Display the average rating of all testimonials, with a specific number of decimal places. Here, the number of decimal places is set to 2.
[testimonial_average_rating decimals="2" /]
Display the average rating of all testimonials, along with a count of the total number of ratings.
[testimonial_average_rating count="true" /]
Display the average rating of all testimonials, with the rating displayed as stars.
[testimonial_average_rating stars="true" /]
Display the average rating of all testimonials, centered within its containing element.
[testimonial_average_rating centered="true" /]
Display the average rating of all testimonials, as a block-level element.
[testimonial_average_rating block="true" /]
PHP Function Code
In case you have difficulties debugging what causing issues with [testimonial_average_rating_shortcode]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( $this->shortcode, array( $this, 'testimonial_average_rating_shortcode' ) );
Shortcode PHP function:
function testimonial_average_rating_shortcode( $atts, $content = null ) {
$pairs = array(
// parts
'average' => '',
'count' => '',
'stars' => '',
// style
'block' => '',
'centered' => '',
// HTML
'element' => 'div', // span
'class' => '', // on wrapper
// filters
'category' => '',
// rounded
'rounded' => '',
// field
'field' => '',
// decimals
'decimals' => 1
);
$pairs = apply_filters( "wpmtst_shortcode_defaults__{$this->shortcode}", $pairs );
$atts = shortcode_atts( $pairs, normalize_empty_atts( $atts ), $this->shortcode );
// default parts
if ( ! $content ) {
$content = '{title} {stars} {summary}';
}
// set parts
preg_match_all( "#{(.*?)}#", $content, $parts );
/*
* Example:
*
* Array
* (
* [0] => Array
* (
* [0] => {title}
* [1] => {stars}
* [2] => {summary}
* )
*
* [1] => Array
* (
* [0] => title
* [1] => stars
* [2] => summary
* )
* )
*/
$tag_list = $parts[0];
$tag_keys = $parts[1];
$parts = array_fill_keys( $tag_keys, '' );
// get posts
$args = array(
'posts_per_page' => -1,
'post_type' => 'wpm-testimonial',
'post_status' => 'publish',
'suppress_filters' => true,
);
// category
if ( $atts['category'] ) {
$args['tax_query'] = array(
array(
'taxonomy' => 'wpm-testimonial-category',
'field' => is_numeric( $atts['category'] ) ? 'id' : 'slug',
'terms' => $atts['category'],
),
);
}
$args = apply_filters( 'wpmtst_query_args', $args, $atts );
$posts_array = get_posts( $args );
// get summary
$summary = $this->get_summary( $posts_array, $atts['field'], $atts['decimals'] );
/*
* Example:
*
* Array
* (
* [review_count] => 2
* [rating_count] => 2
* [rating_sum] => 10
* [rating_average] => 5
* [rating_detail] => Array
* (
* [5] => 2
* [4] => 0
* [3] => 0
* [2] => 0
* [1] => 0
* [0] => 0
* )
* )
*/
// Want to build your own HTML? Return any truthy value to short-circuit this shortcode output.
$html = apply_filters( 'wpmtst_average_rating_pre_html', '', $atts, $summary );
if ( $html ) {
return $html;
}
// assemble classes
$class_list = array_filter( array_merge( array( 'strong-rating-wrapper', 'average' ), explode( ' ', $atts['class'] ) ) );
if ( $atts['block'] ) {
$class_list[] = 'block';
}
if ( $atts['centered'] ) {
$class_list[] = 'centered';
}
// round the rating if necessary
if(!empty($atts['rounded'])){
$rating_average = number_format($summary['rating_average'],0);
} else{
$rating_average = number_format($summary['rating_average'], absint($atts['decimals']));
}
// title
if ( isset( $parts['title'] ) ) {
$parts['title'] = sprintf( '<span class="strong-rating-title">%s</span>', esc_html__( 'Average Rating:', 'strong-testimonials' ) );
}
if ( isset( $parts['title2'] ) ) {
/* translators: %s is a number */
$count = sprintf( _n( 'Average of %s Rating:', 'Average of %s Ratings:', $summary['rating_count'], 'strong-testimonials' ), $summary['rating_count'] );
$parts['title2'] = sprintf( '<span class="strong-rating-title">%s</span>', $count );
}
// stars
if ( isset( $parts['stars'] ) ) {
$parts['stars'] = $this->print_stars( $rating_average);
}
// average
if ( isset( $parts['average'] ) ) {
$parts['average'] = sprintf( '<span class="strong-rating-average">%s</span>', $rating_average );
}
// count
if ( isset( $parts['count'] ) ) {
$parts['count'] = sprintf( '<span class="strong-rating-count">%s</span>', $summary['rating_count'] );
}
// summary phrase
if ( isset( $parts['summary'] ) ) {
/* translators: %s is a number */
$average = sprintf( _n( '%s star', '%s stars', $rating_average, 'strong-testimonials' ), $rating_average );
$count = sprintf( _n( '(based on %s rating)', '(based on %s ratings)', $summary['rating_count'], 'strong-testimonials' ), $summary['rating_count'] );
$parts['summary'] = sprintf( '<span class="strong-rating-summary">%s</span>', $average . ' ' . $count );
} elseif ( isset( $parts['summary2'] ) ) {
/* translators: %s is a number */
$average = sprintf( _n( '%s star', '%s stars', $rating_average, 'strong-testimonials' ), $rating_average );
$parts['summary2'] = sprintf( '<span class="strong-rating-summary">%s</span>', $average );
}
// replace tags
foreach ( $tag_list as $key => $tag ) {
$content = str_replace( $tag, $parts[ $tag_keys[ $key ] ], $content );
}
$allowed_elements = array( 'span', 'p', 'i', 'div', 'li', 'ul', 'ol', 'a', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6' );
$allowed_elements = apply_filters( 'wpmtst_allowed_shortcode_elements', $allowed_elements );
$element = in_array( $atts['element'], $allowed_elements ) ? esc_attr( $atts['element'] ) : 'div';
// assemble it.
$html = sprintf( '<%s class="%s">%s</%s>', esc_attr( $element ), esc_attr( implode( ' ', $class_list ) ), $content, esc_attr( $element ) );
wp_enqueue_style( 'wpmtst-rating-display' );
return apply_filters( 'wpmtst_average_rating_html', $html, $atts, $summary );
}
Code file location:
strong-testimonials/strong-testimonials/includes/class-strong-testimonials-shortcode-average.php
Strong Testimonials [testimonial_count_shortcode] Shortcode
The Strong Testimonials plugin shortcode allows you to display the count of testimonials on your website. It fetches all published testimonials, and if a category is specified, it filters the testimonials based on that category.
Shortcode: [testimonial_count_shortcode]
Parameters
Here is a list of all possible testimonial_count_shortcode shortcode parameters and attributes:
category
– specifies the category of testimonials to countunformatted
– determines if the count should be formatted or not
Examples and Usage
Basic example – Showcases a simple usage of the testimonial count shortcode without any additional parameters.
[testimonial_count /]
Advanced examples
Displaying the count of testimonials under a specific category. Here, the category is specified by its slug, ‘customer-reviews’.
[testimonial_count category="customer-reviews" /]
Returning the count in an unformatted manner, which means the count will be returned as a raw integer without any formatting applied.
[testimonial_count unformatted=1 /]
Combining both the category and unformatted parameters. This will return the raw count of testimonials under the ‘customer-reviews’ category.
[testimonial_count category="customer-reviews" unformatted=1 /]
PHP Function Code
In case you have difficulties debugging what causing issues with [testimonial_count_shortcode]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( $this->shortcode, array( $this, 'testimonial_count_shortcode' ) );
Shortcode PHP function:
function testimonial_count_shortcode( $atts, $content = null ) {
$pairs = array(
'category' => '',
'unformatted' => 0,
);
$pairs = apply_filters( "wpmtst_shortcode_defaults__{$this->shortcode}", $pairs );
$atts = shortcode_atts( $pairs, normalize_empty_atts( $atts ), $this->shortcode );
$args = array(
'posts_per_page' => -1,
'post_type' => 'wpm-testimonial',
'post_status' => 'publish',
'suppress_filters' => true,
);
if ( $atts['category'] ) {
$args['tax_query'] = array(
array(
'taxonomy' => 'wpm-testimonial-category',
'field' => 'slug',
'terms' => $atts['category'],
)
);
}
$args = apply_filters( 'wpmtst_query_args', $args, $atts );
$posts_array = get_posts( $args );
$count = count( $posts_array );
if ( $atts['unformatted'] ) {
return $count;
}
return number_format_i18n( $count );
}
Code file location:
strong-testimonials/strong-testimonials/includes/class-strong-testimonials-shortcode-count.php
Strong Testimonials [testimonial_view_shortcode] Shortcode
The Strong Testimonials shortcode is a powerful tool that displays testimonial views. It is defined by the ‘testimonial_view_shortcode’ function. This shortcode accepts attributes and content, returning a rendered view. It uses the ‘shortcode_atts’ function to combine user-defined attributes with known attributes, allowing customization.
Shortcode: [testimonial_view_shortcode]
Examples and Usage
Basic example – Showcases a simple usage of the shortcode with a single attribute.
[testimonial_view id=1 /]
Advanced examples
Displaying a testimonial view by referencing both ID and title. The shortcode will first try to load the testimonial by ID, but if not found, it will attempt to load by title.
[testimonial_view id=1 title="Testimonial Title" /]
Using the shortcode to display a testimonial view with multiple parameters. This example includes the ID, title, and a custom attribute.
[testimonial_view id=1 title="Testimonial Title" custom_attr="Custom Value" /]
Applying the shortcode to load a testimonial view by ID, along with a fallback title and a custom attribute. This example demonstrates how to use the shortcode with multiple attributes to create a more complex and flexible output.
[testimonial_view id=1 fallback_title="Fallback Testimonial Title" custom_attr="Custom Value" /]
PHP Function Code
In case you have difficulties debugging what causing issues with [testimonial_view_shortcode]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode( $this->shortcode, array( $this, 'testimonial_view_shortcode' ) );
Shortcode PHP function:
function testimonial_view_shortcode( $atts, $content = null ) {
$out = shortcode_atts( // phpcs:ignore sanitized in render_view
array(),
$atts,
$this->shortcode
);
return $this->render_view( $out );
}
Code file location:
strong-testimonials/strong-testimonials/includes/class-strong-testimonials-shortcode.php
Conclusion
Now that you’ve learned how to embed the Strong Testimonials Plugin shortcodes, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.
Leave a Reply