wp-admin/includes/image.php:1088Gets the alt text from image meta data.
$filestringstringfunction 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;}Introduced in 7.0.0. Unchanged from 7.0.4 through 7.1.0.
Signature, return type and hooks compared across 2 parsed releases.
src/wp-admin/includes/image.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.