{"id":8760,"date":"2018-01-03T04:00:40","date_gmt":"2018-01-03T08:00:40","guid":{"rendered":"https:\/\/blog.litespeedtech.com\/?p=8760"},"modified":"2018-01-03T04:00:40","modified_gmt":"2018-01-03T08:00:40","slug":"wpw-password-protected-posts","status":"publish","type":"post","link":"https:\/\/lswp.store\/index.php\/2018\/01\/03\/wpw-password-protected-posts\/","title":{"rendered":"WpW: Password Protection and Caching"},"content":{"rendered":"<p><img fetchpriority=\"high\" decoding=\"async\" class=\"alignnone\" src=\"https:\/\/lswp.store\/wp-content\/uploads\/2017\/12\/null-6.jpeg\" alt=\"WordPress Wednesday Password Protection and Caching\" width=\"640\" height=\"320\" \/><\/p>\n<p>Happy New Year, and welcome to another installment of <a href=\"http:\/\/blog.litespeedtech.com\/tag\/wordpress-wednesday\">WordPress Wednesday<\/a>!<\/p>\n<p><em><strong>Disclaimer<\/strong>: The information contained in this post is accurate for LSCWP v1.6.6.1 [<a href=\"https:\/\/wordpress.org\/plugins\/litespeed-cache\/#developers\">release log<\/a>]. If you are using a newer version of the plugin, some details may have changed. Please<a href=\"https:\/\/www.litespeedtech.com\/support\/wiki\/doku.php\/litespeed_wiki:cache:lscwp\"> refer to our wiki<\/a> for the latest!<\/em><\/p>\n<p>Let\u2019s talk about the relationship between password protection and caching. If you have ever had reason to hide one of your posts from the public view, you may have chosen to protect it with a password. Password-protected posts are a useful way to ensure that your content is visible only to a specific audience. WordPress cache plugins that use rewrite rules to define their caching behavior can handle password-protected posts, but are you aware that they may also be having an impact on other areas of your site that are <em>not<\/em> hidden behind a password?<\/p>\n<p><strong>Today we\u2019re going to explore password-protected posts<\/strong>: How do you make one? How are they cached? How do special rules for password protection affect the rest of your site? And how does LiteSpeed Cache do it differently?<\/p>\n<h2>What is a password-protected post?<\/h2>\n<p>Just like it sounds, a password-protected post is a blog post that a visitor cannot access without a password. When a visitor (let\u2019s call him Fred) accesses <code>http:\/\/www.example.com\/password-protected-post<\/code> for the first time, he is shown a prompt. Fred must enter the correct password before he is shown the post\u2019s content.<\/p>\n<p>The password is hashed and stored as a cookie in Fred\u2019s browser, which means he only has to enter the password correctly once. From that point, and for the life of the cookie, any time Fred visits <code>http:\/\/www.example.com\/password-protected-post<\/code>, he will be taken directly to the post content, and not be prompted for the password.<\/p>\n<p>It\u2019s worth noting, by the way, that Fred could also enter an <em>incorrect<\/em> password, and it would be hashed and stored as a cookie in his browser. The contents of the post would, of course, not be shown to him in that case, but the cookie would still be saved in his browser.<\/p>\n<h2>How do you password-protect a post?<\/h2>\n<p><a href=\"https:\/\/lswp.store\/wp-content\/uploads\/2017\/12\/null-7.png\"><img decoding=\"async\" class=\"aligncenter wp-image-8764 size-large\" src=\"https:\/\/lswp.store\/wp-content\/uploads\/2017\/12\/null-7-1024x560.png\" alt=\"WordPress Wednesday Password Protection and Caching\" width=\"1024\" height=\"560\" srcset=\"https:\/\/lswp.store\/wp-content\/uploads\/2017\/12\/null-7-1024x560.png 1024w, https:\/\/lswp.store\/wp-content\/uploads\/2017\/12\/null-7-300x164.png 300w, https:\/\/lswp.store\/wp-content\/uploads\/2017\/12\/null-7-768x420.png 768w, https:\/\/lswp.store\/wp-content\/uploads\/2017\/12\/null-7-1320x722.png 1320w, https:\/\/lswp.store\/wp-content\/uploads\/2017\/12\/null-7-600x328.png 600w, https:\/\/lswp.store\/wp-content\/uploads\/2017\/12\/null-7.png 1470w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/p>\n<p>To create a password-protected post, first create a post, or edit an existing one. In the <strong>Edit Post<\/strong> screen, click <code>Edit<\/code> next to <strong>Visibility: Public<\/strong>. Select <code>Password Protected<\/code> and enter the password you wish to use. Here we\u2019ve chosen <code>supersecretpassword<\/code> but you may want to be more clever than that \ud83d\ude09<\/p>\n<p>Press <code>OK<\/code> to save your choice. Make any additional edits that you wish to the post, and press <code>Publish<\/code> (or <code>Update<\/code> if you\u2019re editing a post that has previously been published.)<\/p>\n<h2>Password Protection and Caching<\/h2>\n<p>What happens when a password-protected post is cached?<\/p>\n<p>First, it\u2019s important to point out that we\u2019re talking about the kind of caching that is accomplished through rewrite rules. We talk more about this in <a href=\"https:\/\/lswp.store\/2017\/07\/19\/wpw-litespeed-caching-101\/\">our LSCaching 101 post<\/a>, if you want to know more, but essentially, <strong>the most effective WordPress caching plugins use rewrite rules to manage the cache. <\/strong><\/p>\n<p>LiteSpeed Cache for WordPress is no exception. We, too, use rewrite rules, but they are not the only tools in our cache toolbox.<\/p>\n<p><img decoding=\"async\" class=\"alignnone\" src=\"https:\/\/lswp.store\/wp-content\/uploads\/2017\/12\/null-7.jpeg\" alt=\"WordPress Wednesday Password Protection and Caching\" width=\"672\" height=\"448\" \/><\/p>\n<p>If you remember our example with Fred, you\u2019ll notice that password-protected posts require special handling because the posts exist in two forms: the form that is just a prompt for the user to enter the password, and the form that is the full content of the post. We want the latter to show only if the user has entered the correct password, otherwise we will display the former.<\/p>\n<p>When you have a rewrite-rule-based cache, generally the version with the password prompt is cached, and the full content version is NOT, and which one gets displayed depends on the existence of the password cookie.<\/p>\n<p>Most cache plugins will use a rewrite rule to detect the password cookie. If it exists (meaning the user has entered a password) then the page is not served from cache. So far so good; this is exactly what we want to happen. Remember the cached version of the page is the one that only displays the password prompt. Once the valid password is entered, we want the uncached version so that the full content is shown.<\/p>\n<h2>Where does this go wrong?<\/h2>\n<p>Rewrite rules for caching purposes do not single out individual WordPress posts. This means that when you are using a rewrite rule to determine if the password cookie exists, that rewrite rule applies to all of the pages on the site.<\/p>\n<p>Let that sink in for a minute.<\/p>\n<p>Put another way: Using rewrite rules, if one page is password-protected and the cookie is detected, <em>the entire site will be treated like that password-protected page<\/em> (i.e. not cached) <em>for that viewer for the entire life of the cookie<\/em>. Ouch.<\/p>\n<p>For example: Fred visits your one password-protected post, and enters a password (maybe even an incorrect one). The cookie is stored in his browser, the rewrite rule detects the cookie, and serves the uncached contents of the password-protected post to him. After that, Fred moves on to explore the other parts of your site, but he still has that password cookie stored, and the rewrite rule detects it no matter what pages he visits. As such, none of the pages of your site will ever be served to Fred from the cache until he manually clears that cookie from his browser.<\/p>\n<p>This is clearly not ideal.<\/p>\n<h2>How is LiteSpeed better?<\/h2>\n<p>LiteSpeed avoids this problem altogether. If you recall, LiteSpeed Cache for WordPress has a special relationship with your LiteSpeed Web Server. Because of this relationship, we can use HTML request headers to look for the password cookie, and <em>we can do it on an as-needed basis<\/em>. This means, LSCWP is not bound by rewrite rules like other plugins.<\/p>\n<p>If the password cookie exists, we only care about it in reference to the password-protected pages. Instead of relying on rewrite rules to detect the password cookie, LSCache relies on response headers to let it know when it is appropriate to even <em>look<\/em> for a password cookie. If the plugin doesn\u2019t request a password cookie for that particular post, then LSCache doesn\u2019t bother to detect one. Because of this, all of the non-password-protected posts in the site can be served to that visitor from the cache, as they should be.<\/p>\n<h2>In conclusion<\/h2>\n<p>You can see how setting a password on a post can have surprising and unintended consequences for the rest of your site! If you\u2019re using a plugin that relies entirely on rewrite rules to define its caching behavior, then your only recourse is to instruct your visitors to erase their cookies once in a while.<\/p>\n<p>Or, you can <a href=\"https:\/\/wordpress.org\/plugins\/litespeed-cache\/\">switch to LiteSpeed Cache<\/a>. (Come on over to our side! We\u2019d love to have you!)<\/p>\n<p>&#8212;<\/p>\n<p>Have some of your own ideas for future <a href=\"http:\/\/blog.litespeedtech.com\/tag\/wordpress-wednesday\">WordPress Wednesday<\/a> topics? Leave us a comment!<\/p>\n<p>Don\u2019t forget to meet us back here next week for the next installment. In the meantime, here are a few other things you can do:<\/p>\n<ul>\n<li>Subscribe to the <a href=\"http:\/\/blog.litespeedtech.com\/tag\/wordpress-wednesday\/feed\/\">WordPress Wednesday RSS feed<\/a><\/li>\n<li>Download <a href=\"https:\/\/wordpress.org\/plugins\/litespeed-cache\/\">LiteSpeed Cache for WordPress plugin<\/a><\/li>\n<li>Learn more about the plugin <a href=\"https:\/\/www.litespeedtech.com\/products\/cache-plugins\/wordpress-acceleration\">on our website<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Password protection and page caching have an odd relationship. Learn all about it, and how LiteSpeed Cache works to keep it from becoming a problem.<\/p>\n","protected":false},"author":1,"featured_media":8762,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[28],"tags":[316,317],"class_list":["post-8760","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cache","tag-wordpress","tag-wordpress-wednesday"],"jetpack_featured_media_url":"https:\/\/lswp.store\/wp-content\/uploads\/2017\/12\/null-6.jpeg","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/lswp.store\/index.php\/wp-json\/wp\/v2\/posts\/8760","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lswp.store\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lswp.store\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lswp.store\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lswp.store\/index.php\/wp-json\/wp\/v2\/comments?post=8760"}],"version-history":[{"count":0,"href":"https:\/\/lswp.store\/index.php\/wp-json\/wp\/v2\/posts\/8760\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lswp.store\/index.php\/wp-json\/wp\/v2\/media\/8762"}],"wp:attachment":[{"href":"https:\/\/lswp.store\/index.php\/wp-json\/wp\/v2\/media?parent=8760"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lswp.store\/index.php\/wp-json\/wp\/v2\/categories?post=8760"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lswp.store\/index.php\/wp-json\/wp\/v2\/tags?post=8760"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}