Custom_Image_Header::set_header_image( mixed $choice )
- Since
- 3.4.0
- Source
wp-admin/includes/class-custom-image-header.php:1173
Compatibility
- WordPress
- since 3.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
$choicemixed- Which header image to select. Allows for values of 'random-default-image', for randomly cycling among the default images; 'random-uploaded-image', for randomly cycling among the uploaded images; the key of a default image registered for that theme; and the key of an image uploaded for that theme (the attachment ID of the image). Or an array of arguments: attachment_id, url, width, height. All are required.
Performance profile
How much work a call to Custom_Image_Header::set_header_image() 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
- Heavy
- Scaling
- Constant
- Instructions
- 8–49
- Plugin surface
- None
- Called by
- 4
Reaches the database via get_posts().
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.
4 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 Custom_Image_Header::set_header_image() - optionoption read or write
get_option()one call below Custom_Image_Header::set_header_image() - querycontent query
get_posts()one call below Custom_Image_Header::set_header_image()
Further down the call graph this can also reach serialize, cache 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 · 6 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost Custom_Image_Header::set_header_image() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!isset($choice) | 8–12 | none |
!is_array($choice) && !is_object($choice) && $choice | 15 | set_theme_mod(), remove_theme_mod() |
!is_array($choice) && !is_object($choice) && !$choice && !isset($choice) | 17–19 | get_uploaded_header_images(), ->process_default_headers() |
!is_array($choice) && !is_object($choice) && !$choice && isset($uploaded[$choice]) | 29 | get_uploaded_header_images(), sanitize_url(), set_theme_mod(), set_theme_mod() |
!is_array($choice) && !is_object($choice) && !$choice && isset($choice) | 33–35 | get_uploaded_header_images(), ->process_default_headers(), sanitize_url(), set_theme_mod(), set_theme_mod() |
isset($choice) | 47–49 | sanitize_url(), get_stylesheet(), update_post_meta(), set_theme_mod(), set_theme_mod() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 91 instructions, 8–49 executed per call, 8 branches. The work does not change between versions.
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 · 7
- sanitize_url()Sanitizes a URL for database or redirect usage.
- update_post_meta()Updates a post meta field based on the given post ID.
- get_stylesheet()Retrieves name of the current stylesheet.
- set_theme_mod()Updates theme modification value for the active theme.
- remove_theme_mod()Removes theme modification name from active theme list.
- get_uploaded_header_images()Gets the header images uploaded for the active theme.
- Custom_Image_Header::process_default_headers()Processes the default headers.
Used by · 4
- Custom_Image_Header::remove_header_image()Removes a header image.
- Custom_Image_Header::step_2()Displays second step of custom header image page.
- Custom_Image_Header::step_3()Displays third step of custom header image page.
- Custom_Image_Header::take_action()Executes custom header modification.
Source code
final public function set_header_image( $choice ) { if ( is_array( $choice ) || is_object( $choice ) ) { $choice = (array) $choice; if ( ! isset( $choice['attachment_id'] ) || ! isset( $choice['url'] ) ) { return; } $choice['url'] = sanitize_url( $choice['url'] ); $header_image_data = (object) array( 'attachment_id' => $choice['attachment_id'], 'url' => $choice['url'], 'thumbnail_url' => $choice['url'], 'height' => $choice['height'], 'width' => $choice['width'], ); update_post_meta( $choice['attachment_id'], '_wp_attachment_is_custom_header', get_stylesheet() ); set_theme_mod( 'header_image', $choice['url'] ); set_theme_mod( 'header_image_data', $header_image_data ); return; } if ( in_array( $choice, array( 'remove-header', 'random-default-image', 'random-uploaded-image' ), true ) ) { set_theme_mod( 'header_image', $choice ); remove_theme_mod( 'header_image_data' ); return; } $uploaded = get_uploaded_header_images(); if ( $uploaded && isset( $uploaded[ $choice ] ) ) { $header_image_data = $uploaded[ $choice ]; } else { $this->process_default_headers(); if ( isset( $this->default_headers[ $choice ] ) ) { $header_image_data = $this->default_headers[ $choice ]; } else { return; } } set_theme_mod( 'header_image', sanitize_url( $header_image_data['url'] ) ); set_theme_mod( 'header_image_data', $header_image_data ); }Changelog
Introduced in 3.4.0. Unchanged from 6.7.7 through 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-admin/includes/class-custom-image-header.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.