📖 Scenario: Your WordPress site loads slowly for international visitors because all assets are served from a single origin server. You need to integrate a CDN to serve static assets from edge servers worldwide.
🎯 Goal: Set up CDN integration for a WordPress site by rewriting asset URLs in functions.php and configuring cache headers.
📋 What You'll Learn
Define a CDN URL constant in functions.php
Create a filter function to rewrite upload URLs to the CDN
Hook the filter to WordPress content and attachment filters
Add cache-control headers for static assets
💡 Why This Matters
🌍 Real World
CDN integration is essential for WordPress sites serving global audiences, reducing load times by 50-80% for international visitors.
💼 Career
WordPress developers frequently set up CDN integration for client sites to improve performance and handle traffic spikes.
Progress0 / 4 steps
1
Define CDN URL constant
In functions.php, define a constant CDN_URL with the value 'https://cdn.yoursite.com'.
Wordpress
Hint
Use define('CDN_URL', 'https://cdn.yoursite.com') to create a PHP constant.
2
Create URL rewrite function
Create a function called rewrite_cdn_urls that takes $content as a parameter. Inside, get the site URL with get_site_url(), then use str_replace to replace the site's upload path with the CDN upload path. Return the modified content.
Wordpress
Hint
Use str_replace() to swap the origin upload path with the CDN upload path.
3
Hook filters for content and attachments
Use add_filter to hook rewrite_cdn_urls to both 'the_content' and 'wp_get_attachment_url' filters so that post content and media URLs are rewritten to the CDN.
Wordpress
Hint
Use add_filter('the_content', 'rewrite_cdn_urls') and repeat for 'wp_get_attachment_url'.
4
Add cache headers for static assets
Create a function add_cdn_cache_headers that hooks into 'send_headers'. Inside, use header() to set Cache-Control to 'public, max-age=31536000' for requests whose URI contains wp-content/uploads.
Wordpress
Hint
Check $_SERVER['REQUEST_URI'] for upload paths and set cache headers with header().
Practice
(1/5)
1. What is the main purpose of integrating a CDN with a WordPress site?
easy
A. To disable all plugins for better performance
B. To increase the size of the WordPress database
C. To replace the WordPress hosting server completely
D. To speed up content delivery by caching static files globally
Solution
Step 1: Understand CDN function
A CDN stores copies of static content like images and scripts on servers worldwide to reduce load time.
Step 2: Connect CDN purpose to WordPress
Integrating a CDN with WordPress speeds up site loading by serving cached content from nearby servers.
Final Answer:
To speed up content delivery by caching static files globally -> Option D
Quick Check:
CDN speeds up delivery = A [OK]
Hint: CDN caches static files worldwide to speed up sites [OK]
Common Mistakes:
Thinking CDN increases database size
Believing CDN replaces hosting server
Assuming CDN disables plugins
2. Which of the following is the correct way to configure a CDN URL in WordPress settings?
easy
A. Use a plugin or theme option to specify the CDN URL for static files
B. Add the CDN URL in the WordPress Reading Settings
C. Set the CDN URL in the WordPress General Settings under Site Address (URL)
D. Replace the WordPress Admin URL with the CDN URL
Solution
Step 1: Identify where CDN URLs are set
CDN URLs are usually set via plugins or theme options that rewrite static file URLs.
Step 2: Understand WordPress settings roles
General Settings and Reading Settings do not control CDN URLs; Admin URL is unrelated.
Final Answer:
Use a plugin or theme option to specify the CDN URL for static files -> Option A
Quick Check:
CDN URL set via plugin/theme = C [OK]
Hint: CDN URLs are set via plugins or theme options, not general settings [OK]
Common Mistakes:
Trying to set CDN URL in General or Reading Settings
Replacing admin URL with CDN URL
Not using a plugin or theme for CDN integration
3. Given this WordPress plugin configuration snippet for CDN integration: