I am having different buttons for different users in my site, i have used the below code to create those.

    global $user_login, $current_user;
    $user_info = get_userdata($current_user->ID);
    $roles = array (

    $roles_expired = array (
if (is_user_logged_in() && array_intersect( $roles, $user_info->roles)) {
echo Button 1; 
} else if (is_user_logged_in() && array_intersect( $roles_expired, $user_info->roles)) {

    echo 'Button2';
else {

  echo 'Button3';

The code is working for me, but when i enable caching(using w3-total-cache) in the website the button is also cached for different user roles also?

is there a better way to implement this ? Please help me

Thanks and Regards Anand RK

1 Answer 1


If this is on a specific page you'll be able to exclude the page from being cached under Performance > Page Cache > Advanced.

If it's on every page you'll have to enable fragment caching. Set the method to Disk: Basic and enable Late Initialization in Performance > Page Cache > Advanced. Set the following constant in your wp-config.php:

define( 'W3TC_DYNAMIC_SECURITY', md5( rand( 0, 999999 ) ) );

Then wrap any sections you don't want cached like so:

<!--mfunc <?php echo W3TC_DYNAMIC_SECURITY; ?> -->

<?php // your dynamic button code here ?>

<!--/mfunc <?php echo W3TC_DYNAMIC_SECURITY; ?> -->
  • Thanks a lot Chris, Is there any other method, beacuse, when i started using cloudflare, there also the same problem came,
    – Anand Rk
    Commented Aug 10, 2017 at 12:06
  • I know you can bypass the whole page with Cloudflare using Page Rules, but I'm not aware of a way to bypass them with dynamic fragments.
    – Chris Cox
    Commented Aug 10, 2017 at 12:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.