To use a dropdown for the rating field instead of the star rating, use the filter hook below:
/**
* Removes the '.glsr-star-rating' class from the rating field.
* This will prevent the rating dropdown from being converted into a star rating control.
* @param string $field
* @param string $fieldType
* @param array $fieldValues
* @return string
*/
add_filter('site-reviews/rendered/field', function ($field, $fieldType, $fieldValues) {
$fieldName = glsr_get($fieldValues, 'path');
if ('rating' == $fieldName) {
$field = str_replace('glsr-star-rating', '', $field);
}
return $field;
}, 10, 3);
To change the text of the rating dropdown options, please use the Translations page in the Site Reviews Settings.
If you would like to add your own CSS class to specific fields, use the filter hook below:
/**
* Modifies the CSS classes used for the form fields
* @param array $classes
* @param array $field
* @return array
*/
add_filter('site-reviews/rendered/field/classes', function ($classes, $fieldValues) {
$fieldName = glsr_get($fieldValues, 'path');
$fieldNames = [
'email',
'name',
];
if (in_array($fieldName, $fieldNames)) {
$classes[] = 'column-class'; // add your class here as needed.
}
return $classes;
}, 10, 2);
Thanks for the reply.
I just tried the filter hook to add my own CSS class. It just throws a fatal error.
atal error: Method GeminiLabs\SiteReviews\Modules\Html\Field::__toString() must not throw an exception, caught Error: Call to undefined function glsr_get() in /home/servgr1allaboutc/public_html/ryp-reviews-custom/wp-content/plugins/site-reviews/plugin/Modules/Html/Partials/SiteReviewsForm.php on line 0
Also I want to change the way the review count is displayed (the stars) on individual reviews and wanted to show just the number and its corresponding value. eg instead of 2 stars images, I wanted to show the rating number 2 with its label “Average”.
1. Please make sure you are using the latest version of Site Reviews (v3.5.4). If you do not wish to do this, you can replace:
$fieldName = glsr_get($fieldValues, 'path');
With this:
$fieldName = isset($fieldValues['path'])
? $fieldValues['path']
: '';
2. You can do something like this:
/**
* Customise the HTML for the review rating
* @param array $renderedFields
* @param GeminiLabs\SiteReviews\Review $review
* @return array
*/
add_filter('site-reviews/review/build/after', function ($renderedFields, $review) {
if (isset($renderedFields['rating'])) {
$levels = [
'No Rating', // 0
'Terrible', // 1
'Poor', // 2
'Average', // 3
'Great', // 4
'Outstanding', // 5
];
$rating = intval($review->rating);
$ratingText = sprintf('%d, %s', $review->rating, $levels[$rating]);
$renderedFields['rating'] = '<div class="glsr-review-rating-text">'. $ratingText.'</div>';
}
return $renderedFields;
}, 10, 2);
I am using the latest version of Site Reviews (v3.5.4). Replaced the $fieldName as mentioned and it works.
Also, the filter to change the review stars to review count + label works flawlessly. Thanks
Another Query:
I have created a few custom fields via `add_filter( ‘site-reviews/config/forms/submission-form’, function( $config ) {}’. Now, how can I display them in the backend reviews list?
Thanks, It worked. Now, I want to hide certain fields on certain pages but currently can’t. how can we hide those custom fields from the shortcode using hide=””?
This is currently not possible with custom fields.
However, in v4.0 you will be able to do something like this:
/**
* Adds a shortcode/block option to hide a custom field.
*
* @param array $options
* @param string $shortcode
* @return array
*/
add_filter('site-reviews/shortcode/hide-options', function ($options, $shortcode) {
if (in_array($shortcode, ['site_reviews', 'site_reviews_form'])) {
$options['name_of_your_custom_key'] = __('Hide the custom key');
}
return $options;
}, 10, 2);
/**
* Renders the custom review fields.
* In order to display the rendered custom fields, you will need to use a custom "review.php"
* template as shown in the plugin FAQ ("How do I change the order of the review fields?")
* and you will need to add your custom keys to it, for example: {{ name_of_your_custom_key }}
*
* @param array $fields
* @param \GeminiLabs\SiteReviews\Review $review
* @param \GeminiLabs\SiteReviews\Modules\Html\Partials\SiteReviews $partial
* @return array
*/
add_filter('site-reviews/review/wrap', function ($fields, $review, $partial) {
foreach ($review->custom as $key => $value) {
if ($partial->isHidden($key) || array_key_exists($key, $fields)) {
continue;
}
$fields[$key] = '<p>'.$value.'</p>';
}
return $fields;
}, 10, 3);
As there has been no reply in over a week, I will mark this topic as resolved.