Varnish and WordPress Comment Cookies

I wrote before that I’ve been running some experiments with Varnish lately. Not that I have huge traffic here but I’ve always used my own website to test things out. As I wrote earlier, the problem with WordPress and Varnish is that WordPress relies on cookies which create cache misses on Varnish and in that previous post I shared a snippet on how to strip all incoming and outgoing cookies, which hopefully solves one problem.

The second problem now is that cookies are disabled throughout the whole website (except the admin section of course) so if you’ve got commenters on your website, their browsers will no longer save their names and e-mails, so they’ll have to type them in every time they want to leave a comment. I agree it’s a pain, which is why I was searching for a solution, which turned out to be quite simple — handle it all on the client side using javascript.

So I wrote a little plugin which you can download and use. It simply enqueues a javascript file if the request is a singular page and comments are open. It passes in some cookie constants used by WordPress. The javascript itself hooks onto the submission of the comment form to create cookies and restores the form fields upon page load.

Note that the plugin can fail if you’re rendering your comment form differently (with different DOM IDs) from WordPress, and don’t forget to purge.url your Varnish cache after activating!

About the author

Konstantin Kovshenin

WordPress Core Contributor, ex-Automattician, public speaker and consultant, enjoying life in Moscow. I blog about tech, WordPress and DevOps.

2 comments

  • You should throw this up on github!

    Awesome work as always Konstantin!

    Glad to see some more people playing around with varnish and nginx!! Very exciting :)

    Love the site redesign by the way, very sharp.

    • Aaron, thanks for your kind words, I will some day, perhaps as a full-blown Varnish solution for WordPress. I also stumbled across this article by Tim Whitlock, seems he’s been up to something there too. Also interesting to see VarnishAdmin as part of PECL these days :) Thanks for your comment!