FlyingPress: How to generate separate cache for each logged in user
Published on August 29, 2024 4:27 pm
Last updated: August 30, 2024
For logged-in users FlyingPress generates same cached version for same roles . Which means all the users with the user role subscriber
will see same cached version for subscribers , all the customers
will see same cached version for customers and so on .
Note : Caching each logged-in user separately is not much beneficial and may result in a very high disk usage. That’s why FlyingPress does not recommend this
It’s ideal in the scenarios where the content of the page is not user specific and all the visitors get the same content.
But, if the content of the page relies on the logged-in user then it becomes a mess and all the logged-in users with same role see the same thing.
To overcome this issue I have written a code snippet which might be useful for many FlyingPress lovers.
We’ll be using two FlyingPress filters in order to achieve the expected outcome.flying_press_advanced_cache
and flying_press_cache_file_name
Step 1: Inject necessary code for changing logged-in cache file name in advanced-cache.php
<?php
// Filter advanced-cache.php content
add_filter('flying_press_advanced_cache', 'proloybhaduri_flying_press_advanced_cache_cb',10,1);
function proloybhaduri_flying_press_advanced_cache_cb($advanced_cache){
// This code will be injected to the advanced-cache.php file
$code = PHP_EOL.'$logged_in_cookie = array_values(array_filter($_COOKIE, function($key) {'. PHP_EOL.
'return strpos($key, \'wordpress_logged_in\') === 0;'. PHP_EOL.
'}, ARRAY_FILTER_USE_KEY))[0];'. PHP_EOL.
PHP_EOL.'$file_name .= isset($_COOKIE[\'fp_logged_in_roles\']) ? \'-\' . md5($logged_in_cookie) : \'\';'. PHP_EOL.PHP_EOL;
// Append the hash of the logged in cookie to the cache file name
$advanced_cache = str_replace('// File paths for cache files', $code.'// File paths for cache files', $advanced_cache);
return $advanced_cache;
}
Step 2: Change the cache file name when the user is logged in using WordPress logged in cookie
<?php
// Filter the cache file name
add_filter('flying_press_cache_file_name', 'proloybhaduri_flying_press_cache_file_name_cb',10,1);
function proloybhaduri_flying_press_cache_file_name_cb($file_name){
if( !is_user_logged_in() ){
return $file_name;
}
$logged_in_cookie = array_values(array_filter($_COOKIE, function($key) {
return strpos($key, 'wordpress_logged_in') === 0;
}, ARRAY_FILTER_USE_KEY))[0];
return $file_name.'-'.md5($logged_in_cookie);
}
Complete Code 👇
// Filter advanced-cache.php content
add_filter('flying_press_advanced_cache', 'proloybhaduri_flying_press_advanced_cache_cb',10,1);
function proloybhaduri_flying_press_advanced_cache_cb($advanced_cache){
// This code will be injected to the advanced-cache.php file
$code = PHP_EOL.'$logged_in_cookie = array_values(array_filter($_COOKIE, function($key) {'. PHP_EOL.
'return strpos($key, \'wordpress_logged_in\') === 0;'. PHP_EOL.
'}, ARRAY_FILTER_USE_KEY))[0];'. PHP_EOL.
PHP_EOL.'$file_name .= isset($_COOKIE[\'fp_logged_in_roles\']) ? \'-\' . md5($logged_in_cookie) : \'\';'. PHP_EOL.PHP_EOL;
// Append the hash of the logged in cookie to the cache file name
$advanced_cache = str_replace('// File paths for cache files', $code.'// File paths for cache files', $advanced_cache);
return $advanced_cache;
}
// Filter the cache file name
add_filter('flying_press_cache_file_name', 'proloybhaduri_flying_press_cache_file_name_cb',10,1);
function proloybhaduri_flying_press_cache_file_name_cb($file_name){
if( !is_user_logged_in() ){
return $file_name;
}
$logged_in_cookie = array_values(array_filter($_COOKIE, function($key) {
return strpos($key, 'wordpress_logged_in') === 0;
}, ARRAY_FILTER_USE_KEY))[0];
return $file_name.'-'.md5($logged_in_cookie);
}
Make sure to save settings in FlyingPress after you add this code.
This code will enable caching every logged-in user separately with FlyingPress when you enable Cache logged-in user without any conflict on membership sites , community sites and wherever the content highly depends on the user details. Cheers! ❤️