Builds the Caption shortcode output.
Description
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’, ‘caption_id’, ‘align’, ‘width’, ‘caption’, and ‘class’.
Parameters
$attr
arrayrequired- Attributes of the caption shortcode.
id
stringID of the image and caption container element, i.e.<figure>
or<div>
.caption_id
stringID of the caption element, i.e.<figcaption>
or<p>
.align
stringClass name that aligns the caption. Default'alignnone'
. Accepts'alignleft'
,'aligncenter'
, alignright’,'alignnone'
.width
intThe width of the caption, in pixels.caption
stringThe caption text.class
stringAdditional class name(s) added to the caption container.
$content
stringoptional- Shortcode content.
Default:
''
Source
if ( $value ) {
if ( ! in_array( $value, array( 'lazy', 'eager' ), true ) ) {
$value = 'lazy';
}
return str_replace( '<iframe', '<iframe loading="' . esc_attr( $value ) . '"', $iframe );
}
return $iframe;
}
/**
* Adds a 'wp-post-image' class to post thumbnails. Internal use only.
*
* 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 thumbnails.
*
* @ignore
* @since 2.9.0
*
* @param string[] $attr Array of thumbnail attributes including src, class, alt, title, keyed by attribute name.
* @return string[] Modified array of attributes including the new 'wp-post-image' class.
*/
function _wp_post_thumbnail_class_filter( $attr ) {
$attr['class'] .= ' wp-post-image';
return $attr;
}
/**
* Adds '_wp_post_thumbnail_class_filter' callback to the 'wp_get_attachment_image_attributes'
* filter hook. Internal use only.
*
* @ignore
* @since 2.9.0
*
* @param string[] $attr Array of thumbnail attributes including src, class, alt, title, keyed by attribute name.
*/
function _wp_post_thumbnail_class_filter_add( $attr ) {
add_filter( 'wp_get_attachment_image_attributes', '_wp_post_thumbnail_class_filter' );
}
/**
* Removes the '_wp_post_thumbnail_class_filter' callback from the 'wp_get_attachment_image_attributes'
* filter hook. Internal use only.
*
* @ignore
* @since 2.9.0
*
* @param string[] $attr Array of thumbnail attributes including src, class, alt, title, keyed by attribute name.
*/
function _wp_post_thumbnail_class_filter_remove( $attr ) {
remove_filter( 'wp_get_attachment_image_attributes', '_wp_post_thumbnail_class_filter' );
}
/**
* Overrides the context used in wp_get_attachment_image(). Internal use only.
*
* 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 thumbnails.
*
* @ignore
* @since 6.3.0
* @access private
*
* @param string $context The context for rendering an attachment image.
* @return string Modified context set to 'the_post_thumbnail'.
*/
function _wp_post_thumbnail_context_filter( $context ) {
return 'the_post_thumbnail';
}
/**
* Adds the '_wp_post_thumbnail_context_filter' callback to the 'wp_get_attachment_image_context'
* filter hook. Internal use only.
*
* @ignore
* @since 6.3.0
* @access private
*/
function _wp_post_thumbnail_context_filter_add() {
add_filter( 'wp_get_attachment_image_context', '_wp_post_thumbnail_context_filter' );
}
/**
* Removes the '_wp_post_thumbnail_context_filter' callback from the 'wp_get_attachment_image_context'
* filter hook. Internal use only.
*
* @ignore
* @since 6.3.0
* @access private
*/
function _wp_post_thumbnail_context_filter_remove() {
remove_filter( 'wp_get_attachment_image_context', '_wp_post_thumbnail_context_filter' );
}
add_shortcode( 'wp_caption', 'img_caption_shortcode' );
add_shortcode( 'caption', 'img_caption_shortcode' );
/**
* Builds the Caption shortcode output.
*
* 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', 'caption_id', 'align',
* 'width', 'caption', and 'class'.
*
* @since 2.6.0
* @since 3.9.0 The `class` attribute was added.
* @since 5.1.0 The `caption_id` attribute was added.
* @since 5.9.0 The `$content` parameter default value changed from `null` to `''`.
*
* @param array $attr {
* Attributes of the caption shortcode.
*
* @type string $id ID of the image and caption container element, i.e. `<figure>` or `<div>`.
* @type string $caption_id ID of the caption element, i.e. `<figcaption>` or `<p>`.
* @type string $align Class name that aligns the caption. Default 'alignnone'. Accepts 'alignleft',
* 'aligncenter', alignright', 'alignnone'.
* @type int $width The width of the caption, in pixels.
* @type string $caption The caption text.
* @type string $class Additional class name(s) added to the caption container.
* }
* @param string $content Optional. Shortcode content. Default empty string.
* @return string HTML content to display the caption.
*/
function img_caption_shortcode( $attr, $content = '' ) {
// New-style shortcode with the caption inside the shortcode with the link and image tags.
User Contributed Notes
You must log in before being able to contribute a note or feedback.