|
<? |
|
/* |
|
# WP Super Cache plugin to cache pages with different custom cookie |
|
|
|
This file must be in `wp-content/plugins/wp-super-cache-plugins` folder (depend on your choise at 1st step of this instruction) |
|
|
|
To make it work, do the following: |
|
|
|
1. Open `wp-content/wp-cache-config.php` and change `$wp_cache_plugins_dir` to this: |
|
|
|
`$wp_cache_plugins_dir = WP_CONTENT_DIR . "/plugins/wp-super-cache-plugins/";` |
|
|
|
In result, on Plugins page of WP Super Cache settings (/wp-admin/options-general.php?page=wpsupercache&tab=plugins) you will have only one plugin: this plugin, called `Cookie` |
|
|
|
2. Use `mod_rewrite` way to cache your pages (WordPress Dashboard -> Advanced -> Caching -> Use `mod_rewrite` to serve cache files.), save options and update your `.htaccess` file with generated rules |
|
|
|
3. Open `.htaccess` file and add cookie name in the end of each string that looks like this: |
|
|
|
`RewriteCond %{HTTP:Cookie} !^.*(comment_author_|wordpress_logged_in|wp-postpass_).*$` |
|
|
|
So you will have this: |
|
|
|
`RewriteCond %{HTTP:Cookie} !^.*(comment_author_|wordpress_logged_in|wp-postpass_|YOUR_COOKIE_NAME).*$` |
|
|
|
4. Change cookie name in `cache_my_page_with_set_cookie_action` function |
|
it's 2nd argument of `cache_my_page_with_set_cookie` function |
|
|
|
If you omit one of this steps, nothing will work. |
|
|
|
Developer documentation: https://odd.blog/wp-super-cache-developers/ |
|
*/ |
|
function cache_my_page_with_set_cookie_action($string) { |
|
// TODO: move this into plugin settings |
|
return cache_my_page_with_set_cookie($string, 'WEB_RUNR_CITY'); |
|
} |
|
|
|
add_cacheaction('wp_cache_get_cookies_values', 'cache_my_page_with_set_cookie_action'); |
|
function cache_my_page_with_set_cookie($string, $cookie_name) { |
|
if (isset($_COOKIE[$cookie_name])) { |
|
$string = $cookie_name . $_COOKIE[$cookie_name]; |
|
} |
|
return $string; |
|
} |
|
|
|
add_cacheaction('cache_admin_page', 'cache_my_page_with_set_cookie_admin_page'); |
|
function cache_my_page_with_set_cookie_admin_page() { |
|
?> |
|
<h4>Cookie</h4> |
|
<p><strong>Different cache snapshots for pages with different values of custom cookie.</strong></p> |
|
<p>You have to change this plugin and <code>.htaccess</code> to make it work and to change custom cookie name.</p> |
|
<p><em><strong>TODO:</strong> add input here to change cookie name and update <code>.htaccess</code> automatically.</em></p> |
|
<? |
|
} |