Serve Media Library Files From a Different Domain or CDN
Points uploaded media at a separate host (like a CDN or assets subdomain) instead of your main site URL. Two approaches shown — a direct option update, and a filter-based version that avoids writing to the database.
/**
* Change Media Gallery URL
*/
if ( empty( get_option( 'upload_url_path' ) ) ) {
update_option( 'upload_url_path', 'http://assets.website.com/wp-content/uploads' );
}
A cleaner alternative — filters the value on the fly instead of saving it to the options table:
/**
* Change Media Gallery URL
*/
add_filter( 'pre_option_upload_url_path', function() {
return 'http://assets.website.com/wp-content/uploads';
});
More WordPress snippets
Register and Output a Custom Nav Menu
Adds a new menu location you can assign in Appearance → Menus, shows how to output it in a template, and includes the multi-menu version too.
Define a Reusable Global String
A simple pattern for storing a value — like a phone number or address — in one place and reusing it anywhere in your theme.
Auto-Escape HTML Typed Inside Code Tags
Makes sure any markup typed inside <code> or <tt> tags in the editor gets escaped automatically, so it displays as visible text instead of being rendered as real HTML.