8

How can I insert pictures into a post without any hard coded dimensions (e.g. <img src="" alt="" /> instead of <img src="" alt="" width="" height="" />)? I don't want my users to switch to the HTML tab and remove the parameters by themselves, so I was wondering if there is any filter I can use to achieve this?

Note: I'm already inserting them in "Full size".

2

I don't know if this is the best way to do this, but it works for me.

In the functions.php of the theme you are using, put this:

function remove_img_src($html)
{
    $html = preg_replace('@(width|height)="([0-9])+" ?@i', '', $html);

    return $html;
}

add_filter('image_send_to_editor', 'remove_img_src', 10, 8);

It uses regular expresions to change the output that is inserted in the editor.

  • It doesn't seem to work when pulling the images from the post gallery, using wp_get_attachment_image. Any suggestions? – João Apr 4 '12 at 20:14
  • 1
    For what i see on the code, it seems that it can't be done without hardcode the changes. There's a filter in line 663, but on line 665 is harcoded the $hwstring wich contains that width/height definition. – Eduardo Reveles Apr 4 '12 at 21:46
  • Any more ideas on how to solve this? – João Apr 5 '12 at 15:39
1

Looks like you can do it with image captions, but not images at present:

From the wp-hackers mailing list:

I've got a filter I use that forces captions to be responsive on the frontend. Definitely cuts down on the headaches.

Gist here: https://gist.github.com/2243601

Drew

  • Thanks! In the meantime I've used wp_get_attachment_image_src() to get the src for the images. I think it ends up by being the most easy solution and it requires no filters. – João Apr 6 '12 at 11:29
1

I found a solution in the meantime: wp_get_attachment_image_src() to get the src for the images. I think it ends up by being the easiest solution and it requires no filters.

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.