Written on 5. April 2022 by Lennart Christensen

Use external url for files in media in WordPress

This feature allows you to use images hosted anywhere on the Internet, instead of having to host images on your own server.

It could be that you had a staging/development environment of your active website and don't want a media library uploaded twice and in addition you don't need to spend time moving/migrating your media library. It could also be that via a CDN you want to download images and display them on your website where they are also visible in your WordPress media library as thumbnails etc.

Remember the mediums must match. So on the page you are referring to, there must of course be an image uploaded with the url you are referring to.

function external_mediaurl($wp_get_attachment_url){
		$external_mediadir = 'https://hjemmesideurl.dk/filer/';
		$filename = basename($wp_get_attachment_url );
		$newurl = $external_mediadir.$filename;
		return $newurl;
	}
	add_filter('wp_get_attachment_url', 'external_mediaurl');