How to Store WordPress Media Files on a Subdomain
By default, WordPress stores media files in the /wp-content/uploads/ directory, generating image URLs in the format xxx.com/wp-content/uploads/year/month/abc.jpg. However, for better management and optimization, many websites choose to use a dedicated subdomain to host images. Here is a step-by-step guide on how to achieve this.
1. Restore the Upload Path Setting
After WordPress version 3.5, the option to set the media upload path is no longer visible under Settings > Media. You can restore this option by adding the following code to your theme's functions.php file:
// Restore upload settings
if(get_option('upload_path')=='wp-content/uploads' || get_option('upload_path')==null) {
update_option('upload_path',WP_CONTENT_DIR.'/uploads');
}
// End of restore upload settings
2. Modify the Upload Path in the Dashboard
After adding the code, you will see the new option in Settings > Media (see Figure 2-2). You can now modify the upload path. For example, if you want to use img.xxx.com to store images, replace wp-content/uploads with img. If your server does not have a web directory, you can ignore that part. Ensure you enter the full path, such as http://img.xxx.com, and then save the changes.
Figure 2-2: Multimedia Settings
3. Create the Directory and Configure DNS
This is not yet complete. You need to create an img folder in your website's root directory to store the media files. Then, in your domain management settings, create an A record pointing to your host's IP address (ensure your host supports subdomains). Set the record value to img (see Figure below).
DNS Resolution
4. Final Result
Finally, the media file path changes from xxx.com/wp-content/uploads/year/month/abc.jpg to img.xxx.com/year/month/abc.jpg. This makes image management much more convenient, as all images are now stored in the img directory.
If you encounter any issues during the process, feel free to contact via QQ: 337003006. If you find this article helpful, please share it on your Weibo. You are also welcome to leave your suggestions in the comments below.