JReviews logo
Guides My site
← All guides Community guide

Use text labels for review form rating options

V Alejandro
Thread started by Vasilij. Contributors Alejandro.
Thread started
Last updated

What you’ll achieve

Show text labels such as Terrible and Excellent for five-point rating choices in review forms.

My site

Platform
JReviews version

Show text labels for the ratings in five-point review forms. Saved scores, stars, and totals keep their numeric values.

Prepare five-point review criteria

Use this example on a site whose Configuration > Reviews > Rating Settings > Rating scale is already 5. Do not change the scale of existing reviews to fit this example. Set Rating increment to 1, and check that the target listing type also uses an increment of 1 rather than overriding it with half points.

In the target listing type, allow comments and ratings and enable user reviews. In Rating Criteria, create Service as a required criterion and Value as an optional criterion. Give each a weight of 50. These are review rating criteria, not custom fields, so they do not need a field group or predefined field options.

Reviewers fill these criteria when they submit or edit a review. The code supplies the displayed choices: Terrible saves 1, Not so bad saves 2, Just ok saves 3, Good saves 4, and Excellent saves 5. An optional criterion also permits N/A. A reviewer who chooses Good for Service saves a Service score of 4.

Keep the customization in your JReviews overrides. Start a new PHP file with <?php followed by defined('MVC_FRAMEWORK') or die;. In an existing file, add the callback without repeating the opening tag or guard. If a snippet includes those lines, keep each only once.

JReviews 6 Early Access

Open Configuration > Early Access Settings and enable Listing Detail Page. That switch supplies the modern review form on a listing detail page. To apply the labels to an initial review submitted with a new listing, also enable Listing Form. The switches are independent.

Add the PHP code to your overrides

WordPress. Add the PHP example to jreviews_overrides/hooks.php.

Joomla. Add the PHP example to templates/jreviews_overrides/hooks.php.

Show a label next to each rating

The jreviews:review_form.rating_labels filter names each rating value. While a reviewer hovers over or selects a star, the form shows its label next to the number, for example 5 Excellent. You keep the stars and your rating selector. If the review form uses the dropdown selector, the labels replace the numbers in its list.

fwd_add_filter('jreviews:review_form.rating_labels', function (array $labels, $criterion, $comment): array {
    return [1 => 'Terrible', 2 => 'Not so bad', 3 => 'Just ok', 4 => 'Good', 5 => 'Excellent'];
}, 20, 3);

The callback returns one label for each value. The labels change only the text the reviewer sees. Saved scores stay numeric, and the form offers the same values as before. Your rating scale and increment decide those values. An optional criterion keeps its button for clearing the rating, and a required criterion still needs a score.

A value without a label shows only its number. With half-point increments, this example labels the whole numbers and leaves 0.5, 1.5 and the other halves as numbers. To name a half point, add it with a quoted key, such as '4.5' => 'Very good'. Do not write 4.5 => 'Very good', because PHP turns the key 4.5 into 4. The labels follow the number, so on a scale other than five, change the list to match it. The filter receives three arguments. $labels holds the values the form offers, each labelled with its own number, so its keys show the scale and step. $criterion is the rating criterion being rendered, for example $criterion->title. $comment is the review being written or edited, and it is also present on a new review, so $comment->listing_type and $comment->listing_id are available if you need different labels for different criteria, listing types or listings. The 20, 3 at the end registers the callback with priority 20 and tells JReviews it takes three arguments.

On a multilingual site, you can wrap each label in fwd__() with a key from your language overrides.

Add text with CSS instead

If you only want to style the text, you can skip the PHP code. The number beside the stars has a data-jr-rating-value attribute that holds the value on show, such as 5 or 4.5. The attribute is removed when nothing is selected, so no text appears for an empty rating. Add a rule like this to your site's stylesheet:

[data-jr-rating-value="5"]::after { content: " Excellent"; }

Add one rule for each value you want to name.

To give each rating criterion its own words, use the data-jr-rating-criterion attribute. Each criterion's row in the review form carries its ID. You can find the ID in the ID column of the Rating Criteria table when you edit a listing type in the control panel. This rule adds the text only to the criterion with ID 37:

[data-jr-rating-criterion="37"] [data-jr-rating-value="5"]::after { content: " Excellent"; }

Repeat the rule with another ID and other words for each criterion.

JReviews 5 and v6 Legacy

In JReviews configuration, choose the Select rating selector for desktop and mobile review forms so the labels appear as dropdown choices.

Add the code to your overrides

Joomla. Use templates/jreviews_overrides/filters/filter_functions.php.

WordPress. Use jreviews_overrides/filters/filter_functions.php.

Replace review form rating choices with text

Use the review_form_rating_options filter to label a five-point review form with text. This controls the form choices. It does not replace stars or numeric totals elsewhere.

Define and register the filter

function review_form_rating_options($options, $params)
{
    // Uncomment line below to dump filter arguments to the screen
    // dd($options, $params);

    $listingTypeId = S2Array::get($params,'listing_type_id');

    $reviewType = S2Array::get($params,'review_type');

    $required = S2Array::get($params,'required');

    $scale = S2Array::get($params,'scale');

    $increment = S2Array::get($params,'increment');

    if ((float) $scale !== 5.0 || (float) $increment !== 1.0) {
        return $options;
    }

    $options = array_key_exists('', $options) ? ['' => $options['']] : [];

    if ( !$required )
    {
        $options['na'] = 'N/A';
    }

    $options[1] = 'Terrible';
    $options[2] = 'Not so bad';
    $options[3] = 'Just ok';
    $options[4] = 'Good';
    $options[5] = 'Excellent';

    return $options;
}

The function declaration alone does not register a callback. After that function in the same file, add this registration.

Clickfwd\Hook\Filter::add('review_form_rating_options', 'review_form_rating_options', 10);

Keep both blocks in the same override file. The second block makes JReviews use the labels defined in the first.

Customize the choices

Change the label strings to match your scale. The example preserves an existing empty placeholder option and defines labels for values one through five. Other rating scales and increments keep their original options. It preserves N/A when the criterion is optional.

The complete example above supports whole-number scores on a five-point scale. Adapting it to half points requires a PHP developer. A developer must use a separate callback with a quoted key for every value, for example $options['1.5'] = 'Below average';. Do not use floating-point array keys such as $options[1.5], which PHP converts to integers. Adjust the scale/increment guard to match. Replacing the options array also replaces the choices normally generated from the rating increment. You can use listing_type_id, review_type, required, scale, and increment from the parameter array to choose different labels.

Check the rating choices

Open a review form with required and optional five-point criteria. Confirm the text labels and the optional N/A choice. Save a review and reopen it. Confirm that the same score remains selected. Check a phone-sized screen and another rating scale. The other scale must keep its configured choices, and stars and numeric totals must remain unchanged.

Learn more