Last active
August 12, 2025 08:17
-
-
Save cryptexvinci/8a3971f8ab29ce84c8932e9bf956a1e3 to your computer and use it in GitHub Desktop.
Exclude Ultimate Member pages from WP Rocket cache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Exclude Ultimate Member pages from WP Rocket cache | |
* Add this to your theme's functions.php file or create a custom plugin | |
*/ | |
/** | |
* To disable WP Rocket cache for Ultimate Member pages. | |
* | |
* @return void | |
*/ | |
function um_reject_wp_rocket_cache() { | |
// Check if Ultimate Member is active | |
if (!function_exists('um_get_core_page')) { | |
return; | |
} | |
// Check if WP Rocket is active | |
$active_plugins = get_option('active_plugins'); | |
if (!in_array('wp-rocket/wp-rocket.php', $active_plugins, true)) { | |
return; | |
} | |
// Get Ultimate Member page slugs | |
$um_pages = array(); | |
$members_page_url = um_get_core_page('members'); | |
$user_page_url = um_get_core_page('user'); | |
$account_page_url = um_get_core_page('account'); | |
// Extract slug from URL | |
if ($members_page_url) { | |
$slug = trim(parse_url($members_page_url, PHP_URL_PATH), '/'); | |
if (!empty($slug)) { | |
$um_pages[] = $slug; | |
} | |
} | |
if ($user_page_url) { | |
$slug = trim(parse_url($user_page_url, PHP_URL_PATH), '/'); | |
if (!empty($slug)) { | |
$um_pages[] = $slug; | |
} | |
} | |
if ($account_page_url) { | |
$slug = trim(parse_url($account_page_url, PHP_URL_PATH), '/'); | |
if (!empty($slug)) { | |
$um_pages[] = $slug; | |
} | |
} | |
// Get WP Rocket settings | |
$options = get_option('wp_rocket_settings'); | |
$updated = false; | |
// Add UM pages to URI exclusion | |
if (isset($options['cache_reject_uri']) && is_array($options['cache_reject_uri'])) { | |
foreach ($um_pages as $page_slug) { | |
if (!empty($page_slug) && !in_array('/' . $page_slug . '/', $options['cache_reject_uri'], true)) { | |
$options['cache_reject_uri'][] = '/' . $page_slug . '/'; | |
$updated = true; | |
} | |
} | |
// Add wildcard patterns for user profiles and dynamic content | |
$user_patterns = array( | |
'/user/*', | |
'/members/*', | |
'/account/*' | |
); | |
foreach ($user_patterns as $pattern) { | |
if (!in_array($pattern, $options['cache_reject_uri'], true)) { | |
$options['cache_reject_uri'][] = $pattern; | |
$updated = true; | |
} | |
} | |
} | |
// Add UM cookies to cache exclusion | |
if (isset($options['cache_reject_cookies']) && is_array($options['cache_reject_cookies'])) { | |
$um_cookies = array( | |
'um_user_login', | |
'wordpress_logged_in_', | |
'wp-settings-' | |
); | |
foreach ($um_cookies as $cookie) { | |
if (!in_array($cookie, $options['cache_reject_cookies'], true)) { | |
$options['cache_reject_cookies'][] = $cookie; | |
$updated = true; | |
} | |
} | |
} | |
// Update WP Rocket settings if changes were made | |
if ($updated) { | |
update_option('wp_rocket_settings', $options); | |
// Clear WP Rocket cache if function exists | |
if (function_exists('rocket_clean_domain')) { | |
rocket_clean_domain(); | |
} | |
} | |
} | |
// Run on admin init | |
add_action('admin_init', 'um_reject_wp_rocket_cache'); | |
// Also run when Ultimate Member is activated | |
add_action('activated_plugin', function($plugin) { | |
if (strpos($plugin, 'ultimate-member') !== false) { | |
um_reject_wp_rocket_cache(); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment