WP_Customize_Header_Image_Setting::sanitize_choice( mixed $value ): array|string|WP_Error|null
- Since
- 7.1.1
- Source
wp-includes/customize/class-wp-customize-header-image-setting.php:134
Description
This is the value which is ultimately passed to Custom_Image_Header::set_header_image(), whether supplied at the top level of the setting value or nested under its legacy choice key.
Compatibility
- WordPress
- since 7.1.1
- PHP
- 7.4–8.6-dev
- 6.7.10
- 6.8.11
- 6.9.10
- 7.0.7
- 7.1.3
Present in every tracked release (6.7.10 to 7.1.3), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$valuemixed- Value to sanitize.
Return value
array|string|WP_Error|null- Sanitized value, or
null/WP_Errorif invalid.
Performance profile
How much work a call to WP_Customize_Header_Image_Setting::sanitize_choice() 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–124
- Plugin surface
- None
- Called by
- 1
Reaches the database via get_post().
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 154.
Nothing here hands control to plugin code.
1 place 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_Customize_Header_Image_Setting::sanitize_choice() - querycontent query
get_post()one call below WP_Customize_Header_Image_Setting::sanitize_choice()
Further down the call graph this can also reach 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 · 113 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Customize_Header_Image_Setting::sanitize_choice() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!is_string($value) | 8–30 | none |
| always | 9–40 | sanitize_text_field() |
!is_string($value) && is_array($value) && isset($value) && $attachment_id | 28–51 | absint(), get_post_mime_type() |
!is_string($value) && is_array($value) && isset($value) | 29–43 | absint() |
!is_string($value) && is_array($value) | 31–43 | sanitize_url() |
!is_string($value) && is_array($value) | 33–37 | sanitize_url(), __() |
!is_string($value) && is_array($value) | 44–53 | absint(), absint() |
!is_string($value) && is_array($value) && isset($value) && !$attachment_id | 44–56 | absint(), sanitize_url() |
!is_string($value) && is_array($value) | 45–47 | sanitize_text_field(), absint() |
!is_string($value) && is_array($value) | 45–53 | absint(), sanitize_text_field() |
!is_string($value) && is_array($value) | 45–53 | sanitize_url(), absint() |
!is_string($value) && is_array($value) | 46–56 | sanitize_url(), sanitize_url() |
101 further outcomes, up to 124 instructions
!is_string($value) && is_array($value) && isset($value) && !$attachment_id | 46–50 | absint(), sanitize_url(), __() |
!is_string($value) && is_array($value) | 48–53 | sanitize_url(), sanitize_text_field() |
!is_string($value) && is_array($value) | 48–50 | sanitize_url(), sanitize_url(), __() |
!is_string($value) && is_array($value) && isset($value) && $attachment_id && is_string($mime_type) && $mime_type | 52–64 | absint(), get_post_mime_type(), sanitize_url() |
!is_string($value) && is_array($value) && $attachment_id && is_string($mime_type) && $mime_type | 53–61 | absint(), get_post_mime_type(), absint() |
!is_string($value) && is_array($value) && isset($value) && $attachment_id && is_string($mime_type) && $mime_type | 54–58 | absint(), get_post_mime_type(), sanitize_url(), __() |
!is_string($value) && is_array($value) | 55–60 | absint(), sanitize_text_field(), absint() |
!is_string($value) && is_array($value) | 55–63 | absint(), absint(), absint() |
!is_string($value) && is_array($value) | 55–63 | absint(), absint(), sanitize_text_field() |
!is_string($value) && is_array($value) && $attachment_id && is_string($mime_type) && $mime_type | 56–61 | absint(), get_post_mime_type(), sanitize_text_field() |
!is_string($value) && is_array($value) | 57–63 | sanitize_url(), absint(), absint() |
!is_string($value) && is_array($value) | 58–60 | sanitize_url(), sanitize_text_field(), absint() |
!is_string($value) && is_array($value) | 58–63 | sanitize_url(), absint(), sanitize_text_field() |
!is_string($value) && is_array($value) | 58–66 | sanitize_url(), sanitize_url(), absint() |
!is_string($value) && is_array($value) && !$attachment_id | 58–66 | absint(), sanitize_url(), absint() |
!is_string($value) && is_array($value) && isset($value) && !$attachment_id | 59–69 | absint(), sanitize_url(), sanitize_url() |
!is_string($value) && is_array($value) | 61–66 | sanitize_url(), sanitize_url(), sanitize_text_field() |
!is_string($value) && is_array($value) && !$attachment_id | 61–66 | absint(), sanitize_url(), sanitize_text_field() |
!is_string($value) && is_array($value) && isset($value) && !$attachment_id | 61–63 | absint(), sanitize_url(), sanitize_url(), __() |
!is_string($value) && is_array($value) | 65–70 | absint(), absint(), sanitize_text_field(), absint() |
!is_string($value) && is_array($value) | 65–73 | absint(), absint(), absint(), absint() |
!is_string($value) && is_array($value) | 65–73 | absint(), absint(), absint(), sanitize_text_field() |
!is_string($value) && is_array($value) && $attachment_id && is_string($mime_type) && $mime_type | 65–71 | absint(), get_post_mime_type(), absint(), absint() |
!is_string($value) && is_array($value) && $attachment_id && is_string($mime_type) && $mime_type | 66–68 | absint(), get_post_mime_type(), sanitize_text_field(), absint() |
!is_string($value) && is_array($value) && $attachment_id && is_string($mime_type) && $mime_type | 66–71 | absint(), get_post_mime_type(), absint(), sanitize_text_field() |
!is_string($value) && is_array($value) && $attachment_id && is_string($mime_type) && $mime_type | 66–74 | absint(), get_post_mime_type(), sanitize_url(), absint() |
!is_string($value) && is_array($value) && isset($value) && $attachment_id && is_string($mime_type) && $mime_type | 67–77 | absint(), get_post_mime_type(), sanitize_url(), sanitize_url() |
!is_string($value) && is_array($value) | 68–70 | sanitize_url(), absint(), sanitize_text_field(), absint() |
!is_string($value) && is_array($value) | 68–73 | sanitize_url(), absint(), absint(), absint() |
!is_string($value) && is_array($value) | 68–73 | sanitize_url(), absint(), absint(), sanitize_text_field() |
!is_string($value) && is_array($value) && $attachment_id && is_string($mime_type) && $mime_type | 69–74 | absint(), get_post_mime_type(), sanitize_url(), sanitize_text_field() |
!is_string($value) && is_array($value) && isset($value) && $attachment_id && is_string($mime_type) && $mime_type | 69–71 | absint(), get_post_mime_type(), sanitize_url(), sanitize_url(), __() |
!is_string($value) && is_array($value) | 70–76 | sanitize_url(), sanitize_url(), absint(), absint() |
!is_string($value) && is_array($value) && !$attachment_id | 70–76 | absint(), sanitize_url(), absint(), absint() |
!is_string($value) && is_array($value) | 71–73 | sanitize_url(), sanitize_url(), sanitize_text_field(), absint() |
!is_string($value) && is_array($value) | 71–76 | sanitize_url(), sanitize_url(), absint(), sanitize_text_field() |
!is_string($value) && is_array($value) && !$attachment_id | 71–73 | absint(), sanitize_url(), sanitize_text_field(), absint() |
!is_string($value) && is_array($value) && !$attachment_id | 71–76 | absint(), sanitize_url(), absint(), sanitize_text_field() |
!is_string($value) && is_array($value) && isset($value) && !$attachment_id | 71–79 | absint(), sanitize_url(), sanitize_url(), absint() |
!is_string($value) && is_array($value) && !$attachment_id | 74–79 | absint(), sanitize_url(), sanitize_url(), sanitize_text_field() |
!is_string($value) && is_array($value) | 75–80 | absint(), absint(), absint(), sanitize_text_field(), absint() |
!is_string($value) && is_array($value) && $attachment_id && is_string($mime_type) && $mime_type | 76–78 | absint(), get_post_mime_type(), absint(), sanitize_text_field(), absint() |
!is_string($value) && is_array($value) && $attachment_id && is_string($mime_type) && $mime_type | 76–81 | absint(), get_post_mime_type(), absint(), absint(), absint() |
!is_string($value) && is_array($value) && $attachment_id && is_string($mime_type) && $mime_type | 76–81 | absint(), get_post_mime_type(), absint(), absint(), sanitize_text_field() |
!is_string($value) && is_array($value) | 78–80 | sanitize_url(), absint(), absint(), sanitize_text_field(), absint() |
!is_string($value) && is_array($value) | 78–80 | sanitize_url(), absint(), absint(), absint(), absint() |
!is_string($value) && is_array($value) | 78–83 | sanitize_url(), absint(), absint(), absint(), sanitize_text_field() |
!is_string($value) && is_array($value) && !$attachment_id | 78–80 | absint(), absint(), absint(), absint(), absint() |
!is_string($value) && is_array($value) && !$attachment_id | 78–83 | absint(), absint(), absint(), absint(), sanitize_text_field() |
!is_string($value) && is_array($value) && $attachment_id && is_string($mime_type) && $mime_type | 78–84 | absint(), get_post_mime_type(), sanitize_url(), absint(), absint() |
!is_string($value) && is_array($value) && $attachment_id && is_string($mime_type) && $mime_type | 79–81 | absint(), get_post_mime_type(), sanitize_url(), sanitize_text_field(), absint() |
!is_string($value) && is_array($value) && $attachment_id && is_string($mime_type) && $mime_type | 79–84 | absint(), get_post_mime_type(), sanitize_url(), absint(), sanitize_text_field() |
!is_string($value) && is_array($value) && isset($value) && $attachment_id && is_string($mime_type) && $mime_type | 79–87 | absint(), get_post_mime_type(), sanitize_url(), sanitize_url(), absint() |
!is_string($value) && is_array($value) | 81–83 | sanitize_url(), sanitize_url(), absint(), sanitize_text_field(), absint() |
!is_string($value) && is_array($value) | 81–86 | sanitize_url(), sanitize_url(), absint(), absint(), absint() |
!is_string($value) && is_array($value) | 81–86 | sanitize_url(), sanitize_url(), absint(), absint(), sanitize_text_field() |
!is_string($value) && is_array($value) && !$attachment_id | 81–83 | absint(), sanitize_url(), absint(), sanitize_text_field(), absint() |
!is_string($value) && is_array($value) && !$attachment_id | 81–86 | absint(), sanitize_url(), absint(), absint(), absint() |
!is_string($value) && is_array($value) && !$attachment_id | 81–86 | absint(), sanitize_url(), absint(), absint(), sanitize_text_field() |
!is_string($value) && is_array($value) && $attachment_id && is_string($mime_type) && $mime_type | 82–87 | absint(), get_post_mime_type(), sanitize_url(), sanitize_url(), sanitize_text_field() |
!is_string($value) && is_array($value) && isset($value) && !$attachment_id | 83–89 | absint(), sanitize_url(), sanitize_url(), absint(), absint() |
!is_string($value) && is_array($value) && !$attachment_id | 84–86 | absint(), sanitize_url(), sanitize_url(), sanitize_text_field(), absint() |
!is_string($value) && is_array($value) && !$attachment_id | 84–89 | absint(), sanitize_url(), sanitize_url(), absint(), sanitize_text_field() |
!is_string($value) && is_array($value) && $attachment_id && is_string($mime_type) && $mime_type | 86–88 | absint(), get_post_mime_type(), absint(), absint(), sanitize_text_field(), absint() |
!is_string($value) && is_array($value) && $attachment_id && is_string($mime_type) && $mime_type | 86–88 | absint(), get_post_mime_type(), absint(), absint(), absint(), absint() |
!is_string($value) && is_array($value) && $attachment_id && is_string($mime_type) && $mime_type | 86–91 | absint(), get_post_mime_type(), absint(), absint(), absint(), sanitize_text_field() |
!is_string($value) && is_array($value) | 88–90 | sanitize_url(), absint(), absint(), absint(), sanitize_text_field(), absint() |
!is_string($value) && is_array($value) && !$attachment_id | 88–90 | absint(), absint(), absint(), absint(), sanitize_text_field(), absint() |
!is_string($value) && is_array($value) && $attachment_id && is_string($mime_type) && $mime_type | 89–91 | absint(), get_post_mime_type(), sanitize_url(), absint(), sanitize_text_field(), absint() |
!is_string($value) && is_array($value) && $attachment_id && is_string($mime_type) && $mime_type | 89–94 | absint(), get_post_mime_type(), sanitize_url(), absint(), absint(), absint() |
!is_string($value) && is_array($value) && $attachment_id && is_string($mime_type) && $mime_type | 89–94 | absint(), get_post_mime_type(), sanitize_url(), absint(), absint(), sanitize_text_field() |
!is_string($value) && is_array($value) | 91–93 | sanitize_url(), sanitize_url(), absint(), absint(), sanitize_text_field(), absint() |
!is_string($value) && is_array($value) | 91–93 | sanitize_url(), sanitize_url(), absint(), absint(), absint(), absint() |
!is_string($value) && is_array($value) | 91–96 | sanitize_url(), sanitize_url(), absint(), absint(), absint(), sanitize_text_field() |
!is_string($value) && is_array($value) && !$attachment_id | 91–93 | absint(), sanitize_url(), absint(), absint(), sanitize_text_field(), absint() |
!is_string($value) && is_array($value) && !$attachment_id | 91–93 | absint(), sanitize_url(), absint(), absint(), absint(), absint() |
!is_string($value) && is_array($value) && !$attachment_id | 91–96 | absint(), sanitize_url(), absint(), absint(), absint(), sanitize_text_field() |
!is_string($value) && is_array($value) && isset($value) && $attachment_id && is_string($mime_type) && $mime_type | 91–97 | absint(), get_post_mime_type(), sanitize_url(), sanitize_url(), absint(), absint() |
!is_string($value) && is_array($value) && $attachment_id && is_string($mime_type) && $mime_type | 92–94 | absint(), get_post_mime_type(), sanitize_url(), sanitize_url(), sanitize_text_field(), absint() |
!is_string($value) && is_array($value) && $attachment_id && is_string($mime_type) && $mime_type | 92–97 | absint(), get_post_mime_type(), sanitize_url(), sanitize_url(), absint(), sanitize_text_field() |
!is_string($value) && is_array($value) && !$attachment_id | 94–96 | absint(), sanitize_url(), sanitize_url(), absint(), sanitize_text_field(), absint() |
!is_string($value) && is_array($value) && isset($value) && !$attachment_id | 94–99 | absint(), sanitize_url(), sanitize_url(), absint(), absint(), absint() |
!is_string($value) && is_array($value) && !$attachment_id | 94–99 | absint(), sanitize_url(), sanitize_url(), absint(), absint(), sanitize_text_field() |
!is_string($value) && is_array($value) && $attachment_id && is_string($mime_type) && $mime_type | 96–98 | absint(), get_post_mime_type(), absint(), absint(), absint(), sanitize_text_field(), absint() |
!is_string($value) && is_array($value) && $attachment_id && is_string($mime_type) && $mime_type | 99–101 | absint(), get_post_mime_type(), sanitize_url(), absint(), absint(), sanitize_text_field(), absint() |
!is_string($value) && is_array($value) && $attachment_id && is_string($mime_type) && $mime_type | 99–101 | absint(), get_post_mime_type(), sanitize_url(), absint(), absint(), absint(), absint() |
!is_string($value) && is_array($value) && $attachment_id && is_string($mime_type) && $mime_type | 99–104 | absint(), get_post_mime_type(), sanitize_url(), absint(), absint(), absint(), sanitize_text_field() |
!is_string($value) && is_array($value) | 101–103 | sanitize_url(), sanitize_url(), absint(), absint(), absint(), sanitize_text_field(), absint() |
!is_string($value) && is_array($value) && !$attachment_id | 101–103 | absint(), sanitize_url(), absint(), absint(), absint(), sanitize_text_field(), absint() |
!is_string($value) && is_array($value) && $attachment_id && is_string($mime_type) && $mime_type | 102–104 | absint(), get_post_mime_type(), sanitize_url(), sanitize_url(), absint(), sanitize_text_field(), absint() |
!is_string($value) && is_array($value) && isset($value) && $attachment_id && is_string($mime_type) && $mime_type | 102–107 | absint(), get_post_mime_type(), sanitize_url(), sanitize_url(), absint(), absint(), absint() |
!is_string($value) && is_array($value) && $attachment_id && is_string($mime_type) && $mime_type | 102–107 | absint(), get_post_mime_type(), sanitize_url(), sanitize_url(), absint(), absint(), sanitize_text_field() |
!is_string($value) && is_array($value) && !$attachment_id | 104–106 | absint(), sanitize_url(), sanitize_url(), absint(), absint(), sanitize_text_field(), absint() |
!is_string($value) && is_array($value) && !$attachment_id | 104–106 | absint(), sanitize_url(), sanitize_url(), absint(), absint(), absint(), absint() |
!is_string($value) && is_array($value) && isset($value) && !$attachment_id | 104–109 | absint(), sanitize_url(), sanitize_url(), absint(), absint(), absint(), sanitize_text_field() |
!is_string($value) && is_array($value) && $attachment_id && is_string($mime_type) && $mime_type | 109–111 | absint(), get_post_mime_type(), sanitize_url(), absint(), absint(), absint(), sanitize_text_field(), absint() |
!is_string($value) && is_array($value) && $attachment_id && is_string($mime_type) && $mime_type | 112–114 | absint(), get_post_mime_type(), sanitize_url(), sanitize_url(), absint(), absint(), sanitize_text_field(), absint() |
!is_string($value) && is_array($value) && $attachment_id && is_string($mime_type) && $mime_type | 112–114 | absint(), get_post_mime_type(), sanitize_url(), sanitize_url(), absint(), absint(), absint(), absint() |
!is_string($value) && is_array($value) && isset($value) && $attachment_id && is_string($mime_type) && $mime_type | 112–117 | absint(), get_post_mime_type(), sanitize_url(), sanitize_url(), absint(), absint(), absint(), sanitize_text_field() |
!is_string($value) && is_array($value) && isset($value) && !$attachment_id | 114–116 | absint(), sanitize_url(), sanitize_url(), absint(), absint(), absint(), sanitize_text_field(), absint() |
!is_string($value) && is_array($value) && isset($value) && $attachment_id && is_string($mime_type) && $mime_type | 122–124 | absint(), get_post_mime_type(), sanitize_url(), sanitize_url(), absint(), absint(), absint(), sanitize_text_field(), absint() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 154 | 8–124 | 24 | |
| 8.5 | 154 | 8–124 | 24 | |
| 8.4 | 154 | 8–124 | 24 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 157 | 8–127 | 24 | |
| 8.2 | 157 | 8–127 | 24 | |
| 8.1 | 157 | 8–127 | 24 | 10 fewer instructions than PHP 7.4 |
| 7.4 | 167 | 8–137 | 24 |
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_text_field()Sanitizes a string from user input or from the database.
- absint()Converts a value to non-negative integer.
- get_post_mime_type()Retrieves the mime type of an attachment based on the ID.
- str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
- sanitize_url()Sanitizes a URL for database or redirect usage.
- __()Retrieves the translation of $text.
- WP_Error::__construct()Initializes the error.
Used by · 1
- WP_Customize_Header_Image_Setting::sanitize()Sanitizes a header value.
Source code
private function sanitize_choice( $value ) { // Custom_Image_Header::set_header_image() accepts an object in place of an array. if ( is_object( $value ) ) { $value = (array) $value; } if ( is_string( $value ) ) { return sanitize_text_field( $value ); } if ( ! is_array( $value ) ) { return null; } /* * The sanitized value is assembled member by member rather than filtered down from the * supplied one, so that nothing but the members below can end up in it. */ $sanitized = array(); if ( isset( $value['attachment_id'] ) ) { if ( ! is_scalar( $value['attachment_id'] ) ) { return null; } $attachment_id = absint( $value['attachment_id'] ); /* * A supplied attachment must be an existing image, since its ID is written to postmeta and its * data displayed. Note that an ID of zero must be skipped rather than looked up, as * get_post_mime_type() falls back to the global post when passed an empty value. */ if ( $attachment_id > 0 ) { $mime_type = get_post_mime_type( $attachment_id ); if ( ! is_string( $mime_type ) || ! str_starts_with( $mime_type, 'image/' ) ) { return null; } } $sanitized['attachment_id'] = $attachment_id; } if ( isset( $value['url'] ) ) { if ( ! is_string( $value['url'] ) ) { return null; } $sanitized['url'] = sanitize_url( $value['url'] ); if ( '' === $sanitized['url'] ) { return new WP_Error( 'invalid_url', __( 'Invalid URL.' ) ); } } if ( isset( $value['thumbnail_url'] ) ) { if ( ! is_string( $value['thumbnail_url'] ) ) { return null; } $sanitized['thumbnail_url'] = sanitize_url( $value['thumbnail_url'] ); if ( '' === $sanitized['thumbnail_url'] ) { return new WP_Error( 'invalid_url', __( 'Invalid URL.' ) ); } } if ( isset( $value['timestamp'] ) ) { if ( ! is_scalar( $value['timestamp'] ) ) { return null; } $sanitized['timestamp'] = absint( $value['timestamp'] ); } if ( isset( $value['width'] ) ) { if ( ! is_scalar( $value['width'] ) ) { return null; } $sanitized['width'] = absint( $value['width'] ); } if ( isset( $value['height'] ) ) { if ( ! is_scalar( $value['height'] ) ) { return null; } $sanitized['height'] = absint( $value['height'] );Changelog
Introduced in 7.1.1. Unchanged from 6.7.10 through 7.1.3.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.9.10 tag, from
src/wp-includes/customize/class-wp-customize-header-image-setting.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.