wp_star_rating( array $args = array() ): string
- Since
- 3.8.0, 4.4.0
- Source
wp-admin/includes/template.php:2764
Description
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.
Compatibility
- WordPress
- since 4.4.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$argsarrayoptional- Array of star ratings arguments.Default:
array()$ratingint|floatdefault: 0The rating to display, expressed in either a 0.5 rating increment, or percentage.$typestringdefault: 'rating'Format that the $rating is in. Valid values are 'rating' (default), or, 'percent'.$numberintdefault: 0The number of ratings that makes up this rating.$echobooldefault: trueWhether to echo the generated markup. False to return the markup instead of echoing it.
Return value
string- Star rating HTML.
Performance profile
How much work a call to wp_star_rating() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Trivial
- Scaling
- Constant
- Instructions
- 60–79
- Plugin surface
- None
- Called by
- 5
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 91.
Nothing here hands control to plugin code.
5 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()one call below wp_star_rating()
Further down the call graph this can also reach query, option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 4 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_star_rating() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!$parsed_args | 60–61 | wp_parse_args(), floor(), ceil(), __(), number_format_i18n(), sprintf(), str_repeat(), str_repeat(), str_repeat() |
!$parsed_args | 66–67 | wp_parse_args(), round(), floor(), ceil(), __(), number_format_i18n(), sprintf(), str_repeat(), str_repeat(), str_repeat() |
$parsed_args | 72–73 | wp_parse_args(), floor(), ceil(), _n(), number_format_i18n(), number_format_i18n(), sprintf(), str_repeat(), str_repeat(), str_repeat() |
$parsed_args | 78–79 | wp_parse_args(), round(), floor(), ceil(), _n(), number_format_i18n(), number_format_i18n(), sprintf(), str_repeat(), str_repeat(), str_repeat() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 91 | 60–79 | 3 | |
| 8.5 | 91 | 60–79 | 3 | |
| 8.4 | 91 | 60–79 | 3 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 94 | 63–82 | 3 | |
| 8.2 | 94 | 63–82 | 3 | |
| 8.1 | 94 | 63–82 | 3 | |
| 7.4 | 94 | 63–82 | 3 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 4
- wp_parse_args()Merges user defined arguments into defaults array.
- _n()Translates and retrieves the singular or plural form based on the supplied number.
- number_format_i18n()Converts float number to format based on the locale.
- __()Retrieves the translation of $text.
Used by · 5
- WP_Customize_Manager::handle_load_themes_request()Loads themes into the theme browsing/installation UI.
- WP_Plugin_Install_List_Table::display_rows()Generates the list table rows.
- WP_Theme_Install_List_Table::install_theme_info()Prints the info for a theme (to be used in the theme installer modal).
- install_plugin_information()Displays plugin information in dialog box form.
- wp_ajax_query_themes()Handles getting themes from themes_api() via AJAX.
Source code
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;}Changelog
Introduced in 4.4.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
echo parameter.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-admin/includes/template.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.