Forcing max-width for image captions


I’ve implemented a filter to my image captions based on this question I asked a few days ago. How can I slip into the caption the css style of max-width as well? I think it can be done with preg_replace, but I can’t get it to work.

All I really am trying to do is force the image caption text to wrap inside the div.

The default html output looks like…

<div id="attachment_1071" style="width: 310px" class="wp-caption alignright">

I need it to look like…

<div id="attachment_1071" style="width: 310px; max-width:310px;" class="wp-caption alignright">

Here is what the filter currently looks like…

function my_caption_shortcode($atts) {
  if (isset($atts['caption'])) {
    remove_filter( current_filter(), __FUNCTION__);
    $atts['caption'] = do_shortcode($atts['caption']);
    add_filter(current_filter(), __FUNCTION__);
  //preg_replace("/width:{????}px/i", "width:{1}px; max-width:{1}px;", $atts['caption']);
  return $atts;
, eddie 3 years 2020-06-07T11:10:26-05:00 0 Answers 94 views 0

Leave an answer