To speed up your site, it is necessary to reduce Http request. When you analyze your website with Gtmetrix , Pingdom or Page Speed, you will be suggested to use cookieless domain for serving static content. The best practice is that use a cookieless subdomain to serve your static content like images and stylesheets. In this tutorial we will learn how to create cookie free domain in WordPress.
Use cookie-free domains
Here is a screenshot of Gtmetrix results which suggest to use cookie free domains to speedup your site. As we know Gtmetrix checks sites with Google pagespeed and Yahoo Yslow tools and provide recommendations to speed up our site so When you analyze your site with Gtmetrix you can find it under the Yslow tab.
At above image, Gtmetrix gives warning to use cookie less domain because tested site score for this is zero. Same we will see when we test our site with Pingdom speed test to use cookie free domains.
How to Create Cookie Free Domain for WordPress (Updated In 2020)
In this beginner tutorial, we will follow these steps.
- 1. Create subdomain point to static content folder
- 2. Define cookie domain
- 3. Ensure subdomain is cookieless
Serving static content or resources from a cookieless domain reduces the total size of requests made for a page. Cookies created on the main domain also serve on the subdomain also by default.
In process to create cookie free subdomain first, we create a subdomain like “cdn.mydomain.com” or “static.mydomain.com” whichever you like. After creating subdomain, Point this subdomain to your wp-content directory of your WordPress folder by define Document Root to “/public_html/wp-content” instead of /public_html/static.
The second step is defining the cookie domain in wp-config.php file. So Add two lines in wp-config.php file. first line is defining the URL to serve static content and second define the cookie domain.
define("WP_CONTENT_URL", "http://static.yourdomain.com"); define("COOKIE_DOMAIN", "www.yourdomain.com");
There are two cookie setters in WordPress, First is WordPress and second google analytics. we set cookie for WordPress in the above step so now we have to replace the analytics code to set the cookie domain by “_setDomainName” value looks like below code.
_gaq.push( ['_setAccount', 'UA-xxxxxxx-1'], ['_setDomainName', 'www.mydomain.com'], ['_trackPageview'] );