1

The Image Widget includes width and height on the images which I've unsuccessfully have been trying to filter out so I can have fluid width.

I've tried the following, which seems to be the most common solutions for similar problems, in functions.php, but without success:

function responsive_image_widget( $attr = array() ) {
    unset($attr['width']);
    unset($attr['height']);
    return $attr;
}
add_filter( 'image_widget_image_attributes', 'responsive_image_widget' );

and

function responsive_image_widget($html) {
  $html = preg_replace('/(width|height)=["\']\d*["\']\s?/', "", $html);
    return $html;
}
add_filter('image_widget_image_attributes','responsive_image_widget', 10);

Here is a link for the Image Widget's filter documentation.

Any help is much appreciated!

1

It is not possible to remove it from attribute filter. When rendering image, plugin is doing as below.

$output .= rtrim("<img $hwstring");
foreach ( $attr as $name => $value ) {
  $output .= sprintf( ' %s="%s"', $name, $value );
}
$output .= ' />';

It is attaching width and height string beforehand and only applying attribute. Sad thing is that width and height is not kept in attribute filter.

** I am not sure this should be posted as answer. I posted here because I could not comment in your question.**

1
  • Thanks for the explanation. I worked around it by removing it using jQuery.
    – Eystein
    Jun 13 '14 at 14:44
0

Have you tried targeting the width and height filters directly?

(from the plugin code)

   $instance['width'] = apply_filters( 'image_widget_image_width', abs( $instance['width'] ), $args, $instance );
    $instance['height'] = apply_filters( 'image_widget_image_height', abs( $instance['height'] ), $args, $instance );

To use your code as an example:

function responsive_image_widget( $currentWidth ) {

   $currentWidth = '';
   return $currentWidth;
}

add_filter('image_widget_image_width', 'responsive_image_widget' );     
2
  • Thank you, but unfortunately this didn't work for me. I "solved" it by removing them with jQuery.
    – Eystein
    Jun 13 '14 at 14:43
  • Ha! That was my next suggestion. :-)
    – leromt
    Jun 13 '14 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.