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;
}

Topic shortcode captions Wordpress

Category Web


why not set white-space : normal ?

https://developer.mozilla.org/en-US/docs/Web/CSS/white-space

without seeing the rest of your css, its hard to determine why the text isn't wrapping naturally

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.