Regenerate PDF thumbnails in WordPress


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/

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.


File Access Rights

For shared web hosting, the above command should be all you need. If you have a managed server, cloud server, or VPS, 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/'

Missing Environment Variables

You may see a warning or error message saying HTTP_HOST or SERVER_NAME is not defined. This may happen if a theme or plug-in on your WordPress site assumes these values always exist. This is incompatible with wp-cli. In most cases, these warnings or error messages do not prevent regeneration of thumbnails. 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=""; export SERVER_NAME=$HTTP_HOST; /usr/local/bin/wp media regenerate --yes --path=/var/www/vhosts/'

Replace the values of HTTP_HOST and path with the appropriate values for your web site.

ImageMagick Security Violations

Thumbnails for PDF files will not be generated if you see a security policy message similar to the following…
Warning: attempt to perform an operation not allowed by the security policy ‘PDF’ @ error/constitute.c/IsCoderAuthorized/408
This can happen because of an ImageMagick update disabling PDF file access. To fix this problem, edit the security policy file with your favorite editor. The policy file is 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: Allowing untrusted users to upload any files to your WordPress site is crazy. It will cause security problems. Even a trusted user might unknowingly upload a malicious file. A specially crafted PDF file might exploit a previously unknown flaw in Imagick. This is why PDF file processing is disabled by default.

Web Server Prerequisites

Make sure your web server has everything it needs to generate thumbnails for a wide variety of media files. For example, install curl, ghostscript, ffmpeg, imagemagick, php-curl, and php-imagick packages with the following commands on Debian.

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

Also, follow the installation instructions for wp-cli if you haven’t installed it yet. Make sure you configure a cron job to keep wp-cli updated too.