Our Biggest Sale Is Here
Up to 70% Off
Get the Deal

How to cache table data and update the cache automatically in wpDataTables

Say your price list is a CSV file with 20,000 rows, and the page with the table takes seconds to load, because wpDataTables reads the whole file on every visit. With caching on, wpDataTables saves the data in the database once and loads the table from there. If the file changes every day, a scheduled job refreshes the cache for you.

Which tables can use the cache?

Every table linked to a file or URL can use the cache: tables from CSV, Excel, XML, Google Spreadsheet, JSON, Nested JSON and serialized PHP array sources. Without the cache, these tables get slow above about 3,000 to 5,000 rows.

Don’t cache a PHP array table that returns different data for each user or URL. Every visitor would see the cached data.

How do I turn on the cache for a table?

Open the linked table in the editor, turn on Cache data from source in the Data source tab and click Save Changes.

  1. Open the table. Go to wpDataTables → wpDataTables and open the linked table.
  2. Turn on the cache. In the Data source tab, under Cache data, turn on Cache data from source. It is off by default.
  3. Save the table. Click Save Changes. wpDataTables saves the data in the database and loads the table from the cache from now on.

The cache now holds the data from the moment you saved. Changes in the source file don’t show until the cache is updated, so this is enough for files that rarely change.

The Data source tab of a linked CSV table in wpDataTables with the Cache data option Cache data from source
Caching a linked table: (1) the data source settings, (2) Cache data from source.

How do I update the cache automatically?

Turn on Enable for all non-server side tables under Auto update cache for tables in the settings, turn on Auto update cache in the table, and set up a cron job that calls the update URL on a schedule.

  1. Turn on auto update. Go to wpDataTables → Settings → Cache settings and, under Auto update cache for tables, turn on Enable for all non-server side tables. Click Save Changes. The tab now shows the cron instructions with your cache verify string.
  2. Turn it on in the table. Open the table, reload the page, and in the Data source tab turn on Auto update cache. It appears after you turn on Cache data from source. Click Save Changes.
  3. Schedule the update. Add a cron job on your server, or in a cron plugin, as described below.
The Cache settings tab in wpDataTables settings with Auto update cache for tables turned on and the instructions panel
The cache settings: (1) the Cache settings tab, (2) Enable for all non-server side tables, (3) the instructions, which continue with the cron commands and your verify string.

Auto update helps with APIs that limit the number of calls, such as the free Google Sheets API. With a 15-minute schedule, a table linked through the Google Sheets API calls Google only four times an hour.

How do I set up the cron job on my server?

Add a line to your server’s crontab that requests the update URL with your site address and your cache verify string. This line runs every 15 minutes:

*/15 * * * * wget -q -O - "https://your.website.url/wp-admin/admin-ajax.php?action=wdtable_update_cache&wdtable_cache_verify=your.cache.verify.string"

If your server has no wget, use curl:

*/15 * * * * curl -s "https://your.website.url/wp-admin/admin-ajax.php?action=wdtable_update_cache&wdtable_cache_verify=your.cache.verify.string" > /dev/null

Replace your.website.url with your site address and your.cache.verify.string with the string from wpDataTables → Settings → Cache settings. Keep the string private: anyone with it can start a cache update.

How do I schedule the update without server access?

Use the free WP Crontrol plugin to call the update URL from WordPress itself.

  1. Add a schedule. After you install WP Crontrol, add a schedule of 15 minutes in its Cron Schedules screen.
  2. Add the event. Go to Tools → Cron Events, click Add Cron Event and choose URL cron event.
  3. Fill in the details. Paste the update URL with your site address and verify string, set the next run a few minutes from now, and choose the 15-minute schedule as the recurrence.
  4. Save the event. Click Add Event.

WordPress runs its cron events when someone visits the site, so on a quiet site the update can start later than planned.

Why does the cache not update?

The cache update stops when the source no longer matches the table. Check the source for these problems:

Problem Fix
The number of columns changed Restore the columns, or create the table again.
Column names changed Restore the names, or create the table again.
The source is empty Add the data back to the file.
The file or URL no longer exists Put the file back under the same URL.
Reading the file or URL fails Open the URL in a browser and check the server’s error log.

You’ll find more troubleshooting steps at the bottom of the Cache settings tab.

What should I remember about table caching?

Turn on Cache data from source in a linked table to load it from the database instead of the source. To keep it fresh, turn on Enable for all non-server side tables in Cache settings and Auto update cache in the table, then schedule the update URL with a server cron job or WP Crontrol. The update stops if the source’s columns change or the file is missing.

Wait, Before You Download!

To get your hands on wpDataTables Lite, please enter your email address below. We’ll send you a direct download link and keep you updated on existing features along with helpful tips and tricks!

By continuing, I accept Privacy Policy and T&C
Mail Box