* @param int|array|object $attachment Attachment ID, data array, or data object.
* @param string $output Output type. 'names' to return an array of taxonomy names,
* or 'objects' to return an array of taxonomy objects.
* @return string[]|WP_Taxonomy[] List of taxonomies or taxonomy names. Empty array on failure.
function get_attachment_taxonomies( $attachment, $output = 'names' ) {
if ( is_int( $attachment ) ) {
$attachment = get_post( $attachment );
} elseif ( is_array( $attachment ) ) {
$attachment = (object) $attachment;
if ( ! is_object( $attachment ) ) {
$file = get_attached_file( $attachment->ID );
$filename = wp_basename( $file );
$objects = array( 'attachment' );
if ( false !== strpos( $filename, '.' ) ) {
$objects[] = 'attachment:' . substr( $filename, strrpos( $filename, '.' ) + 1 );
if ( ! empty( $attachment->post_mime_type ) ) {
$objects[] = 'attachment:' . $attachment->post_mime_type;
if ( false !== strpos( $attachment->post_mime_type, '/' ) ) {
foreach ( explode( '/', $attachment->post_mime_type ) as $token ) {
if ( ! empty( $token ) ) {
$objects[] = "attachment:$token";
foreach ( $objects as $object ) {
$taxes = get_object_taxonomies( $object, $output );
$taxonomies = array_merge( $taxonomies, $taxes );
if ( 'names' === $output ) {
$taxonomies = array_unique( $taxonomies );
* Retrieves all of the taxonomies that are registered for attachments.
* Handles mime-type-specific taxonomies such as attachment:image and attachment:video.
* @param string $output Optional. The type of taxonomy output to return. Accepts 'names' or 'objects'.
* @return string[]|WP_Taxonomy[] Array of names or objects of registered taxonomies for attachments.
function get_taxonomies_for_attachments( $output = 'names' ) {
foreach ( get_taxonomies( array(), 'objects' ) as $taxonomy ) {
foreach ( $taxonomy->object_type as $object_type ) {
if ( 'attachment' === $object_type || 0 === strpos( $object_type, 'attachment:' ) ) {
if ( 'names' === $output ) {
$taxonomies[] = $taxonomy->name;
$taxonomies[ $taxonomy->name ] = $taxonomy;
* Determines whether the value is an acceptable type for GD image functions.
* In PHP 8.0, the GD extension uses GdImage objects for its data structures.
* This function checks if the passed value is either a resource of type `gd`
* or a GdImage object instance. Any other type will return false.
* @param resource|GdImage|false $image A value to check the type for.
* @return bool True if $image is either a GD image resource or GdImage instance,
function is_gd_image( $image ) {
if ( is_resource( $image ) && 'gd' === get_resource_type( $image )
|| is_object( $image ) && $image instanceof GdImage
* Create new GD image resource with transparency support
* @todo Deprecate if possible.
* @param int $width Image width in pixels.
* @param int $height Image height in pixels.
* @return resource|GdImage|false The GD image resource or GdImage instance on success.
function wp_imagecreatetruecolor( $width, $height ) {
$img = imagecreatetruecolor( $width, $height );
&& function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' )
imagealphablending( $img, false );
imagesavealpha( $img, true );
* Based on a supplied width/height example, return the biggest possible dimensions based on the max width/height.
* @see wp_constrain_dimensions()
* @param int $example_width The width of an example embed.
* @param int $example_height The height of an example embed.
* @param int $max_width The maximum allowed width.
* @param int $max_height The maximum allowed height.
* An array of maximum width and height values.
* @type int $0 The maximum width in pixels.
* @type int $1 The maximum height in pixels.
function wp_expand_dimensions( $example_width, $example_height, $max_width, $max_height ) {
$example_width = (int) $example_width;
$example_height = (int) $example_height;
$max_width = (int) $max_width;
$max_height = (int) $max_height;
return wp_constrain_dimensions( $example_width * 1000000, $example_height * 1000000, $max_width, $max_height );
* Determines the maximum upload size allowed in php.ini.
* @return int Allowed upload size.
function wp_max_upload_size() {
$u_bytes = wp_convert_hr_to_bytes( ini_get( 'upload_max_filesize' ) );
$p_bytes = wp_convert_hr_to_bytes( ini_get( 'post_max_size' ) );
* Filters the maximum upload size allowed in php.ini.
* @param int $size Max upload size limit in bytes.
* @param int $u_bytes Maximum upload filesize in bytes.
* @param int $p_bytes Maximum size of POST data in bytes.
return apply_filters( 'upload_size_limit', min( $u_bytes, $p_bytes ), $u_bytes, $p_bytes );
* Returns a WP_Image_Editor instance and loads file into it.
* @param string $path Path to the file to load.
* @param array $args Optional. Additional arguments for retrieving the image editor.
* @return WP_Image_Editor|WP_Error The WP_Image_Editor object on success,
* a WP_Error object otherwise.
function wp_get_image_editor( $path, $args = array() ) {
if ( ! isset( $args['mime_type'] ) ) {
$file_info = wp_check_filetype( $args['path'] );
// If $file_info['type'] is false, then we let the editor attempt to
// figure out the file type, rather than forcing a failure based on extension.
if ( isset( $file_info ) && $file_info['type'] ) {
$args['mime_type'] = $file_info['type'];
$implementation = _wp_image_editor_choose( $args );
$editor = new $implementation( $path );
$loaded = $editor->load();
if ( is_wp_error( $loaded ) ) {
return new WP_Error( 'image_no_editor', __( 'No editor could be selected.' ) );
* Tests whether there is an editor that supports a given mime type or methods.
* @param string|array $args Optional. Array of arguments to retrieve the image editor supports.
* @return bool True if an eligible editor is found; false otherwise.
function wp_image_editor_supports( $args = array() ) {
return (bool) _wp_image_editor_choose( $args );
* Tests which editors are capable of supporting the request.
* @param array $args Optional. Array of arguments for choosing a capable editor. Default empty array.
* @return string|false Class name for the first editor that claims to support the request.
* False if no editor claims to support the request.
function _wp_image_editor_choose( $args = array() ) {
require_once ABSPATH . WPINC . '/class-wp-image-editor.php';
require_once ABSPATH . WPINC . '/class-wp-image-editor-gd.php';
require_once ABSPATH . WPINC . '/class-wp-image-editor-imagick.php';
* Filters the list of image editing library classes.
* @param string[] $image_editors Array of available image editor class names. Defaults are
* 'WP_Image_Editor_Imagick', 'WP_Image_Editor_GD'.
$implementations = apply_filters( 'wp_image_editors', array( 'WP_Image_Editor_Imagick', 'WP_Image_Editor_GD' ) );
foreach ( $implementations as $implementation ) {
if ( ! call_user_func( array( $implementation, 'test' ), $args ) ) {
if ( isset( $args['mime_type'] ) &&
array( $implementation, 'supports_mime_type' ),
if ( isset( $args['methods'] ) &&
array_diff( $args['methods'], get_class_methods( $implementation ) ) ) {
* Prints default Plupload arguments.
function wp_plupload_default_settings() {
$wp_scripts = wp_scripts();
$data = $wp_scripts->get_data( 'wp-plupload', 'data' );
if ( $data && false !== strpos( $data, '_wpPluploadSettings' ) ) {
$max_upload_size = wp_max_upload_size();
$allowed_extensions = array_keys( get_allowed_mime_types() );
foreach ( $allowed_extensions as $extension ) {
$extensions = array_merge( $extensions, explode( '|', $extension ) );
* Since 4.9 the `runtimes` setting is hardcoded in our version of Plupload to `html5,html4`,
* and the `flash_swf_url` and `silverlight_xap_url` are not used.
'file_data_name' => 'async-upload', // Key passed to $_FILE.
'url' => admin_url( 'async-upload.php', 'relative' ),
'max_file_size' => $max_upload_size . 'b',
'mime_types' => array( array( 'extensions' => implode( ',', $extensions ) ) ),
* Currently only iOS Safari supports multiple files uploading,
* but iOS 7.x has a bug that prevents uploading of videos when enabled.
if ( wp_is_mobile() && strpos( $_SERVER['HTTP_USER_AGENT'], 'OS 7_' ) !== false &&
strpos( $_SERVER['HTTP_USER_AGENT'], 'like Mac OS X' ) !== false ) {
$defaults['multi_selection'] = false;
* Filters the Plupload default settings.
* @param array $defaults Default Plupload settings array.
$defaults = apply_filters( 'plupload_default_settings', $defaults );
'action' => 'upload-attachment',
* Filters the Plupload default parameters.
* @param array $params Default Plupload parameters array.
$params = apply_filters( 'plupload_default_params', $params );
$params['_wpnonce'] = wp_create_nonce( 'media-form' );
$defaults['multipart_params'] = $params;
'mobile' => wp_is_mobile(),
'supported' => _device_can_upload(),
'limitExceeded' => is_multisite() && ! is_upload_space_available(),
$script = 'var _wpPluploadSettings = ' . wp_json_encode( $settings ) . ';';
$script = "$data\n$script";
$wp_scripts->add_data( 'wp-plupload', 'data', $script );
* Prepares an attachment post object for JS, where it is expected
* to be JSON-encoded and fit into an Attachment model.
* @param int|WP_Post $attachment Attachment ID or object.
* Array of attachment details, or void if the parameter does not correspond to an attachment.
* @type string $alt Alt text of the attachment.
* @type string $author ID of the attachment author, as a string.
* @type string $authorName Name of the attachment author.
* @type string $caption Caption for the attachment.
* @type array $compat Containing item and meta.
* @type string $context Context, whether it's used as the site icon for example.
* @type int $date Uploaded date, timestamp in milliseconds.
* @type string $dateFormatted Formatted date (e.g. June 29, 2018).
* @type string $description Description of the attachment.
* @type string $editLink URL to the edit page for the attachment.
* @type string $filename File name of the attachment.
* @type string $filesizeHumanReadable Filesize of the attachment in human readable format (e.g. 1 MB).
* @type int $filesizeInBytes Filesize of the attachment in bytes.
* @type int $height If the attachment is an image, represents the height of the image in pixels.
* @type string $icon Icon URL of the attachment (e.g. /wp-includes/images/media/archive.png).
* @type int $id ID of the attachment.
* @type string $link URL to the attachment.
* @type int $menuOrder Menu order of the attachment post.
* @type array $meta Meta data for the attachment.
* @type string $mime Mime type of the attachment (e.g. image/jpeg or application/zip).
* @type int $modified Last modified, timestamp in milliseconds.
* @type string $name Name, same as title of the attachment.
* @type array $nonces Nonces for update, delete and edit.
* @type string $orientation If the attachment is an image, represents the image orientation
* (landscape or portrait).
* @type array $sizes If the attachment is an image, contains an array of arrays
* for the images sizes: thumbnail, medium, large, and full.
* @type string $status Post status of the attachment (usually 'inherit').
* @type string $subtype Mime subtype of the attachment (usually the last part, e.g. jpeg or zip).
* @type string $title Title of the attachment (usually slugified file name without the extension).
* @type string $type Type of the attachment (usually first part of the mime type, e.g. image).
* @type int $uploadedTo Parent post to which the attachment was uploaded.
* @type string $uploadedToLink URL to the edit page of the parent post of the attachment.
* @type string $uploadedToTitle Post title of the parent of the attachment.
* @type string $url Direct URL to the attachment file (from wp-content).
* @type int $width If the attachment is an image, represents the width of the image in pixels.
function wp_prepare_attachment_for_js( $attachment ) {
$attachment = get_post( $attachment );
if ( 'attachment' !== $attachment->post_type ) {
$meta = wp_get_attachment_metadata( $attachment->ID );
if ( false !== strpos( $attachment->post_mime_type, '/' ) ) {
list( $type, $subtype ) = explode( '/', $attachment->post_mime_type );
list( $type, $subtype ) = array( $attachment->post_mime_type, '' );
$attachment_url = wp_get_attachment_url( $attachment->ID );
$base_url = str_replace( wp_basename( $attachment_url ), '', $attachment_url );
'title' => $attachment->post_title,
'filename' => wp_basename( get_attached_file( $attachment->ID ) ),
'url' => $attachment_url,
'link' => get_attachment_link( $attachment->ID ),
'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ),
'author' => $attachment->post_author,
'description' => $attachment->post_content,
'caption' => $attachment->post_excerpt,
'name' => $attachment->post_name,
'status' => $attachment->post_status,
'uploadedTo' => $attachment->post_parent,
'date' => strtotime( $attachment->post_date_gmt ) * 1000,
'modified' => strtotime( $attachment->post_modified_gmt ) * 1000,
'menuOrder' => $attachment->menu_order,
'mime' => $attachment->post_mime_type,
'icon' => wp_mime_type_icon( $attachment->ID ),
'dateFormatted' => mysql2date( __( 'F j, Y' ), $attachment->post_date ),
$author = new WP_User( $attachment->post_author );
if ( $author->exists() ) {
$author_name = $author->display_name ? $author->display_name : $author->nickname;
$response['authorName'] = html_entity_decode( $author_name, ENT_QUOTES, get_bloginfo( 'charset' ) );
$response['authorLink'] = get_edit_user_link( $author->ID );
$response['authorName'] = __( '(no author)' );
if ( $attachment->post_parent ) {
$post_parent = get_post( $attachment->post_parent );
$response['uploadedToTitle'] = $post_parent->post_title ? $post_parent->post_title : __( '(no title)' );
$response['uploadedToLink'] = get_edit_post_link( $attachment->post_parent, 'raw' );
$attached_file = get_attached_file( $attachment->ID );
if ( isset( $meta['filesize'] ) ) {
$bytes = $meta['filesize'];
} elseif ( file_exists( $attached_file ) ) {
$bytes = filesize( $attached_file );