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.
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.
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.
I have been working with WordPress and WooCommerce since 2012 and have developed a deep knowledge of the content management system. Since 2012, I have developed several plugins and designed dozens of websites utilising different frameworks, CMS’s and programming languages. I am proficient in PHP, Python, Java, C, C++, R and JavaScript with limited experience in Go, Kotlin and Swift.
Educationally, I have a Master’s degree in cyber security a Bachelor’s (Hons, First Class) in Applied Research and a Graduate Certificate in Data Science. I’m currently undertaking PhD studies investigating IoT cybersecurity. I recently graduated with First Class Honours and Masters of Information Technology, receiving the Executive Dean’s Award for studies undertaken in the 2021 and 2022 academic years. I have worked in the information technology industry for the past 11 years primarily as a software/web developer specific to design, optimisation, network management and security. My research interests are in the areas of Internet of Things (IoT), 5G and Beyond Networks, information security for wireless networks and software development.
Stay In Touch

