How to Change WordPress Media URL – 2026

Last updated on November 3rd, 2024 at 06:02 am

WordPress assigns media URLs based on the default upload path and your site’s URL structure. However, there are times when you may want to change the media URL to a custom directory, external storage, or a CDN (Content Delivery Network) for better performance. In this guide, we’ll show you how to change the WordPress media URL without using a plugin.

Remember to use a child theme to ensure your customisations are protected from theme updates.


Why Change WordPress Media URLs?

Changing the media URL can:

  • Improve Performance: Use a CDN to serve images and files faster.
  • Organise Media: Point media files to a custom directory for better file management.
  • Fix URL Conflicts: Adjust URLs if you’ve moved your media files to a new location.

Step 1: Modify Media Upload Path and URL

If you’ve moved your media files to a custom directory or external storage, you’ll need to update both the upload path and URL in WordPress.

Update the wp-config.php File

Add the following code to your wp-config.php file:

/*
 * Snippet: How to Change WordPress Media URL – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1593
* Tested with WooCommerce 10.7.0
* "This code changes the media upload path and URL in WordPress"
*/ define( 'UPLOADS', 'wp-content/my-custom-media' );

How This Works

  • Custom Upload Directory: This code sets a new directory (wp-content/my-custom-media) for media uploads.
  • Relative Path: The new directory path must be relative to your WordPress installation.

Step 2: Change the Media URL Using Functions.php

You can also modify the media URL using a custom function in your theme’s functions.php file or in a child theme.

/*
 * Snippet: How to Change WordPress Media URL – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1593
* Tested with WooCommerce 10.7.0
* "This function changes the media URL in WordPress"
*/ function wcsuccess_change_media_url( $url ) { return str_replace( 'wp-content/uploads', 'cdn.yourwebsite.com/media', $url ); } add_filter( 'wp_get_attachment_url', 'wcsuccess_change_media_url' );

How This Works

  • URL Replacement: This code replaces the default media URL (wp-content/uploads) with a custom URL (cdn.yourwebsite.com/media).
  • CDN Integration: You can modify the URL to point to a CDN for improved performance.
See also  How to Change the Additional Information Text on WooCommerce Checkout - 2026

Step 3: Use Shortcodes to Display Media with Custom URLs

If you need to display media using custom URLs dynamically, use the following shortcode in your functions.php file.

/*
 * Snippet: How to Change WordPress Media URL – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1593
* Tested with WooCommerce 10.7.0
* "This shortcode displays media with custom URLs"
*/ function wcsuccess_custom_media_shortcode( $atts ) { $atts = shortcode_atts( array( 'id' => '', ), $atts, 'custom_media' ); $url = wp_get_attachment_url( $atts['id'] ); return '<img src="' . esc_url( $url ) . '" alt="Custom Media" />'; } add_shortcode( 'custom_media', 'wcsuccess_custom_media_shortcode' );

How to Use the Shortcode

Insert the shortcode in your posts or pages as follows:

[custom_media id="123"]

Replace 123 with the ID of the media item you want to display.


Step 4: Update URLs in the Database

If you’ve moved your media files to a new location, you’ll need to update old URLs in your database. You can use a tool like WP-CLI or run a search-and-replace query directly in your database.

Example SQL Query

UPDATE wp_posts SET post_content = REPLACE(post_content, 'wp-content/uploads', 'cdn.yourwebsite.com/media');

Best Use Cases for Changing Media URLs

  • Use a CDN: Improve loading times by pointing media URLs to a CDN.
  • Organise Media Files: Move media to a specific directory for easier management.
  • Migrate Sites: Update media URLs if you’ve migrated your WordPress site to a new domain or hosting.
See also  How to Export WooCommerce Orders in CSV or XML Format Without a Plugin - 2026

Conclusion

Changing WordPress media URLs allows you to optimise performance, organise your media files, and resolve URL conflicts. Whether you’re integrating a CDN or moving media to a custom directory, the steps provided here will help you customise your media paths efficiently.

Always test these changes on a staging environment before applying them to your live site. Don’t forget to use a child theme to protect your customisations. For further customisations, explore our wp-config generator to simplify your configurations.

5 1 vote
Article Rating

Stay In Touch

Was this post helpful? Why not show your support and buy me a coffee?

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Scroll to Top
0
Would love your thoughts, please comment.x
()
x
×