wppaste
WordPress

_custom_background_cb()

Since
3.0.0
Source
wp-includes/theme.php:1873
Default custom background callback.

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
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
28–128

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • hookthird-party callbacksapply_filters()one call below _custom_background_cb()

Further down the call graph this can also reach option, cache, serialize, transient and query those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 6 distinct outcomes

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

WhenInstructionsCalls it makes
!is_customize_preview()28–30get_background_image()set_url_scheme()get_background_color()get_theme_support()current_theme_supports()is_customize_preview()
is_customize_preview()32–34get_background_image()set_url_scheme()get_background_color()get_theme_support()current_theme_supports()is_customize_preview()printf()
always34–36get_background_image()set_url_scheme()get_background_color()get_theme_support()current_theme_supports()
always39–41get_background_image()set_url_scheme()get_background_color()get_theme_support()current_theme_supports()maybe_hash_hex_color()
always116–123get_background_image()set_url_scheme()get_background_color()get_theme_support()current_theme_supports()sanitize_url()get_theme_support()get_theme_mod()get_theme_support()get_theme_mod()get_theme_support()get_theme_mod()get_theme_support()get_theme_mod()get_theme_support()get_theme_mod()
always121–128get_background_image()set_url_scheme()get_background_color()get_theme_support()current_theme_supports()maybe_hash_hex_color()sanitize_url()get_theme_support()get_theme_mod()get_theme_support()get_theme_mod()get_theme_support()get_theme_mod()get_theme_support()get_theme_mod()get_theme_support()get_theme_mod()

Across PHP versions

PHPCompiledExecutedBranchesNotes
7.414228–13112More instructions than PHP 8.4
8.114128–13012More instructions than PHP 8.4
8.214128–13012More instructions than PHP 8.4
8.314128–13012More instructions than PHP 8.4
8.413928–12812Compiles 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 · 9

Source

function _custom_background_cb() {	// $background is the saved custom image, or the default image.	$background = set_url_scheme( get_background_image() ); 	/*	 * $color is the saved custom color.	 * A default has to be specified in style.css. It will not be printed here.	 */	$color = get_background_color(); 	if ( get_theme_support( 'custom-background', 'default-color' ) === $color ) {		$color = false;	} 	$type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"'; 	if ( ! $background && ! $color ) {		if ( is_customize_preview() ) {			printf( '<style%s id="custom-background-css"></style>', $type_attr );		}		return;	} 	$style = $color ? 'background-color: ' . maybe_hash_hex_color( $color ) . ';' : ''; 	if ( $background ) {		$image = ' background-image: url("' . sanitize_url( $background ) . '");'; 		// Background Position.		$position_x = get_theme_mod( 'background_position_x', get_theme_support( 'custom-background', 'default-position-x' ) );		$position_y = get_theme_mod( 'background_position_y', get_theme_support( 'custom-background', 'default-position-y' ) ); 		if ( ! in_array( $position_x, array( 'left', 'center', 'right' ), true ) ) {			$position_x = 'left';		} 		if ( ! in_array( $position_y, array( 'top', 'center', 'bottom' ), true ) ) {			$position_y = 'top';		} 		$position = " background-position: $position_x $position_y;"; 		// Background Size.		$size = get_theme_mod( 'background_size', get_theme_support( 'custom-background', 'default-size' ) ); 		if ( ! in_array( $size, array( 'auto', 'contain', 'cover' ), true ) ) {			$size = 'auto';		} 		$size = " background-size: $size;"; 		// Background Repeat.		$repeat = get_theme_mod( 'background_repeat', get_theme_support( 'custom-background', 'default-repeat' ) ); 		if ( ! in_array( $repeat, array( 'repeat-x', 'repeat-y', 'repeat', 'no-repeat' ), true ) ) {			$repeat = 'repeat';		} 		$repeat = " background-repeat: $repeat;"; 		// Background Scroll.		$attachment = get_theme_mod( 'background_attachment', get_theme_support( 'custom-background', 'default-attachment' ) ); 		if ( 'fixed' !== $attachment ) {			$attachment = 'scroll';		} 		$attachment = " background-attachment: $attachment;"; 		$style .= $image . $position . $size . $repeat . $attachment;	}	?><style<?php echo $type_attr; ?> id="custom-background-css">body.custom-background { <?php echo trim( $style ); ?> }</style>	<?php}

History

Introduced in 3.0.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 6.8.8 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.