wp-includes/media.php

WordPress API for media display.

package
WordPress

Functions

function _wp_post_thumbnail_class_filter( array $attr ) : array
Adds a 'wp-post-image' class to post thumbnail thumbnails Uses the begin_fetch_post_thumbnail_html and end_fetch_post_thumbnail_html action hooks to dynamically add/remove itself so as to only filter post thumbnail thumbnails
Parameters
Name Type Description
$attr array Attributes including src, class, alt, title
Returns
Type Description
array
Details
since
2.9.0
function _wp_post_thumbnail_class_filter_add( $attr ) :
Adds _wp_post_thumbnail_class_filter to the wp_get_attachment_image_attributes filter
Parameters
Name Type Description
$attr
Details
since
2.9.0
function _wp_post_thumbnail_class_filter_remove( $attr ) :
Removes _wp_post_thumbnail_class_filter from the wp_get_attachment_image_attributes filter
Parameters
Name Type Description
$attr
Details
since
2.9.0
function add_image_size( $name, $width = 0, $height = 0, $crop = false ) :
Registers a new image size
Parameters
Name Type Description
$name
$width
$height
$crop
function adjacent_image_link( bool $prev = true, $size = thumbnail, $text = false ) :
Display next or previous image link that has the same post parent.

Retrieves the current attachment object from the $post global.

Parameters
Name Type Description
$prev bool Optional. Default is true to display previous link, true for next.
$size
$text
Details
since
2.5.0
function gallery_shortcode( array $attr ) : string
The Gallery shortcode.

This implements the functionality of the Gallery Shortcode for displaying WordPress images on a post.

Parameters
Name Type Description
$attr array Attributes attributed to the shortcode.
Returns
Type Description
string
Details
since
2.5.0
function gd_edit_image_support( string $mime_type ) : bool
Check if the installed version of GD supports particular image type
Parameters
Name Type Description
$mime_type string
Returns
Type Description
bool
Details
since
2.9.0
function get_attachment_taxonomies( int|array|object $attachment ) : array
Retrieve taxonomies attached to the attachment.
Parameters
Name Type Description
$attachment int|array|object Attachment ID, Attachment data array, or Attachment data object.
Returns
Type Description
array
Details
since
2.5.0
function get_image_tag( int $id, string $alt, string $title, string $align, string $size = medium ) : string
An tag for an image attachment, scaling it down if requested.

The filter 'get_image_tag_class' allows for changing the class name for the image without having to use regular expressions on the HTML content. The parameters are: what WordPress will use for the class, the Attachment ID, image align value, and the size the image should be.

The second filter 'get_image_tag' has the HTML content, which can then be further manipulated by a plugin to change all attribute values and even HTML content.

Parameters
Name Type Description
$id int Attachment ID.
$alt string Image Description for the alt attribute.
$title string Image Description for the title attribute.
$align string Part of the class name for aligning the image.
$size string

Optional. Default is 'medium'.

Returns
Type Description
string
Details
since
2.5.0
uses
\apply_filters()
uses
\apply_filters()
function get_intermediate_image_sizes( ) : array
Get the available image sizes
Returns
Type Description
array
Details
since
3.0.0
function image_constrain_size_for_editor( int $width, int $height, string|array $size = medium ) : array
Scale down the default size of an image.

This is so that the image is a better fit for the editor and theme.

The $size parameter accepts either an array or a string. The supported string values are 'thumb' or 'thumbnail' for the given thumbnail size or defaults at 128 width and 96 height in pixels. Also supported for the string value is 'medium' and 'full'. The 'full' isn't actually supported, but any value other than the supported will result in the content_width size or 500 if that is not set.

Finally, there is a filter named, 'editor_max_image_size' that will be called on the calculated array for width and height, respectively. The second parameter will be the value that was in the $size parameter. The returned type for the hook is an array with the width as the first element and the height as the second element.

Parameters
Name Type Description
$width int Width of the image
$height int Height of the image
$size string|array Size of what the result image should be.
Returns
Type Description
array
Details
since
2.5.0
uses
\wp_constrain_dimensions()
function image_downsize( int $id, array|string $size = medium ) : bool|array
Scale an image to fit a particular size (such as 'thumb' or 'medium').

Array with image url, width, height, and whether is intermediate size, in that order is returned on success is returned. $is_intermediate is true if $url is a resized image, false if it is the original.

The URL might be the original image, or it might be a resized version. This function won't create a new resized copy, it will just return an already resized one if it exists.

A plugin may use the 'image_downsize' filter to hook into and offer image resizing services for images. The hook must return an array with the same elements that are returned in the function. The first element being the URL to the new image that was resized.

Parameters
Name Type Description
$id int Attachment ID for image.
$size array|string

Optional, default is 'medium'. Size of image, either array or string.

Returns
Type Description
boolarray
Details
since
2.5.0
uses
\apply_filters()
function image_get_intermediate_size( int $post_id, array|string $size = thumbnail ) : bool|array
Retrieve the image's intermediate size (resized) path, width, and height.

The $size parameter can be an array with the width and height respectively. If the size matches the 'sizes' metadata array for width and height, then it will be used. If there is no direct match, then the nearest image size larger than the specified size will be used. If nothing is found, then the function will break out and return false.

The metadata 'sizes' is used for compatible sizes that can be used for the parameter $size value.

The url path will be given, when the $size parameter is a string.

If you are passing an array for the $size, you should consider using add_image_size() so that a cropped version is generated. It's much more efficient than having to find the closest-sized image and then having the browser scale down the image.

Parameters
Name Type Description
$post_id int Attachment ID for image.
$size array|string

Optional, default is 'thumbnail'. Size of image, either array or string.

Returns
Type Description
boolarray
Details
see
\add_image_size()
since
2.5.0
function image_hwstring( int|string $width, int|string $height ) : string
Retrieve width and height attributes using given width and height values.

Both attributes are required in the sense that both parameters must have a value, but are optional in that if you set them to false or null, then they will not be added to the returned string.

You can set the value using a string, but it will only take numeric values. If you wish to put 'px' after the numbers, then it will be stripped out of the return.

Parameters
Name Type Description
$width int|string Optional. Width attribute value.
$height int|string Optional. Height attribute value.
Returns
Type Description
string
Details
since
2.5.0
function image_make_intermediate_size( string $file, int $width, int $height, bool $crop = false ) : bool|array
Resize an image to make a thumbnail or intermediate size.

The returned array has the file size, the image width, and image height. The filter 'image_make_intermediate_size' can be used to hook in and change the values of the returned array. The only parameter is the resized file path.

Parameters
Name Type Description
$file string File path.
$width int Image width.
$height int Image height.
$crop bool Optional, default is false. Whether to crop image to specified height and width or resize.
Returns
Type Description
boolarray
Details
since
2.5.0
function image_resize( string $file, int $max_w, int $max_h, bool $crop = false, string $suffix = null, string $dest_path = null, int $jpeg_quality = 90 ) : mixed
Scale down an image to fit a particular size and save a new copy of the image.

The PNG transparency will be preserved using the function, as well as the image type. If the file going in is PNG, then the resized image is going to be PNG. The only supported image types are PNG, GIF, and JPEG.

Some functionality requires API to exist, so some PHP version may lose out support. This is not the fault of WordPress (where functionality is downgraded, not actual defects), but of your PHP version.

Parameters
Name Type Description
$file string Image file path.
$max_w int Maximum width to resize to.
$max_h int Maximum height to resize to.
$crop bool Optional. Whether to crop image or resize.
$suffix string Optional. File Suffix.
$dest_path string Optional. New image file path.
$jpeg_quality int Optional, default is 90. Image quality percentage.
Returns
Type Description
mixed
Details
since
2.5.0
function image_resize_dimensions( int $orig_w, int $orig_h, int $dest_w, int $dest_h, bool $crop = false ) : bool|array
Retrieve calculated resized dimensions for use in imagecopyresampled().

Calculate dimensions and coordinates for a resized image that fits within a specified width and height. If $crop is true, the largest matching central portion of the image will be cropped out and resized to the required size.

Parameters
Name Type Description
$orig_w int Original width.
$orig_h int Original height.
$dest_w int New width.
$dest_h int New height.
$crop bool Optional, default is false. Whether to crop image or resize.
Returns
Type Description
boolarray
Details
since
2.5.0
function img_caption_shortcode( array $attr, string $content = null ) : string
The Caption shortcode.

Allows a plugin to replace the content that would otherwise be returned. The filter is 'img_caption_shortcode' and passes an empty string, the attr parameter and the content parameter values.

The supported attributes for the shortcode are 'id', 'align', 'width', and 'caption'.

Parameters
Name Type Description
$attr array Attributes attributed to the shortcode.
$content string Optional. Shortcode content.
Returns
Type Description
string
Details
since
2.6.0
function next_image_link( string $size = thumbnail, string $text = false ) : string
Display next image link that has the same post parent.
Parameters
Name Type Description
$size string

Optional, default is 'thumbnail'. Size of image, either array or string. 0 or 'none' will default to post_title or $text;

$text string

Optional, default is false. If included, link will reflect $text variable.

Returns
Type Description
string
Details
since
2.5.0
function previous_image_link( string $size = thumbnail, string $text = false ) : string
Display previous image link that has the same post parent.
Parameters
Name Type Description
$size string

Optional, default is 'thumbnail'. Size of image, either array or string. 0 or 'none' will default to post_title or $text;

$text string

Optional, default is false. If included, link will reflect $text variable.

Returns
Type Description
string
Details
since
2.5.0
function set_post_thumbnail_size( $width = 0, $height = 0, $crop = false ) :
Registers an image size for the post thumbnail
Parameters
Name Type Description
$width
$height
$crop
function wp_constrain_dimensions( int $current_width, int $current_height, int $max_width = 0, int $max_height = 0 ) : array
Calculates the new dimentions for a downsampled image.

If either width or height are empty, no constraint is applied on that dimension.

Parameters
Name Type Description
$current_width int Current width of the image.
$current_height int Current height of the image.
$max_width int Optional. Maximum wanted width.
$max_height int Optional. Maximum wanted height.
Returns
Type Description
array
Details
since
2.5.0
function wp_embed_defaults( ) : array
Create default array of embed parameters.
Returns
Type Description
array
Details
since
2.9.0
function wp_embed_handler_googlevideo( array $matches, array $attr, string $url, array $rawattr ) : string
The Google Video embed handler callback. Google Video does not support oEmbed.
Parameters
Name Type Description
$matches array

The regex matches from the provided regex when calling {@link wp_embed_register_handler()}.

$attr array Embed attributes.
$url string The original URL that was matched by the regex.
$rawattr array The original unmodified attributes.
Returns
Type Description
string
Details
see
\WP_Embed::register_handler()
see
\WP_Embed::shortcode()
function wp_embed_register_handler( $id, $regex, $callback, $priority = 10 ) :
Register an embed handler. This function should probably only be used for sites that do not support oEmbed.
Parameters
Name Type Description
$id
$regex
$callback
$priority
Details
see
\WP_Embed::register_handler()
since
2.9.0
function wp_embed_unregister_handler( $id, $priority = 10 ) :
Unregister a previously registered embed handler.
Parameters
Name Type Description
$id
$priority
Details
see
\WP_Embed::unregister_handler()
since
2.9.0
function wp_expand_dimensions( int $example_width, int $example_height, int $max_width, int $max_height ) : array
Based on a supplied width/height example, return the biggest possible dimensions based on the max width/height.
Parameters
Name Type Description
$example_width int The width of an example embed.
$example_height int The height of an example embed.
$max_width int The maximum allowed width.
$max_height int The maximum allowed height.
Returns
Type Description
array
Details
since
2.9.0
uses
\wp_constrain_dimensions()
function wp_get_attachment_image( int $attachment_id, string $size = thumbnail, bool $icon = false, $attr ) : string
Get an HTML img element representing an image attachment

While $size will accept an array, it is better to register a size with add_image_size() so that a cropped version is generated. It's much more efficient than having to find the closest-sized image and then having the browser scale down the image.

Parameters
Name Type Description
$attachment_id int Image attachment ID.
$size string

Optional, default is 'thumbnail'.

$icon bool Optional, default is false. Whether it is an icon.
$attr
Returns
Type Description
string
Details
see
\add_image_size()
since
2.5.0
uses
\apply_filters()
uses
\wp_get_attachment_image_src()
function wp_get_attachment_image_src( int $attachment_id, string $size = thumbnail, bool $icon = false ) : bool|array
Retrieve an image to represent an attachment.

A mime icon for files, thumbnail or intermediate size for images.

Parameters
Name Type Description
$attachment_id int Image attachment ID.
$size string

Optional, default is 'thumbnail'.

$icon bool Optional, default is false. Whether it is an icon.
Returns
Type Description
boolarray
Details
since
2.5.0
function wp_imagecreatetruecolor( int $width, int $height ) : \image
Create new GD image resource with transparency support
Parameters
Name Type Description
$width int Image width
$height int Image height
Returns
Type Description
\image
Details
since
2.9.0
function wp_load_image( string $file ) : resource
Load an image from a string, if PHP supports it.
Parameters
Name Type Description
$file string Filename of the image to load.
Returns
Type Description
resource
Details
since
2.1.0
function wp_maybe_load_embeds( ) :
Determines if default embed handlers should be loaded.

Checks to make sure that the embeds library hasn't already been loaded. If it hasn't, then it will load the embeds library.

Details
since
2.9.0
function wp_oembed_add_provider( string $format, string $provider, boolean $regex = false ) :
Adds a URL format and oEmbed provider URL pair.
Parameters
Name Type Description
$format string The format of URL that this provider can handle. You can use asterisks as wildcards.
$provider string The URL to the oEmbed provider.
$regex boolean

Whether the $format parameter is in a regex format.

Details
see
\WP_oEmbed
since
2.9.0
uses
\_wp_oembed_get_object()
function wp_oembed_get( string $url, array $args ) : string
Attempts to fetch the embed HTML for a provided URL using oEmbed.
Parameters
Name Type Description
$url string The URL that should be embeded.
$args array Addtional arguments and parameters.
Returns
Type Description
string
Details
see
\WP_oEmbed
since
2.9.0
uses
\_wp_oembed_get_object()
uses
\WP_oEmbed::get_html()

\WP_Embed

API for easily embedding rich media such as videos and images into content.

package
WordPress
since
2.9.0
subpackage
Embed

Properties

Propertypublic $handlers= 'array'
Default valuearrayDetails
Type
Propertypublic $linkifunknown= 'true'
Default valuetrueDetails
Type
Propertypublic $post_ID= ''
Details
Type
Propertypublic $usecache= 'true'
Default valuetrueDetails
Type

Methods

methodpublic __construct( ) :
Constructor
methodpublic autoembed( string $content ) : string
Passes any unlinked URLs that are on their own line to {@link WP_Embed::shortcode()} for potential embedding.
Parameters
Name Type Description
$content string The content to be searched.
Returns
Type Description
string
Details
uses
\WP_Embed::autoembed_callback()
methodpublic autoembed_callback( array $match ) : string
Callback function for {@link WP_Embed::autoembed()}.
Parameters
Name Type Description
$match array A regex match array.
Returns
Type Description
string
Details
used_by
\WP_Embed::autoembed()
uses
\WP_Embed::shortcode()
methodpublic cache_oembed( int $post_ID ) :
Triggers a caching of all oEmbed results.
Parameters
Name Type Description
$post_ID int Post ID to do the caching for.
methodpublic delete_oembed_caches( int $post_ID ) :
Delete all oEmbed caches.
Parameters
Name Type Description
$post_ID int Post ID to delete the caches for.
methodpublic maybe_make_link( string $url ) : string
Conditionally makes a hyperlink based on an internal class variable.
Parameters
Name Type Description
$url string URL to potentially be linked.
Returns
Type Description
string
Details
used_by
\WP_Embed::shortcode()
methodpublic maybe_run_ajax_cache( ) :
If a post/page was saved, then output Javascript to make an AJAX request that will call WP_Embed::cache_oembed().
methodpublic register_handler( string $id, string $regex, callback $callback, int $priority = 10 ) :
Register an embed handler. Do not use this function directly, use {@link wp_embed_register_handler()} instead.

This function should probably also only be used for sites that do not support oEmbed.

Parameters
Name Type Description
$id string

An internal ID/name for the handler. Needs to be unique.

$regex string The regex that will be used to see if this handler should be used for a URL.
$callback callback The callback function that will be called if the regex is matched.
$priority int

Optional. Used to specify the order in which the registered handlers will be tested (default: 10). Lower numbers correspond with earlier testing, and handlers with the same priority are tested in the order in which they were added to the action.

methodpublic run_shortcode( string $content ) : string
Process the [embed] shortcode.

Since the [embed] shortcode needs to be run earlier than other shortcodes, this function removes all existing shortcodes, registers the [embed] shortcode, calls {@link do_shortcode()}, and then re-registers the old shortcodes.

Parameters
Name Type Description
$content string Content to parse
Returns
Type Description
string
Details
uses
\$shortcode_tags
uses
\remove_all_shortcodes()
uses
\add_shortcode()
uses
\do_shortcode()
methodpublic shortcode( array $attr, string $url ) : string
The {@link do_shortcode()} callback function.

Attempts to convert a URL into embed HTML. Starts by checking the URL against the regex of the registered embed handlers. If none of the regex matches and it's enabled, then the URL will be given to the {@link WP_oEmbed} class.

Parameters
Name Type Description
$attr array Shortcode attributes.
$url string The URL attempting to be embeded.
Returns
Type Description
string
Details
used_by
\WP_Embed::autoembed_callback()
uses
\wp_oembed_get()
uses
\wp_parse_args()
uses
\wp_embed_defaults()
uses
\WP_Embed::maybe_make_link()
uses
\get_option()
uses
\current_user_can()
uses
\wp_cache_get()
uses
\wp_cache_set()
uses
\get_post_meta()
uses
\update_post_meta()
methodpublic unregister_handler( string $id, int $priority = 10 ) :
Unregister a previously registered embed handler. Do not use this function directly, use {@link wp_embed_unregister_handler()} instead.
Parameters
Name Type Description
$id string The handler ID that should be removed.
$priority int

Optional. The priority of the handler to be removed (default: 10).

Documentation was generated by DocBlox 0.13.0.