View Categories

How to rename my WebP images from single to double extension

If you generated the WebP images with a single extension (image.webp) and want to use a plugin (like LiteSpeed Cache) that requires WebP files with a double extension (image.jpg.webp), you can either rename the WebP files manually or let ShortPixel handle the task.

Manual renaming #

To manually rename the files, you need access to a shell console with Bash. If you’re unsure, consult your hosting provider or a trusted developer.

Simply run the following script to rename all images in a folder (provide the folder as a parameter):

#!/bin/bash
cd $1
for file in *.webp
do
if test -f "${file%.webp}.jpg"
then
echo "${file%.webp}.jpg" EXISTS. Moving "$file" to "${file%.webp}.jpg.webp"
mv "$file" "${file%.webp}.jpg.webp"
fi

if test -f "${file%.webp}.jpeg"
then
echo "${file%.webp}.jpeg" EXISTS. Moving "$file" to "${file%.webp}.jpeg.webp"
mv "$file" "${file%.webp}.jpeg.webp"
fi

if test -f "${file%.webp}.png"
then
echo "${file%.webp}.png" EXISTS. Moving "$file" to "${file%.webp}.png.webp"
mv "$file" "${file%.webp}.png.webp"
fi

if test -f "${file%.webp}.gif"
then
echo "${file%.webp}.gif" EXISTS. Moving "$file" to "${file%.webp}.gif.webp"
mv "$file" "${file%.webp}.gif.webp"
fi
done

You will need to execute this in each folder or incorporate it into a shell script that traverses the directory structure. To achieve this, you can use a command like the following (assuming the script above is named replace-in-folder.sh):

find . -type d -exec ./replace-in-folder.sh {} 

Automatic renaming #

Automatic renaming is only possible if you first restore all the images. Please note that unless you have an Unlimited plan, this process will use additional credits. Follow these steps:

  1. Restore your images.
  2. Add the following constant to your wp-config.php file: define('SHORTPIXEL_USE_DOUBLE_WEBP_EXTENSION', true);
  3. Optimize your images again while creating WebP files.