• HI there,
    I am currently running a header CSP check on my site for or XXS errors.
    On one page, I have used the WordPress shortcode [caption] many times to show many images.
    When using the caption shortcode, https://codex.ww.wp.xz.cn/Caption_Shortcode you must enter a width value. But this value is then inserted as an inline style value:
    [caption id="" align="aligncenter" width="300"].......[/caption] results in <figure style="width: 300px" being output as inline styling.
    I would like to enter this width from a style sheet.
    Can anyone advise on this please?

    • This topic was modified 5 years, 7 months ago by haddlyapis.

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Look at the Code Reference for the shortcode: https://developer.ww.wp.xz.cn/reference/functions/img_caption_shortcode/
    and you can see that you can override the entire markup with the ‘img_caption_shortcode’ filter, or you can affect the width with the ‘img_caption_shortcode_width’ filter.
    The comment on the width filter says “To remove this inline style, return zero.”

    Thread Starter haddlyapis

    (@haddlyapis)

    Hi there,
    thank you for pointing me in this direction.
    I tried adding the following to my functions.php file, but this was not the solution:
    apply_filters( 'img_caption_shortcode_width', $width = 0 , $atts, $content );
    As I am not a plugin developer, I just need to know where to return the 0. thx in advance.

    OK, the way it works is that filter functions can exist at any time, and are applied by the core function in a certain place. You add your filter function to the list to be applied.
    So instead of the code you show, it would be more like

    function my_img_caption_shortcode_width ($width, $atts, $content) {
      return 0;
    }
    add_filter( 'img_caption_shortcode_width', 'my_img_caption_shortcode_width', 10, 3 );

    You could check the $atts or $content if you need to, but if you will always return 0, then no need to.

    Thread Starter haddlyapis

    (@haddlyapis)

    @joyously. Thank you so much. This worked perfectly. All the best!!!

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘stop inline styling when using [caption] shortcode’ is closed to new replies.