WordPress will create thumbnail images in your Media Library if your server software is installed correctly. In some cases the thumbnails are no longer generated for new media file uploads. In other cases, thumbnails might be generated for image files, but not PDF files. This article will show you how to regenerate thumbnail images for all media files. This article also shows you how to make WordPress generate thumbnail images for PDF files.
The fastest way to regenerate thumbnails for your WordPress Media Library is with wp-cli. This is because wp-cli is a command-line utility that bypasses a lot of the overhead of using a WordPress plug-in.
To regenerate all media thumbnails, use the wp media regenerate command. Log in to your web server with SSH and run the following command.
wp media regenerate --yes --path=/var/www/vhosts/my-site.com/htdocs
Replace the path parameter shown above with the path to your WordPress web site.
Wait for the command to finish processing all your media files. Then log in to your WordPress site and open the Media Library. You should see all your media files have thumbnails generated based on their content.
TROUBLESHOOTING THUMBNAIL REGENERATION
- File Access Rights
For shared web hosting, the above command should be all you need. If you have a managed server, cloud server, or virtual private server, the command above could cause problems with file access rights. To prevent this, use the su command to switch users to the same user that your web server runs with. For example, on an Apache web server with www-data as the account the web server runs in, you will use the following command.
su -s /bin/bash www-data -c '/usr/local/bin/wp media regenerate --yes --path=/var/www/vhosts/my-site.com/htdocs'
- Missing Environment Variables
You may see a warning or error message saying HTTP_HOST or SERVER_NAME is not defined. This is because a theme or plug-in on your WordPress site was developed with a bad assumption that these values are always defined. This behavior is incompatible with wp-cli. In most cases, these warnings or error messages will not prevent the thumbnails from being regenerated. However, you can work around this problem by exporting these values before executing wp-cli as shown below.
su -s /bin/bash www-data -c 'export HTTP_HOST="www.my-site.com"; export SERVER_NAME=$HTTP_HOST; /usr/local/bin/wp media regenerate --yes --path=/var/www/vhosts/my-site.com/htdocs'
Replace the values of HTTP_HOST and path with the appropriate values for your web site.
- ImageMagick Security Violations
If you see a security policy message similar to the following, then thumbnails for PDF files will not be generated.
Warning: attempt to perform an operation not allowed by the security policy ‘PDF’ @ error/constitute.c/IsCoderAuthorized/408
This can happen because of a new or updated installation of ImageMagick disabling PDF file access. To fix this problem, edit the security policy file with your favorite editor. The policy file is usually located at: /etc/ImageMagick-6/policy.xmlSearch for the following line in the file.
<policy domain="coder" rights="none" pattern="PDF" />
Change the rights value from none to read as follows.
<policy domain="coder" rights="read" pattern="PDF" />
Caution: If you allow untrusted users to upload PDF files to your WordPress site, which is crazy, then enabling PDF files access for Imagick could cause security problems. A specially crafted PDF file might exploit a previously unknown flaw in Imagick. PDF file processing is disabled by default for this reason.
- Web Server Prerequisites
Make sure your web server has everything it needs to generate thumbnails for a wide variety of media files. For example, on Debian, install curl, ghostscript, ffmpeg, imagemagick, php-curl, and php-imagick packages with the following commands.
apt update apt install curl ffmpeg ghostscript imagemagick php-curl php-imagick
Enable PHP curl and imagick modules, then restart your web server. On Debian with Apache, that command might look like the following.
phpenmod -v ALL curl imagick service apache2 restart