Skip to content

Cookie-free JS, CSS, etc for WordPress

These 3 steps will help you to set up cookie free JavaScript, CSS and other files for your WordPress installation. (This is especially helpful for changing the domain of certain JS/CSS file references added by plugins.)

Before you start, make sure your blog URL has a leading www (e.g. www.example.com, not example.com) in your WordPress settings. (Cookie-tech-stuff)

Step 1) Create a sub-domain pointing to your web root (e.g. static.example.com pointing to /home/example/public_html/ instead of it pointing to, say, /home/example/public_html/static/).

Step 2a) Install CDN Rewrites plugin. (Did not work for me, so I had to revert to another way). If this is you as well, enter step 2b.1.

Step 2b.1) Install Free CDN plugin. Change its settings if you want to.

Step 2b.2) Edit /wp-content/plugins/free-cdn/free-cdn.php.

Find

if (isset($parts['port']))
{
$parts['host'] .= '.$parts[port].nyud.net';
unset($parts['port']);
}
else
{
$parts['host'] .= '.nyud.net';
}

around line 282. Right after this add:

$parts['host'] = 'static.example.com'; // my own edit

and make sure the domain matches the one you set up in step 1.

If you want to keep your edit safe, change the version number of the plugin to something like "1200" (at the beginning of the file) so that WP won't suggest updating the plugin.

[UPDATE: Remember to set WP cookie domain in wp-config.php: define('COOKIE_DOMAIN', 'www.yoursite.com');]

7 Comments

  1. Before you start, make sure your blog URL has a leading www (e.g. http://www.example.com, not example.com). (Cookie-tech-stuff)

    This part was not clear. Exactly how do you effect the first change?

    Step 1) Create a sub-domain pointing to your real web root (e.g. static.example.com).

    It is not clear to me what the difference between the real web root and the web root is? Is there a fake web root? Perhaps an example would be good.

    Posted on 27-Jan-11 at 3:01 pm | Permalink
  2. Jussi wrote:

    Thank you for your comment!

    I've revised the article a bit, does it make any sense now?

    - Step 0: in wordpress settings
    - Step 1: added file system example

    Posted on 27-Jan-11 at 3:18 pm | Permalink
  3. Sorry, it is still very confusing. Some questions:
    Is 2b.1 a fallback for 2a ? Do you recommend that we try 2a? What does it do? Is 2b better? Do we need to move any files to a static directory? What does the CDN have to do with the cookies? Is this free CDN slow? Do I need to wait a certain amount of time after enabling the static subdomain before doing the other steps? Why would my site look completely screwed up after doing these steps? Why did you put the UPDATE after the other instructions, and what is the purpose of that?

    Regards,
    harold

    Posted on 27-Jan-11 at 3:41 pm | Permalink
  4. Jussi wrote:

    You're a man of many questions. 🙂

    As the idea of this article was to speed up a wp installation, you'll find more ways of doing that at http://mashable.com/2010/07/19/speed-up-wordpress/

    And as to your questions, I think you'll find good answers at http://emonweb.com/serve-wordpress-static-resource-from-cookie-free-sub-domain/ or http://www.ravelrumba.com/blog/static-cookieless-domain/

    Posted on 28-Jan-11 at 12:56 pm | Permalink
  5. Yes, I need to ask questions in order to understand the procedure. When a procedure doesn't make sense, it often ends up not working, I find. In this case, there are quite a few elements missing in the explanation.

    The post at emonweb was confusing as well. I am trying to get the author to explain his post.

    The post at ravelrumba was written quite clearly, and I'll give that a try. He doesn't suggest using a CDN, which has no obvious connection to cookies, and so it makes more sense to me. In addition, he shows exactly how each step should be carried out. If he doesn't show locations of the files that need to be modified, he at least discusses the theory in a complete and intelligent way so people only have themselves to blame if it doesn't work.

    Obviously, I had read all the comments in this post before I posted, but they gave no help, since they were all just praise for how well it worked.

    Regards,
    Harold

    Posted on 28-Jan-11 at 2:36 pm | Permalink
  6. sOliver wrote:

    You know of any way how to do it without rewriting to WWW? Modifiy the WordPress set cookie function?

    Posted on 14-Jul-11 at 4:34 pm | Permalink
  7. Jussi wrote:

    Due to how cookies are set up, a cookie-free subdomain is required. There might be several ways to achieve this, but I find the solution in this article to be the easiest for me.

    Posted on 14-Jul-11 at 6:49 pm | Permalink