By default, WP-Optimize doesn’t support clearing the cache for individual users when their MyCred balance changes—it’s usually an all-or-nothing process, or it can target specific URLs. However, we might be able to work around this if each user on your site has their own unique profile URL.
If your users do have their own specific profile pages, we could potentially target and clear the cache just for those pages. This way, only the cache for the affected user gets cleared, without impacting others.
Thread Starter
ibro87
(@ibro87)
Hi there,
This is very interesting! My users do have their own specific profile pages. What should I do to make this work? Please let me know. Thanks again!
To flush the cache for specific users, you can modify the snippet you mentioned to target only the cache for the affected user’s profile page. Since each user has a unique profile URL, we can clear the cache for that specific URL instead of purging the entire cache. Here’s how you can modify the snippet:
add_filter('mycred_add_finished', 'check_for_balance_change_and_purge_user_cache', 1000, 3);
function check_for_balance_change_and_purge_user_cache($reply, $request, $mycred) {
if ($reply === true) {
$user_id = $request['user_id']; // Assuming user ID is passed in $request
$user_profile_url = get_author_posts_url($user_id); // Adjust this if the profile URL is different
WPO_Page_Cache::delete_single_post_cache_by_url($user_profile_url);
}
return $reply;
}
This approach will ensure that only the cache for the specific user who had their MyCred balance changed is purged, leaving other users’ cache intact. This should help maintain the performance of your site while ensuring that the correct data is displayed for the user whose balance has changed.
Note:
We’re exploring a code snippet that falls outside the typical scope of WP-Optimize’s core functionalities. While WP-Optimize is designed with clear and defined functions, this custom approach might help achieve your specific goal of flushing the cache for individual users based on their MyCred balance changes. Please be aware that, as this is a custom implementation, it may or may not work perfectly with your setup. We recommend testing it thoroughly on a staging site before applying it to your live environment.
Thread Starter
ibro87
(@ibro87)
Hey @wpmansour,
Thank you so much for the code provided. Just to confirm, this code will only purge the cache for specific user’s profile page when their MyCred balance is changed, right?
If so, is it possible to purge the cache for specific users on other pages as well without affecting other logged in or guest users?
The thing is, MyCred on-site notifications are sent across all the pages. In addition to that, I am also using OptinSpin plugin which allows users to win or lose MyCred points. This plugin is visible on every page and users are allowed to spin the wheel only once per day. If only profile page is purged. serving other cached pages would allow users to spin the wheel as many times as they want which is not how this plugin suppose to work.
Looking forward to your reply. Thanks again.
You’re right—the code provided will only purge the cache for the specific user’s profile page when their MyCred balance changes. However, extending this to purge other pages, like those involving the OptinSpin plugin, would require a more customized approach.
While WP-Optimize doesn’t natively support purging cache for multiple specific pages tied to a user, it might be possible with custom development. This is a bit outside the typical scope of WP-Optimize’s functionality, so I’d recommend consulting with a developer to explore this further if you need a tailored solution.
Thread Starter
ibro87
(@ibro87)
Hi @wpmansour,
Thanks for your confirmation. Since WP-Optimize natively supports user caching, it would make perfect sense to support user purging as well, not only on user profile page but all other pages as well.
Anyway, for now it would make sense for me to show OptinSpin plugin on user profile page only as this is the only page that can be purged when MyCred balance changes.
So, my user profile URL structure is like this: https://example.com/profile/1638/ where the number in the URL is my user ID (I’m using user IDs instead of username for security reasons).
Based on my user profile URL structure mentioned above, could you please let me know which snippet would be appropriate to use on my website so I can give it a try? Thanks again for your help!
Thank you for the details and for understanding the current limitations. Given the functionality available in WP-Optimize, I’ve adjusted the approach to target your specific user profile pages.
You can use the following snippet, which converts your profile URL into a post ID and then purges the cache for that specific post:
add_filter('mycred_add_finished', 'check_for_balance_change_and_purge_user_cache', 1000, 3);
function check_for_balance_change_and_purge_user_cache($reply, $request, $mycred) {
if ($reply === true) {
$user_id = $request['user_id']; // Assuming user ID is passed in $request
$user_profile_url = home_url('/profile/' . $user_id . '/');
$post_id = url_to_postid($user_profile_url); // Convert the URL to a post ID
if ($post_id) {
WPO_Page_Cache::delete_single_post_cache($post_id); // Purge the cache for that specific post ID
}
}
return $reply;
}
This snippet will ensure that the cache is purged specifically for the user’s profile page without affecting other users or pages. It should help keep your site performing well while displaying accurate data for users.
Please test this on a staging environment first to make sure it integrates smoothly with your setup.
Thread Starter
ibro87
(@ibro87)
Hey @wpmansour,
Thank you so much for this. I can confirm that user purging works perfectly fine for user profile pages when MyCred balance changes without affecting other logged in users or guests. Thanks again for your help!