wp_star_rating() – Outputs a HTML element with a star rating for a given rating.
You appear to be a bot. Output may be restricted
Description
Outputs a HTML element with a star rating for a given rating.
Outputs a HTML element with the star rating exposed on a 0..5 scale in half star increments (ie. 1, 1.5, 2 stars). Optionally, if specified, the number of ratings may also be displayed by passing the $number parameter.
Usage
$string = wp_star_rating( $args );
Parameters
- $args
- ( array ) optional – { Optional. Array of star ratings arguments.
- $rating
- ( int|float ) optional – The rating to display, expressed in either a 0.5 rating increment, or percentage. Default 0.
- $type
- ( string ) optional – Format that the $rating is in. Valid values are 'rating' (default), or, 'percent'. Default 'rating'.
- $number
- ( int ) optional – The number of ratings that makes up this rating. Default 0.
- $echo
- ( bool ) optional – Whether to echo the generated markup. False to return the markup instead of echoing it. Default true. }
Returns
string Star rating HTML.
Source
File name: wordpress/wp-admin/includes/template.php
Lines:
1 to 44 of 44
function wp_star_rating( $args = array() ) { $defaults = array( 'rating' => 0, 'type' => 'rating', 'number' => 0, 'echo' => true, ); $parsed_args = wp_parse_args( $args, $defaults ); // Non-English decimal places when the $rating is coming from a string. $rating = (float) str_replace( ',', '.', $parsed_args['rating'] ); // Convert percentage to star rating, 0..5 in .5 increments. if ( 'percent' === $parsed_args['type'] ) { $rating = round( $rating / 10, 0 ) / 2; } // Calculate the number of each type of star needed. $full_stars = floor( $rating ); $half_stars = ceil( $rating - $full_stars ); $empty_stars = 5 - $full_stars - $half_stars; if ( $parsed_args['number'] ) { /* translators: Hidden accessibility text. 1: The rating, 2: The number of ratings. */ $format = _n( '%1$s rating based on %2$s rating', '%1$s rating based on %2$s ratings', $parsed_args['number'] ); $title = sprintf( $format, number_format_i18n( $rating, 1 ), number_format_i18n( $parsed_args['number'] ) ); } else { /* translators: Hidden accessibility text. %s: The rating. */ $title = sprintf( __( '%s rating' ), number_format_i18n( $rating, 1 ) ); } $output = '<div class="star-rating">'; $output .= '<span class="screen-reader-text">' . $title . '</span>'; $output .= str_repeat( '<div class="star star-full" aria-hidden="true"></div>', $full_stars ); $output .= str_repeat( '<div class="star star-half" aria-hidden="true"></div>', $half_stars ); $output .= str_repeat( '<div class="star star-empty" aria-hidden="true"></div>', $empty_stars ); $output .= '</div>'; if ( $parsed_args['echo'] ) { echo $output; } return $output; }