wppaste
WordPress

_get_random_header_data(): object

Since
3.4.0
Source
wp-includes/theme.php:1395
Gets random header image data from registered images in theme.

Return

object

Cost profile

Measured from the compiled opcodes, not from a stopwatch. Every number here is identical on any machine that runs the same PHP version, and every function in core is ranked by these figures.

Cost class
Heavy

Reaches the database via get_posts().

Scaling
Constant

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

Instructions
5–55

Executed per call on PHP 8.4, depending on the branch taken. The body compiles to 64.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
2

2 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 _get_random_header_data()
  • querycontent queryget_posts()one call below _get_random_header_data()
  • optionoption read or writeget_option()one call below _get_random_header_data()

Further down the call graph this can also reach 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 · 7 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost _get_random_header_data() can have, taken from its control-flow graph on PHP 8.4.

WhenInstructionsCalls it makes
!empty($_wp_random_header)5none
empty($_wp_random_header) && $header_image_mod !== "random-uploaded-image" && empty($headers)19–23get_theme_mod()
empty($_wp_random_header) && $header_image_mod === "random-uploaded-image" && empty($headers)21get_theme_mod()get_uploaded_header_images()
empty($_wp_random_header) && $header_image_mod !== "random-uploaded-image" && !empty($_wp_default_headers) && $header_image_mod !== "random-default-image" && empty($headers)26–27get_theme_mod()current_theme_supports()
empty($_wp_random_header) && $header_image_mod !== "random-uploaded-image" && !empty($headers)47–51get_theme_mod()array_rand()get_template_directory_uri()get_stylesheet_directory_uri()sprintf()get_template_directory_uri()get_stylesheet_directory_uri()sprintf()
empty($_wp_random_header) && $header_image_mod === "random-uploaded-image" && !empty($headers)49get_theme_mod()get_uploaded_header_images()array_rand()get_template_directory_uri()get_stylesheet_directory_uri()sprintf()get_template_directory_uri()get_stylesheet_directory_uri()sprintf()
empty($_wp_random_header) && $header_image_mod !== "random-uploaded-image" && !empty($_wp_default_headers) && $header_image_mod !== "random-default-image" && !empty($headers)54–55get_theme_mod()current_theme_supports()array_rand()get_template_directory_uri()get_stylesheet_directory_uri()sprintf()get_template_directory_uri()get_stylesheet_directory_uri()sprintf()

Across PHP versions

PHPCompiledExecutedBranchesNotes
7.4645–556Compiles to the same instructions
8.1645–556Compiles to the same instructions
8.2645–556Compiles to the same instructions
8.3645–556Compiles to the same instructions
8.4645–556Compiles to the same instructions

Oldest PHP that compiles this body: 7.4. An instruction is not a fixed amount of time, so a version with the same count is not necessarily the same speed; what the count rules out is a difference in the work itself.

Uses · 6

Used by · 2

Source

function _get_random_header_data() {	global $_wp_default_headers;	static $_wp_random_header = null; 	if ( empty( $_wp_random_header ) ) {		$header_image_mod = get_theme_mod( 'header_image', '' );		$headers          = array(); 		if ( 'random-uploaded-image' === $header_image_mod ) {			$headers = get_uploaded_header_images();		} elseif ( ! empty( $_wp_default_headers ) ) {			if ( 'random-default-image' === $header_image_mod ) {				$headers = $_wp_default_headers;			} else {				if ( current_theme_supports( 'custom-header', 'random-default' ) ) {					$headers = $_wp_default_headers;				}			}		} 		if ( empty( $headers ) ) {			return new stdClass();		} 		$_wp_random_header = (object) $headers[ array_rand( $headers ) ]; 		$_wp_random_header->url = sprintf(			$_wp_random_header->url,			get_template_directory_uri(),			get_stylesheet_directory_uri()		); 		$_wp_random_header->thumbnail_url = sprintf(			$_wp_random_header->thumbnail_url,			get_template_directory_uri(),			get_stylesheet_directory_uri()		);	} 	return $_wp_random_header;}

History

Introduced in 3.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.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/theme.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.