Skip to main content

PDF for GravityView Hooks – gfpdf_gv_cache_ttl

Description​

Use this filter to change how long a cached PDF is served before it's regenerated. By default this matches GravityView's Entry Cache Duration: the View's own setting if it has one, otherwise GravityView's global setting (one day by default).

Edits to the entry, View, form or PDF settings already trigger a fresh PDF. The duration only matters for content that changes on its own, such as a merge tag that prints today's date. See Caching.

You can also shorten it for one View by lowering that View's Entry Cache Duration, but that also shortens how long GravityView caches the View's entries. Use this filter when you only want the PDFs to refresh sooner, or when the rule depends on something a setting can't check.

This filter only applies when the cache is on (see gfpdf_gv_enable_pdf_cache).

Version​

This hook was introduced in PDF for GravityView 2.0.

Arguments​

The following arguments are passed to functions that use this hook:

$ttl | int​

  • How long a cached PDF is served, in seconds.
  • Values below 1 are treated as 1.
  • The default value is the View's Entry Cache Duration, falling back to GravityView's global Entry Cache Duration (86400, one day, by default).

$view | \GV\View​

  • The View the PDF is being generated for.

Usage​

Refresh PDFs hourly on any View whose PDF header or footer prints the current date, without changing GravityView's entry cache:

add_filter( 'gfpdf_gv_cache_ttl', function( $ttl, $view ) {
$settings = [ 'gpdf_header', 'gpdf_footer', 'gpdf_multiple_header', 'gpdf_multiple_footer' ];

foreach ( $settings as $setting ) {
$html = (string) $view->settings->get( $setting, '' );

if ( strpos( $html, '{date_mdy}' ) !== false || strpos( $html, '{date_dmy}' ) !== false ) {
return min( $ttl, HOUR_IN_SECONDS );
}
}

return $ttl;
}, 10, 2 );

Source Code​

This filter is located in src/Pdf/Caching/Cache.php of the PDF for GravityView extension.