Support » Plugin: W3 Total Cache » Using shortcodes with fragment caching

  • Resolved mashastudio

    (@mashastudio)


    I use a geolocate shotcode and comment it out this way:

    <?php if (!defined('W3TC_DYNAMIC_SECURITY')) { define('W3TC_DYNAMIC_SECURITY', md5(rand(0,999))); } ?>
    
    <!--mfunc <?php echo W3TC_DYNAMIC_SECURITY; ?>  -->
    echo do_shortcode('[wt_geotargeting get="city"]');  
    <!--/mfunc <?php echo W3TC_DYNAMIC_SECURITY; ?> -->

    Weirdly it doesn’t work on 10% of the pages on my site, it displays “echo do_shortcode('[wt_geotargeting get="city"]');” instead of the city name

    Do you have any idea?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Curious, why is your echo do_shortcode line outside of the <?php indicators?
    I am more surprised it doesn’t display your problem 100% of the time, instead of only 10%.

    Typically people have their fragment code inside the <!--mfunc <?php area.

    Plugin Contributor gidomanders

    (@gidomanders)

    Are you using this partial in multiple templates or template partials? The problem might be the way that part of code is added to the website. If it works fine in a page template but not in a partial which is added through another shortcode or get_template_part(), that might be the issue.

    Thank you for the reply @gidomanders . You are right, I’m using it in the header template. So, no way to fix that?

    • This reply was modified 1 year, 3 months ago by mashastudio.
    Plugin Contributor gidomanders

    (@gidomanders)

    @mashastudio Sorry for the late reply.
    You should never use a random string as dynamic security variable. You have to define the variable in your wp-config.php file. For security reasons, W3 Total Cache compares the string used in cached pages to the defined variable. If you randomise it, the comparison will fail and you’ll see the PHP code as normal text, like in your issue. The correct syntax is:

    in your wp-config.php:
    `
    define(‘W3TC_DYNAMIC_SECURITY’, ‘mycustomstring’);
    `

    in your template file:
    `
    <!–mfunc <?php echo W3TC_DYNAMIC_SECURITY; ?> –>
    echo do_shortcode(‘[wt_geotargeting get=”city”]’);
    <!–/mfunc <?php echo W3TC_DYNAMIC_SECURITY; ?> –>
    `

    You probably copied the code from someone I also found when using Google. He suggests to use rand(), which is not correct and causes issues.

    @fistfullofcrisco The code you want to run dynamically should never be between PHP-tags, otherwise it won’t be dynamic as the PHP code will compile before the page is cached, defeating the purpose of page fragment cache. Also your suggestion might be “typically used”, but we actually support multiple implementations. Check our wiki for more details: https://github.com/Auctollo/w3-total-cache/wiki/FAQ:-Developers#how-do-i-implement-page-fragment-caching

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Using shortcodes with fragment caching’ is closed to new replies.