The img_caption_shortcode_width WordPress PHP Filter allows you to modify the width of an image’s caption.
Usage
add_filter('img_caption_shortcode_width', 'your_custom_function', 10, 3); function your_custom_function($width, $atts, $content) { // your custom code here return $width; }
Parameters
$width
int: Width of the caption in pixels. To remove this inline style, return zero.$atts
array: Attributes of the caption shortcode.$content
string: The image element, possibly wrapped in a hyperlink.
More information
See WordPress Developer Resources: img_caption_shortcode_width
Examples
Increase caption width by 20 pixels
This code will increase the caption width by 20 pixels.
add_filter('img_caption_shortcode_width', 'increase_caption_width', 10, 3); function increase_caption_width($width, $atts, $content) { $width += 20; return $width; }
Set a fixed caption width
This code sets a fixed width of 300 pixels for all captions.
add_filter('img_caption_shortcode_width', 'fixed_caption_width', 10, 3); function fixed_caption_width($width, $atts, $content) { return 300; }
Remove inline caption width
This code removes the inline caption width by returning zero.
add_filter('img_caption_shortcode_width', 'remove_caption_width', 10, 3); function remove_caption_width($width, $atts, $content) { return 0; }
Set caption width based on image size
This code sets the caption width based on the size of the image plus 30 pixels.
add_filter('img_caption_shortcode_width', 'dynamic_caption_width', 10, 3); function dynamic_caption_width($width, $atts, $content) { $image_width = $atts['width']; return $image_width + 30; }
Set caption width to match container width
This code sets the caption width to 100%, so it matches the container width.
add_filter('img_caption_shortcode_width', 'container_caption_width', 10, 3); function container_caption_width($width, $atts, $content) { return '100%'; }