Instead of adding the Gallery block and dropping a bunch of images onto it, open the Media Gallery, upload the images there, and then add the gallery to the Gallery block.

Why? It seems that dragging and dropping or selecting several images directly from the Gallery block sends them all to the server concurrently. The uploads may be completing sequentially, but WordPress does a lot of post-processing like resizing for each uploaded image. This post-processing is very memory and cpu intensive, which can cause the server to respond very slowly or outright crash1 if too much of it is happening at once. At least as of WordPress 6.3.2, this background post-processing runs in parallel for all the images selected from or drag-and-dropped directly onto the Gallery block. With the Media Library approach, the images are processed one by one.

If you’re struggling to upload even a single image you may need to tweak a few PHP settings2. This article here describes a few ways of doing it. The htaccess mechanism worked well for me:

# I use Cloudflare as a proxy for my site. Their free tier has some additional limitations.
# Cloudflare's max is 100M, so there's no point in allowing file uploads larger than that.
php_value upload_max_filesize 100M
php_value post_max_size 100M

# Cloudflare's timeout is around 100s. PHP default is 30s. The value below could probably be 100, but I'm leaving it higher so that image post-processing completes in the background even if the request is aborted by the proxy.
php_value max_execution_time 300

# Default is 128M and not enough for images >~6mb. Photos from recent phones are around this size, but often larger.
php_value memory_limit 256M

I initially tried tuning the max_file_uploads setting. It controls how many concurrent file uploads PHP will accept. It didn’t make a difference in my testing, though! WP uploads images one at a time anyway, but background processing is concurrent and not restricted by this. Default is 20.

When the server is overloaded it either responds with a generic HTTP 500-series error or closes the connection. WordPress can’t discern the cause of the issue and, unfortunately, its error messages aren’t clear. They may say something like “The response is not a valid JSON response.” or “HTTP Error“. It’s not a valid JSON response because the web server is returning an HTML page containing some human-readable error text. If you open developer tools in the browser, reload the page, and click on the failing media requests in the Networks tab, you’ll see what I mean.

This ticket may be about this bug, or at least a very similar issue.

  1. That’s what happened in my case. I run WordPress inside Docker on a small VPS and give the container a memory limit. When it goes over that limit the container gets killed (out-of-memory / OOM). ↩︎
  2. If you’re using a shared host like this may not be allowed. Your best bet is to reach out to their support folks and plead your case for a higher memory limit. ↩︎

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.