How to set custom frequency for MPG data fetch
The MPG plugin allows users to control how frequently data updates are fetched. We have a "Live" option. However, this option refreshes data every 15 minutes and is not actually live because this was causing loading issues to the generated pages when data was fetched each time. By default, data is fetched every 15 minutes, but users can set custom intervals or trigger updates via a webhook for on-demand data fetching.
How to Change the Live Data Fetch Interval
To customize the interval for live data fetching, you can use the following filter:
add_filter('mpg_live_data_update_interval', function($time){ return 60; // Set the time in seconds. In this example, it's 60 seconds (1 minute). });
After adding this filter, you need to clear the existing transients (cached data) or wait for them to expire for the new interval to take effect. This ensures the updated time interval is applied correctly.
On-Demand Data Fetching with Webhooks
In addition to customizing the interval, MPG supports data fetching on demand through webhooks. The Sync frequency option in the plugin allows you to select On Demand mode, where you can trigger data updates via a Webhook URL (as seen in the screenshot below).
The webhook URL can be used to send a POST request to fetch data whenever needed, without waiting for scheduled intervals. This is particularly useful in scenarios where data updates are triggered by external actions or systems.
Real Use Cases for Webhooks:
- Form Submission: Trigger data updates when a user submits a form on your website, ensuring the most recent data is available.
- API Integration: Sync your MPG dataset with an external system or API that sends a webhook request each time data changes, ensuring real-time updates.
- Spreadsheet Updates: If you're using a Google Sheet or external spreadsheet as a data source, you can trigger data refreshes by configuring the webhook to be called whenever the sheet is updated.
Testing Webhooks Using Postman
To test your webhook, you can use Postman, a tool for API testing. Here’s how you can test the webhook:
- Download and install Postman from the official website.
- Open Postman and create a new POST request.
- Enter the Webhook URL from the MPG plugin as the request URL.
- Click Send to trigger the data fetch via the webhook.
Use this URL to send POST requests to the MPG plugin to trigger on-demand data fetching.
FAQs:
Q: Can I set the interval to less than 1 minute?
A: Yes, but setting it too low might increase server load. It is generally advisable to keep the interval at 1 minute or more, depending on your business needs.