Using Subdirectories to Store WordPress Media Files

Publish: 2014-09-02 | Modify: 2014-09-02

WordPress by default stores multimedia files in the /wp-content/uploads/ directory, and the generated image URL format is xxx.com/wp-content/uploads/year/month/abc.jpg. However, many websites choose to use a dedicated subdomain to store images for better management and optimization. Here's how you can do it step by step.

  1. After WordPress version 3.5, the option for media path can no longer be found in Settings >> Media. However, you can retrieve the option by adding the following code to the functions.php file in your theme directory:

    // Retrieve upload settings
    if(get_option('upload_path')=='wp-content/uploads' || get_option('upload_path')==null) {
    update_option('upload_path',WP_CONTENT_DIR.'/uploads');
    }
    // End retrieve upload settings
  2. In the backend, go to Settings >> Media, and you will see the added functionality options as shown in the image below. Now you can modify the upload path. For example, if you want to use the subdomain img.xxx.com to store images, replace wp-content/uploads with img. You don't need to worry about the web directory. Don't forget to fill in the complete path as http://img.xxx.com, and save it. Media Settings

    Figure 2-2: Media Settings
  3. Don't rush, we're not done yet. We still need to create a folder named "img" in the root directory of your website to store multimedia files. Then, in the domain management, point the A record to your hosting IP (assuming your hosting supports subdomains). The record value should be "img", as shown in the image below.

    ![Domain Resolution](http://cdn.xiaoz.top/wp-content/uploads/2014/08/img.jpg)
    Domain Resolution
  4. Finally, the path of multimedia files changes from xxx.com/wp-content/uploads/year/month/abc.jpg to img.xxx.com/year/month/abc.jpg. This makes it much easier to manage images, and all images are stored in the img directory.

If you encounter any problems during the process, you can add me on QQ: 337003006. If you find this article helpful, don't forget to share it on your Weibo. Feel free to leave any suggestions in the comments below.


Comments