Dynamic Shortcodes extension for W3 Total Cache

The Dynamic Shortcodes Extension for W3 Total Cache enables you to choose certain shortcodes to keep dynamic, while the rest of the page is cached. This can be useful if you have shortcodes that display geo-localised content or time-based content e.g. eBay listings ending in the next 10 minutes, for example, but want to take advantage of W3TC’s page caching to speed up your website.

For an explanation of why caching can be problematic with dynamic content, and how dynamic caching / fragment caching (used by this extension) compares to other methods, please see this page: Differences between caching methods with Geo targeted content.

Install

Installing current version for W3TC 0.9.5 and later

Version 0.9.5 of W3 Total Cache significantly changed the way extensions are handled. Instead of a Extensions folder within the W3TC plugin directory, extensions are installed the same way as normal plugins. After doing this you also need to activate the plugin / extension. Note that activating the extension from the WordPress plugin page doesn’t activate the extension, it just makes it so that the extension will be able to register itself with W3TC. Actual activation of the extension is done via the W3TC extensions page (see Using the extension).

As of W3TC 0.9.5.1, there is a bug in its page cache module that prevents cached content being served whenever a page contains dynamic content. To fix this, open w3-total-cache/PgCache_ContentGrabber.php and find line 249:

			if ( $this->_set_extract_page_key( $mobile_group,

And change it to:

			if ( !$this->_set_extract_page_key( $mobile_group,

(i.e. add an exlamation mark)

Installing older version for W3TC 0.9.4.1 and earlier

To install the extension, unzip it, then upload the contents to the W3TC plugin’s extension directory ( typically wp-content/plugins/w3-total-cache/extensions/ ). Note that depending on your unzip software, it may unzip the files into a folder with the same name as the zip. If so, it is the file and folder inside that folder that need uploading to the extensions directory.

Extension installed incorrectly - folder (with same name as the zip file) containing the extension installed to W3TC extensions folder

Extension installed correctly - XooguDynamicShortcodes folder and XooguDynamicShortcodesAdmin.php file installed to W3TC extensions folder

Updating

Please deactivate the extension, install the new version, then reactivate the extension. You may want to clear the cache after activating the new version.

Using the extension

The extension requires several W3TC settings to work:

  • On the W3TC General Settings page, under the Page Cache section, Page cache method should be set to Disk: Basic.
  • On the Page Cache page, under the Advanced section, enable Late initialization.
  • If you want the user to see the dynamic output of a shortcode whenever they refresh / revisit the same page, then you need to disable browser caching. This is unlikely to be needed for most people, but if you do need this, then go to the Browser Settings page. Find the HTML & XML section, and enable Set cache control header. Set the Cache Control policy to no-cache ("max-age=0, private, no-store, no-cache, must-revalidate").

On the W3TC Extensions page, activate the Dynamic Shortcodes extension. Then click on the Settings link underneath it. This brings you to the extension settings page, where you can select which shortcodes you wish to make dynamic.

Dynamic Shortcodes extension for W3 Total Cache settings page

After you’ve saved the settings, you’ll need to clear the W3TC cache so that pages will be cached with the dynamic shortcodes.

FAQ

Shortcode is not listed in the extension settings

Some plugins don’t register the shortcode when you are viewing an admin page, hence the extension can’t find the shortcode to pick it up. In this case there is a field at the bottom of the shortcodes list, where you can manually type out the shortcode tag name(s) to make dynamic.

There could also be other reasons for a shortcode not showing up. If you find that some shortcodes are missing from the list, please let me know the theme / plugin that adds that shortcode, and the shortcode tag, and I’ll try to see why the shortcode isn’t being picked up by the extension.

The shortcode is not made dynamic or disappears

The first thing to check is that you’ve cleared the W3TC cache, and also your browser’s cache, to ensure you’re looking at a page where the dynamic shortcodes extension is active. Secondly, check that the shortcode works and is dynamic without W3TC page cache enabled, as the problem could be with the shortcode itself.

Some plugins provide shortcodes that also rely on actions to make the shortcode work, and these will not work properly with this extension. Generally this is plugins that make use of dynamic javascript, where the javascript is output separately to the processed shortcode. It should be possible to make a dynamic actions extension, which you would then need to use alongside this extension to make the shortcode fully dynamic. I’ve added this to my to-do list, but if you need this functionality, then please let me know.

If something doesn’t work or if you have any suggestions for improvements, please do let me know. You can leave a comment below, email me on info@, or send me an email via the contact form.

Changelog

2017-01-01

  • Extensive modifications to enable the extension to work with W3TC 0.9.5 and above.

2016-06-02

  • Fixed a bug on the admin page where adding a shortcode(s) by text input and checking / unchecking shortcodes from the list only saved the new manually input shortcode(s).
  • Fixed a bug where the plugin would try and process shortcodes for AJAX requests, which resulted in the shortcode not being processed correctly.

2016-01-10

  • If the extension can’t write to wp-config.php it will not activate and displays a (rather ugly) error message with the modification that needs to be made. Previously no error message would be shown and the extension would activate even when it couldn’t write to wp-config.php
  • Added the option to manually enter shortcodes if they’re not listed on the extension settings screen. The extension can only ‘see’ shortcodes that are available in the admin area. Some plugins don’t register their shortcodes for pages in the admin area, so these shortcodes would not be available to choose from. With this update you can now add these shortcodes manually.

2015-08-11

  • Previously the extension didn’t work if you had HTML minification enabled in W3TC. Now it does.

2015-04-11

  • Initial version
Posted on by xoogu, last updated

15 Responses to “Dynamic Shortcodes extension for W3 Total Cache”

  1. Adam R says:

    I have a hard time to get W3 Total Cache fragment caching to work. My issues are described here: https://wordpress.org/support/topic/fragment-cache-is-not-working?replies=1 and here: http://stackoverflow.com/questions/31125413/w3-total-cache-and-fragment-cache-not-able-get-it-to-work. Maybe you are be able to give proper directions how get it to work. Thanks

  2. Abdullah says:

    Hi,
    Dynamic Short codes extension is only W3-TC Pro version or can I use it with free version of w3-TC plugin.
    Please let me know.

    Thanks

  3. BUX says:

    HELLO M8!

    anyway you could update this to work with last version of W3TC?

    Cheers! 😀

  4. rs says:

    Hi Dave,
    Do you have plans to update your extension for W3TC .9.5.1?

  5. Calin says:

    Hello! Thank you for this great extension, I did all the steps but how to I integrate the short code in the php file where I want to display it?

    — ?

    Thank you!

    • xoogu says:

      Hi Calin

      If you’re coding the display of the shortcode in a file, you’d be better off just using mfunc tags directly. (All this extension does is provide a convenient way to automatically always wrap a shortcode within mfunc tags). I found this post that describes how to use mfunc tags: https://1fix.io/blog/2015/02/22/page-fragment-caching-w3-total-cache/

      So you’d just have something like:
      <!-- mfunc W3TC_DYNAMIC_SECURITY -->do_shortcode('[some_shortcode /]');<!-- /mfunc W3TC_DYNAMIC_SECURITY -->
      Or more preferably call the shortcode handler for the specific shortcode you want to display directly, e.g.:
      <!-- mfunc W3TC_DYNAMIC_SECURITY -->$GLOBALS['some_plugin']->process_some_shortcode('');<!-- /mfunc W3TC_DYNAMIC_SECURITY -->

      If you did still want to use this extension for some reason, you’d just include the shortcode for processing in the PHP the same way you would normally, e.g.
      do_shortcode('[some_shortcode /]');

      Dave

  6. Fadi says:

    Hi,

    Thank you for this great extension.
    one question – does this work for add_action( ‘init’, ‘setting_RQ_cookies’ );
    also, I mean can I add setting_RQ_cookies and make it dynamic as well?
    because one of the shortcodes I want to make dynamic, uses a global parameter found inside this action

    • xoogu says:

      Hi Fadi

      I’m not sure without testing it. Generally shortcodes that rely on an action won’t work with this extension because the cached page is loaded before most actions occur. However, the init action may happen before the late init cache retrieval kicks in. I would test it myself to check, but I’m afraid I’m just too busy with other work at the moment.

  7. Binaya says:

    Actually after adding this extention it works fine for the first time.
    but after that it unable to resolve the php code within the cached version of the file.

    For example:-
    if (!empty($GLOBALS[“shortcode_tags”][‘event’])) {echo call_user_func( $GLOBALS[“shortcode_tags”][‘event’], unserialize(stripslashes(‘s:0:\”\”;’)), ‘#_EVENTURL’, ‘event’ );}

  8. IOKIN PARDO ICHASO says:

    Hi, I’m exactly on the same situation:

    This is what I get from a shortcode [‘su_random’] that is supposed to be selected as “Shortcodes to make dynamic” through the Extension, once the page has been cached:

    if (!empty($GLOBALS[“shortcode_tags”][‘su_random’])) {echo call_user_func( $GLOBALS[“shortcode_tags”][‘su_random’], unserialize(stripslashes(‘s:0:\”\”;’)), ”, ‘su_random’ );}

    Did someone solved this issue?

    Thank you very much.

  9. Gilbert says:

    Hi there,
    I have the same problem… it’s work well the first time and after I have the error message “if (!empty($GLOBALS[“shortcode_tags”][‘su_random’])) {echo call_user_func(” etc;

    Really appreciate if someone has found an issue!
    Thanks

  10. Hello

    I get errors

    on all page

    “Warning: Invalid argument supplied for foreach() in www/wp-content/plugins/xoogu-dynamic-shortcodes-for-w3tc/lib/Xoogu/W3TC/DynamicShortcodes.php on line 66”

    and on extension page

    “Shortcodes to make dynamic

    Warning: array_flip() expects parameter 1 to be array, null given in /www/wp-content/plugins/xoogu-dynamic-shortcodes-for-w3tc/lib/Xoogu/W3TC/DynamicShortcodes_Admin.php on line 106

    Warning: array_merge(): Argument #2 is not an array in /www/wp-content/plugins/xoogu-dynamic-shortcodes-for-w3tc/lib/Xoogu/W3TC/DynamicShortcodes_Admin.php on line 106

    Warning: array_flip() expects parameter 1 to be array, null given in /www/wp-content/plugins/xoogu-dynamic-shortcodes-for-w3tc/lib/Xoogu/W3TC/DynamicShortcodes_Admin.php on line 106

    Warning: sort() expects parameter 1 to be array, null given in /www/wp-content/plugins/xoogu-dynamic-shortcodes-for-w3tc/lib/Xoogu/W3TC/DynamicShortcodes_Admin.php on line 107

    Warning: Invalid argument supplied for foreach() in /www/wp-content/plugins/xoogu-dynamic-shortcodes-for-w3tc/lib/Xoogu/W3TC/DynamicShortcodes_Admin.php on line 109”

Leave a Reply to Abdullah