wppaste
WordPress

wp_star_rating( array $args = array() ): string

Since
3.8.0, 4.4.0
Source
wp-admin/includes/template.php:2745
Outputs a HTML element with a star rating for a given rating.

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: 0

    The 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: 0

    The number of ratings that makes up this rating.
  • $echobooldefault: true

    Whether 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

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
60–79

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 91.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
5

5 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_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.

WhenInstructionsCalls it makes
!$parsed_args60–61wp_parse_args(), floor(), ceil(), __(), number_format_i18n(), sprintf(), str_repeat(), str_repeat(), str_repeat()
!$parsed_args66–67wp_parse_args(), round(), floor(), ceil(), __(), number_format_i18n(), sprintf(), str_repeat(), str_repeat(), str_repeat()
$parsed_args72–73wp_parse_args(), floor(), ceil(), _n(), number_format_i18n(), number_format_i18n(), sprintf(), str_repeat(), str_repeat(), str_repeat()
$parsed_args78–79wp_parse_args(), round(), floor(), ceil(), _n(), number_format_i18n(), number_format_i18n(), sprintf(), str_repeat(), str_repeat(), str_repeat()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev9160–793
8.59160–793
8.49160–7933 fewer instructions than PHP 8.3
8.39463–823
8.29463–823
8.19463–823
7.49463–823

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

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

4.4.0
Introduced the echo parameter.from the docblock
3.8.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 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.