Dynamic Shortcodes plugin for WP Super Cache

The Dynamic Shortcodes plugin for WP Super Cache enables you to select shortcodes that should be processed dynamically on every page view. This can be useful if you have a shortcode that needs to display changing information, such as geo-targeted content, short timescale based content, or visitor specific content.

Download version 2016-06-02

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

To use the plugin, first extract the contents of the zip file into your Super Cache Plugins folder. You should not be using the Super Cache Plugins folder in its default location, see here for how to change it: Moving the WP Super Cache Plugins folder.

In your WordPress admin panel, go to the WP Super Cache settings page. Go to the advanced tab.

  • Under the Caching section, choose Use PHP to serve cache files.
  • Under the Advanced section, enable dynamic caching and also enable the Late init option.

Save the settings and go to the Plugins tab of the WP Super Cache Settings.

Scroll down to find the entry Dynamic Shortcodes. Here you will see a list of all shortcodes available on your website. Check the ones you want to be processed dynamically on every page load, then press the Update button.

Xoogu Dynamic Shortcodes settings on the Plugins page of the WP Super Cache settings

Any pages already cached will contain the non-dynamic shortcodes, so go to the Contents tab of the WP Super Cache Settings and delete the cache. Now all pages will be generated using the dynamic shortcodes.

Troubleshooting

The most likely reason for the plugin not working is if you forget to clear the cache after selecting the shortcode(s) to make dynamic and saving the settings. You may also need to clear your browser’s cache, depending on your settings, to ensure you’re looking at a freshly fetched version of the page.

You should also check that the shortcode works and is dynamic without page cache enabled, as the problem could be with the shortcode itself. However, there are some more technical issues I have come across that can cause the plugin not to work effectively:

wp_loaded action not triggered

If you are trying to make a shortcode dynamic, and that the function that handles that shortcode expects the wp_loaded action to have completed, then the shortcode may not work properly. The reason for this is that WP Super Cache hooks into the init action, which occurrs before the wp_loaded action. WP Super Cache returns the cached code and exits, so the wp_loaded action is never triggered.

So, if your shortcode requires that the wp_loaded action has been triggered, then you are best to manually trigger this action. Either edit the shortcode handler, or edit the processShortcodes function of this plugin, and add the following:

if (!did_action('wp_loaded')) {
    do_action('wp_loaded');
}

That will manually trigger the wp_loaded action if it has not already been triggered.

The shortcode relies on a dynamic action / filter handler

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 plugin, which you would then need to use alongside this plugin 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.

Use of output buffering

If the shortcode handler for the shortcode you are trying to make dynamic makes use of output buffering, then this plugin won’t work. You’ll either need to modify the code for the shortcode handler so it does not use any output buffering functions, or modify the code to make the output dynamic yourself. More on working with dynamic content that makes use of output buffering and is compatible with WP Super Cache can be found in this blog post: The shiny new dynamic content in WP Super Cache.

If you find anything with the plugin isn’t working, or any of the above isn’t clear, please let me know (leave a comment below or send me a message). If the issue is with a specific shortcode not working (or not appearing in the list to choose from), please let me know the name of (or a link to) the plugin that adds the shortcode as well as the shortcode name.

Changelog

2016-06-02

  • Fixed a bug where shortcode attributes with no value would be incorrectly processed.
  • 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-11

  • 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-03-27

  • Initial version
Posted on by xoogu, last updated

15 Responses to “Dynamic Shortcodes plugin for WP Super Cache”

  1. khan says:

    Hi, When i try the above mentioned method, the screen turns white and nothing else. Any idea why it happens like that?

    • xoogu says:

      Typically that means a Fatal PHP error.

      Can you try this:
      In your wp-config.php file, find the line define('WP_DEBUG', false); and change it to define('WP_DEBUG', true);
      Below that add the line define('WP_DEBUG_LOG', true);

      Then try and load the page again.

      Now look at the WordPress files in your wp-content folder, and you should see a file named debug.log. This should contain the information about the error that is causing the screen to go blank. You can either email a copy of the debug.log file to me (info@) or use something like pastebin.com and paste a link in a comment here, then I’ll take a look at it to see if I can identify the issue.

      (You’ll probably want to undo the changes to wp-config.php after the debug.log file has been generated, otherwise WordPress will continue to write details of all the errors that occur to that file).

      Dave

    • xoogu says:

      Hi Khan

      Thanks for the info and including the excerpt of the file, that makes it easy for me to see what the issue is.

      The problem is due to the theme using output buffering, which isn’t compatible with WP Super Cache and dynamic content (at least not without extra work – see this blog post: The shiny new dynamic content in WP Super Cache).

      If that is the only part of the theme that uses output buffering, then you can fix it quite easily. Replace
      ob_start();
      echo '<div class="woocommerce-message">Få din ordre tilsendt gratis hvis du bestiller for ' . ($maximum - $current) . ' kr. mere!</div>';
      $output = ob_get_clean();
      return $output;

      with

      return '<div class="woocommerce-message">Få din ordre tilsendt gratis hvis du bestiller for ' . ($maximum - $current) . ' kr. mere!</div>';

      After you’ve done that, try again. If you still get a blank screen, try deleting the debug.log file then edit wp-config.php and activate the debugging again. Try the page again, then look at the debug log and you’ll likely find it has a different error caused by use of an output buffer somewhere else in the theme.

      Again, feel free to send me a pastebin link if you need some help interpreting the error / fixing it.

      Dave

  2. khan says:

    Hi xoogu,

    I have debugged for errors and solved them.. But when i cleared cache, for the 1st time page load, the short-code is displaying random no. to the end of the code..

    ex: [test_code_3424nr][/test_code_3424nr]

    Once i reload it again, then the shorcode works perfectly as needed (dynamic)

    any idea / help regarding this please.

    • xoogu says:

      Ah, sounds like you fixed the main issue while I was writing my previous reply.

      The _3424nr bit added onto the shortcode is done by my plugin. WP Super Cache should re-process the page so that the [test_code_3424nr][/test_code_3424nr] appear in the cached file, but are processed when presented to the user. It sounds like WP Super Cache is not re-processing the page on the initial cache. I’m not sure what would cause that, so I’ll need to spend a bit of time testing to see if I can do anything to recreate the issue.

      • khan says:

        Xoogu,

        This is solved, i was adding the dynamic_output_buffer_test_safety after my function. I removed that, clear cache and now everything seems to be in place. Thank you so much for you prompt reply.

        How do i subscribe to this post to get mail notifications, so that if any one faces any issue i can see if i can contribute something.

        • xoogu says:

          Good to hear you got it working, and thanks for posting what the problem was. Makes it easier for me or anyone else coming across a similar problem in the future.

          I’m afraid at the moment I haven’t implemented any way of subscribing to comments. Maybe in the future I might add the Jetpack plugin to allow doing that. You might possibly be able to subscribe to comments at the moment by using a feed to email service with the comments feed for this page ( http://www.xoogu.com/dynamic-shortcodes-plugin-for-wp-super-cache/feed/ ), but I haven’t tried that.

          Kind regards

          Dave

  3. Hi, I have a problem here.
    I want to use dynamic shortcode plugin with your geo text wordpress plugin, but they are not working together.

    When I use geo text wordpress plugin alone, the plugin works correct, but when i tick the radio box for geotext and geocontent, the plugin doesn’t work.
    if these two plugin are not compitable, please suggest what plugin should i use instead of geo text wordpress plugin for geo targeting

    • xoogu says:

      Hi Abhishek

      They should both work together, unfortunately I’m currently away at the moment, and so can’t check what the problem is right now. I don’t get back until early December, so for now all I can suggest is to use the geo text plugin without any caching plugin. Sorry I can’t be of more help at the moment, I’ll update when I’ve got back and had a look at it.

      Dave

    • xoogu says:

      Hi Abhishek

      I’ve tried to recreate the problem, but everything is working okay for me. Did you make sure to clear the cache (both in WP Super Cache and also your browser cache) after making the shortcodes dynamic?

      What theme and what other plugins are you using? It could possibly be a compatibility issue with a certain theme or plugin.

      Dave

  4. Christian says:

    Thank you so much for this! You have no idea how badly I needed a plugin that does this!!

  5. Rahil says:

    Hey,

    Why don’t you make the source code available in GitHub? It would be easy to contribute.

    Thanks for the awesome work!

  6. Alex says:

    Thanks for awesome plugin first of all! But when i activated late init server get same critical load as there is no cache at all. I mean speed of loading of page same with cache and without when late init activated. Its only my problem?

Leave a Reply