wp_get_image_alttext( string $file ): string
- Since
- 7.0.0
- Source
wp-admin/includes/image.php:1088
Compatibility
- WordPress
- since 7.0.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in 2 of the 5 tracked releases, added in 7.0.0, and compiles on PHP 7.4 through 8.6-dev.
Parameters
$filestring- File path to the image.
Return value
string- Embedded alternative text.
Performance profile
How much work a call to wp_get_image_alttext() 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
- 9–82
- Plugin surface
- None
- Called by
- 1
Reads or writes the filesystem via file_get_contents().
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 85.
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
- filesystemfilesystem access
file_get_contents()called directly - sqldatabase query
->query()called directly - hookthird-party callbacks
apply_filters()one call below wp_get_image_alttext() - optionnetwork option
get_site_option()one call below wp_get_image_alttext()
Further down the call graph this can also reach cache, serialize, query 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 wp_get_image_alttext() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 9–15 | file_get_contents() |
$img_contents !== false && $xmp_start !== false && $xmp_end !== false | 27 | file_get_contents(), ->loadXML() |
$img_contents !== false && $xmp_start !== false && $xmp_end !== false | 48 | file_get_contents(), ->loadXML(), ->registerNamespace(), ->registerNamespace(), ->registerNamespace(), ->query() |
$img_contents !== false && $xmp_start !== false && $xmp_end !== false && !->count() | 51 | file_get_contents(), ->loadXML(), ->registerNamespace(), ->registerNamespace(), ->registerNamespace(), ->query(), ->count() |
$img_contents !== false && $xmp_start !== false && $xmp_end !== false && ->count() | 67 | file_get_contents(), ->loadXML(), ->registerNamespace(), ->registerNamespace(), ->registerNamespace(), ->query(), ->count(), ->item(), get_locale(), ->evaluate() |
$img_contents !== false && $xmp_start !== false && $xmp_end !== false && ->count() | 77 | file_get_contents(), ->loadXML(), ->registerNamespace(), ->registerNamespace(), ->registerNamespace(), ->query(), ->count(), ->item(), get_locale(), ->evaluate(), ->evaluate() |
$img_contents !== false && $xmp_start !== false && $xmp_end !== false && ->count() | 82 | file_get_contents(), ->loadXML(), ->registerNamespace(), ->registerNamespace(), ->registerNamespace(), ->query(), ->count(), ->item(), get_locale(), ->evaluate(), ->evaluate(), ->evaluate() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 85 | 9–82 | 8 | |
| 8.5 | 85 | 9–82 | 8 | |
| 8.4 | 85 | 9–82 | 8 | 13 fewer instructions than PHP 8.3 |
| 8.3 | 98 | 9–95 | 8 | |
| 8.2 | 98 | 9–95 | 8 | |
| 8.1 | 98 | 9–95 | 8 | |
| 7.4 | 98 | 9–95 | 8 |
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 · 3
- get_locale()Retrieves the current locale.
- DOMDocument::__construct()
- DOMXPath::__construct()
Used by · 1
- wp_read_image_metadata()Gets extended image metadata, exif or iptc as available.
Source code
function wp_get_image_alttext( $file ) { $alt_text = ''; $img_contents = file_get_contents( $file ); if ( false === $img_contents ) { return $alt_text; } // Find the start and end positions of the XMP metadata. $xmp_start = strpos( $img_contents, '<x:xmpmeta' ); $xmp_end = strpos( $img_contents, '</x:xmpmeta>' ); if ( false === $xmp_start || false === $xmp_end ) { // No XMP metadata found. return $alt_text; } // Extract the XMP metadata from the JPEG contents $xmp_data = substr( $img_contents, $xmp_start, $xmp_end - $xmp_start + 12 ); // Parse the XMP metadata using DOMDocument. $doc = new DOMDocument(); if ( false === $doc->loadXML( $xmp_data ) ) { // Invalid XML in metadata. return $alt_text; } // Instantiate an XPath object, used to extract portions of the XMP. $xpath = new DOMXPath( $doc ); // Register the relevant XML namespaces. $xpath->registerNamespace( 'x', 'adobe:ns:meta/' ); $xpath->registerNamespace( 'rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#' ); $xpath->registerNamespace( 'Iptc4xmpCore', 'http://iptc.org/std/Iptc4xmpCore/1.0/xmlns/' ); $node_list = $xpath->query( '/x:xmpmeta/rdf:RDF/rdf:Description/Iptc4xmpCore:AltTextAccessibility' ); if ( $node_list && $node_list->count() ) { $node = $node_list->item( 0 ); // Get the site's locale. $locale = get_locale(); // Get the alt text accessibility alternative most appropriate for the site language. // There are 3 possibilities: // // 1. there is an rdf:li with an exact match on the site locale. // 2. there is an rdf:li with a partial match on the site locale (e.g., site locale is en_US and rdf:li has @xml:lang="en"). // 3. there is an rdf:li with an "x-default" lang. // // Evaluate in that order, stopping when we have a match. $alt_text = $xpath->evaluate( "string( rdf:Alt/rdf:li[ @xml:lang = '{$locale}' ] )", $node ); if ( ! $alt_text ) { $alt_text = $xpath->evaluate( 'string( rdf:Alt/rdf:li[ @xml:lang = "' . substr( $locale, 0, 2 ) . '" ] )', $node ); if ( ! $alt_text ) { $alt_text = $xpath->evaluate( 'string( rdf:Alt/rdf:li[ @xml:lang = "x-default" ] )', $node ); } } } return $alt_text;}Changelog
Introduced in 7.0.0. Unchanged from 7.0.4 through 7.1.0.
Signature, return type and hooks compared across 2 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-admin/includes/image.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.