wp_read_audio_metadata( string $file ): array|false
- Since
- 3.6.0
- Source
wp-admin/includes/media.php:3688
Compatibility
- WordPress
- since 3.6.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
$filestring- Path to file.
Return value
array|false- Returns array of metadata, if found.
Performance profile
How much work a call to wp_read_audio_metadata() 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
- 6–93
- Plugin surface
- 1 hook
- Called by
- 2
Reads or writes the filesystem via file_exists().
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 95.
Third-party callbacks on 'wp_read_audio_metadata' run inside this call, and their cost is not bounded by anything here.
2 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()called directly - filesystemfilesystem access
file_exists()called directly
What one call costs · 9 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_read_audio_metadata() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!file_exists() | 6 | file_exists() |
file_exists() && defined('GETID3_TEMP_DIR') && empty($data) && !empty($metadata) | 49–72 | file_exists(), ->analyze(), wp_add_id3_tag_data(), apply_filters() |
file_exists() && defined('GETID3_TEMP_DIR') && empty($data) && empty($metadata) | 55–80 | file_exists(), ->analyze(), wp_get_media_creation_timestamp(), wp_add_id3_tag_data(), apply_filters() |
file_exists() && !defined('GETID3_TEMP_DIR') && empty($data) && !empty($metadata) | 55–78 | file_exists(), get_temp_dir(), define(), ->analyze(), wp_add_id3_tag_data(), apply_filters() |
file_exists() && defined('GETID3_TEMP_DIR') && !empty($data) && !empty($metadata) | 56–79 | file_exists(), ->analyze(), round(), wp_add_id3_tag_data(), apply_filters() |
file_exists() && !defined('GETID3_TEMP_DIR') && empty($data) && empty($metadata) | 61–86 | file_exists(), get_temp_dir(), define(), ->analyze(), wp_get_media_creation_timestamp(), wp_add_id3_tag_data(), apply_filters() |
file_exists() && defined('GETID3_TEMP_DIR') && !empty($data) && empty($metadata) | 62–87 | file_exists(), ->analyze(), round(), wp_get_media_creation_timestamp(), wp_add_id3_tag_data(), apply_filters() |
file_exists() && !defined('GETID3_TEMP_DIR') && !empty($data) && !empty($metadata) | 62–85 | file_exists(), get_temp_dir(), define(), ->analyze(), round(), wp_add_id3_tag_data(), apply_filters() |
file_exists() && !defined('GETID3_TEMP_DIR') && !empty($data) && empty($metadata) | 68–93 | file_exists(), get_temp_dir(), define(), ->analyze(), round(), wp_get_media_creation_timestamp(), wp_add_id3_tag_data(), apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 95 | 6–93 | 12 | |
| 8.5 | 95 | 6–93 | 12 | |
| 8.4 | 95 | 6–93 | 12 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 98 | 6–96 | 12 | |
| 8.2 | 98 | 6–96 | 12 | |
| 8.1 | 98 | 6–96 | 12 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 99 | 6–97 | 12 |
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.
Hooks and filters fired · 1
One hook fires while wp_read_audio_metadata() runs, in this order:
- apply_filters( wp_read_audio_metadata )filterline 3760 (+72 into the body)
Filters the array of metadata retrieved from an audio file.
Uses · 5
- get_temp_dir()Determines a writable directory for temporary files.
- wp_get_media_creation_timestamp()Parses creation date from media metadata.
- wp_add_id3_tag_data()Parses ID3v2, ID3v1, and getID3 comments to extract usable data.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- getID3::__construct()
Used by · 2
- media_handle_upload()Saves a file submitted from a POST request and create an attachment post for it.
- wp_generate_attachment_metadata()Generates attachment meta data and create image sub-sizes for images.
Source code
function wp_read_audio_metadata( $file ) { if ( ! file_exists( $file ) ) { return false; } $metadata = array(); if ( ! defined( 'GETID3_TEMP_DIR' ) ) { define( 'GETID3_TEMP_DIR', get_temp_dir() ); } if ( ! class_exists( 'getID3', false ) ) { require ABSPATH . WPINC . '/ID3/getid3.php'; } $id3 = new getID3(); // Required to get the `created_timestamp` value. $id3->options_audiovideo_quicktime_ReturnAtomData = true; // phpcs:ignore WordPress.NamingConventions.ValidVariableName $data = $id3->analyze( $file ); if ( ! empty( $data['audio'] ) ) { unset( $data['audio']['streams'] ); $metadata = $data['audio']; } if ( ! empty( $data['fileformat'] ) ) { $metadata['fileformat'] = $data['fileformat']; } if ( ! empty( $data['filesize'] ) ) { $metadata['filesize'] = (int) $data['filesize']; } if ( ! empty( $data['mime_type'] ) ) { $metadata['mime_type'] = $data['mime_type']; } if ( ! empty( $data['playtime_seconds'] ) ) { $metadata['length'] = (int) round( $data['playtime_seconds'] ); } if ( ! empty( $data['playtime_string'] ) ) { $metadata['length_formatted'] = $data['playtime_string']; } if ( empty( $metadata['created_timestamp'] ) ) { $created_timestamp = wp_get_media_creation_timestamp( $data ); if ( false !== $created_timestamp ) { $metadata['created_timestamp'] = $created_timestamp; } } wp_add_id3_tag_data( $metadata, $data ); $file_format = isset( $metadata['fileformat'] ) ? $metadata['fileformat'] : null; /** * Filters the array of metadata retrieved from an audio file. * * In core, usually this selection is what is stored. * More complete data can be parsed from the `$data` parameter. * * @since 6.1.0 * * @param array $metadata Filtered audio metadata. * @param string $file Path to audio file. * @param string|null $file_format File format of audio, as analyzed by getID3. * Null if unknown. * @param array $data Raw metadata from getID3. */ return apply_filters( 'wp_read_audio_metadata', $metadata, $file, $file_format, $data );}Changelog
Introduced in 3.6.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 6.7.7 tag, from
src/wp-admin/includes/media.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.