I couldn't figure out how wordpress handles media library.

  • I copied the media files from one vm to another using gcloud compute scp...

  • The files are successfully copied to the wp-content/uploads/ folder of the destination.

  • I updated wp_postmeta and wp_posts and verified that the image urls are updated as intended. However, when I access the images urls, it returns 404 error.

  • I tried regenerating thumbnail and have errors. The fullsize image file cannot be found in your uploads directory at .... They are not accessible via browser too. However, the files are there in the wp-content/uploads/ folder and the urls are correct.

  1. Do all images need to go through the upload process in wordpress?
  2. Images deleted from the server still have reference in the media library albeit the images are not found. Is this the intended behavior?
  • "They are not accessible via browser too" sounds like you copied them to the wrong location and/or it is not accessible. Even when the files don't appear in WP media library, you should be able to get them directly.
    – kero
    Commented Sep 14, 2021 at 16:52

1 Answer 1

  1. Yes, pretty much.
  2. Yes.

When you upload an image to WordPress a few things happen. These might not be in the precise order, I'm going off memory, but it shouldn't change much:

  1. It makes sure the image has a unique filename and puts it in the wp-content/uploads directory.
  2. It creates an attachment post for the image.
  3. It saves the file path of the original image as post meta.
  4. It generates metadata for the image, including dimensions etc. and saves it as post meta.
  5. It uses registered WordPress image sizes to crate cropped and resized versions of the image which are saved in the uploads directory.
  6. It saves the file paths and dimensions of the resized versions of the image to post meta.

When an attachment is deleted from the media library, the original and resized versions of the image are deleted from the server.

What's important to know in your situation is that when an image is deleted from the server, absolutely nothing happens in WordPress. The file will just be missing when WordPress attempts to find it.

WordPress also never scans the uploads directory and lists files in the media library. It's not a file explorer, and only lists attachments from the database. It is build so all media management is done in WordPress. If you try to manage media directly on the server you will get unexpected results in the media library.

There's a few functions you should be aware of for what you're trying to do:

  • media_handle_upload() - Saves a file submitted from a POST request and create an attachment post for it.
  • media_handle_sideload() - Similar to media_handle_upload(), but you can use it for images that are already on the server somewhere.
  • update_attached_file() - If you have the path to a file, and want to update an existing attachment to use it, you can use this function.
  • wp_generate_attachment_metadata() - Saves all the meta data for the attachment, and generates the resized and cropped versions. You would want to run this after using update_attached_file().

There's more to it, but that's an overview of what needs to happen for media in WordPress.

  • 1
    ... just a small footnote addition, to the informative answer, for command-line fans; there's some command line stuff here developer.wordpress.org/cli/commands/media/import to "create attachments from local files or URLs." cheers
    – birgire
    Commented Sep 14, 2021 at 17:42
  • Thanks @jacobpeattie! That makes things clearer! As a newbie, it has been a struggle for me.... Commented Sep 15, 2021 at 6:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.