Remove image width and height from WordPress image URL

Is there a way to turn off automatic -widthxheight being added to my WordPress image URL? Like, for example;

http://xevens.com/projects/xe/wp-content/uploads/2014/10/alaska_winter_nights-1366×768-300×168.jpg
http://xevens.com/projects/xe/wp-content/uploads/2014/10/alaska_winter_nights-1366×768.jpg

Read More

That 300×168 is unnecessary to me in most cases. And now I need the image to be its original dimensions. If there’s no way to do that, can you at least brew me a nice little REGEX so I can get rid of -300×168 via jQuery?

Thanks!

Related posts

Leave a Reply

2 comments

  1. I can think of a few regex variations depending on the situation with the WordPress images.

    1

    If you want a regex which selects only the image name from resized images in the wp-content/uploads and EXCLUDES the resized portion, you could do something more specific:
    Working Demo

    /wp-content/uploads/[0-9]{4}/[0-9]{2}/(.*)(?:-[0-9]{1,5}x[0-9]{1,5})(..{2,4})
    

    Regular expression visualization
    Group $1 selects the file name (excluding the resized part) and group $2 selects the extension

    2

    Or, if you were moving your library and wanted to change the URL even for non-resized images, but still need to exclude the resized portion of the filename: Working Demo

    /wp-content/uploads/[0-9]{4}/[0-9]{2}/(.*)(?:-[0-9]{1,5}x[0-9]{1,5})(..{2,4})|/wp-content/uploads/[0-9]{4}/[0-9]{2}/(.*)
    

    Regular expression visualization
    Group $1 selects the file name (excluding the resized part), group $2 selects the extension, and group $3 selects entire filenames for those files that don’t have a resize. If you output $1$2$3 it will either combine $1 and $2, or show $3 – so you always get the filename without the resized portion.

    3

    Or if you want to select only the resized portion of the filename for any of the image uploads: Working Demo

    /wp-content/uploads/[0-9]{4}/[0-9]{2}/.*(-[0-9]{1,5}x[0-9]{1,5})..{2,4}
    

    Regular expression visualization
    Group $1 selects the resized portion of the files that are in the wp-content/uploads

    4

    Or perhaps you want to select the entire url for any images that are resized: Working Demo

    (/wp-content/uploads/[0-9]{4}/[0-9]{2}/.*-[0-9]{1,5}x[0-9]{1,5}..{2,4})
    

    Regular expression visualization

    $1 selects any url that contains the wp-content/uploads with any year/month folder and which has a resized image (ex: containing -123×456.jpg)

    I used #2 above when setting up redirects for all the images when migrating a WordPress website. We no longer wanted to use the YYYY/MM folders and we had different image resize proportions. The regex allowed the old images to redirect to the full-size image so at least they aren’t simply getting a 404.