WordPress.org

Ready to get started?Download WordPress

Codex

Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Plugin API/Filter Reference/img caption shortcode

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.

Parameters

$empty
(string) (required) Empty string
Default: None
$attr
(array) (required) Attributes attributed to the shortcode.
Default: The supported attributes for the shortcode are 'id', 'align', 'width', and 'caption'.
$content
(string) (required) Shortcode content.
Default: None

Return value

The filter should return a string containing the full HTML code that should be displayed. If it returns an empty string the standard WordPress functionality will be executed (see wp-includes/media.php on line 629)

Example

add_filter( 'img_caption_shortcode', 'my_img_caption_shortcode', 10, 3 );

function my_img_caption_shortcode( $empty, $attr, $content ){
	$attr = shortcode_atts( array(
		'id'      => '',
		'align'   => 'alignnone',
		'width'   => '',
		'caption' => ''
	), $attr );

	if ( 1 > (int) $attr['width'] || empty( $attr['caption'] ) ) {
		return '';
	}

	if ( $attr['id'] ) {
		$attr['id'] = 'id="' . esc_attr( $attr['id'] ) . '" ';
	}

	return '<div ' . $attr['id']
	. 'class="wp-caption ' . esc_attr( $attr['align'] ) . '" '
	. 'style="max-width: ' . ( 10 + (int) $attr['width'] ) . 'px;">'
	. do_shortcode( $content )
	. '<p class="wp-caption-text">' . $attr['caption'] . '</p>'
	. '</div>';

}

Triggers

As of WordPress 3.6 this hook is applied in the following location:

This page is marked as incomplete. You can help Codex by expanding it.