Cache
Developer documentation for the Ajax Load More Cache add-on.
Auto-Generate Cache
The Ajax Load More cache can be auto-generated by populating the alm_cache_array filter with an array of cache URLs and IDs.
The alm_cache_array filter informs Ajax Load More of which cache instances should be auto-generated when the build is initiated.
function my_alm_cache_array(){
$array = array(
array(
'url' => 'http://website.com/blog/',
'id' => '7402199883'
),
array(
'url' => 'http://website.com/work/',
'id' => '2460844120',
'max' => 5
)
);
return $array;
}
add_filter( 'alm_cache_array', 'my_alm_cache_array' );
PHPParameters
The following array parameters are available for each cache item. Note: url and id are required fields.
url | The URL to the page holding the cache instance.Required |
---|---|
id | The unique cache_id of the ALM instance.Required |
max | The maximum number of cache pages to create per item. |
Before the cache is auto-generated, the current Ajax Load More cache is emptied – this means even cache instances not set to be auto-generated will be cleared.
Note: The Ajax Load More cache is not built on its own, it requires you to initiate the build process from the WordPress admin.
Building the Cache
To start the cache build process, you’ll need to visit the Ajax Load More Cache page in your WordPress admin and click the Generate Cache button in the sidebar.
Once initiated, you will be redirected to a new page (as shown above) where you can track the cache progress and status.
Note: If you exit the page while auto-generating the cache, the process will stop where you exited.
Filter Hooks
The following filter hooks are available when using this add-on:
alm_cache_created
The alm_cache_created action is triggered after a cache file has been created.
add_action( 'alm_cache_created', function(){
// Run a custom function here
} );
PHPalm_cache_deleted
The alm_cache_deleted action is triggered after an individual cache file or the entire ALM cache has been deleted.
add_filter( 'alm_cache_deleted', function(){
// Run a custom function here
} );
PHPalm_cache_path
The alm_cache_path filter can be used to update the absolute server path of the Ajax Load More Cache – this path is used when writing and saving files to your server.
The following snippet will adjust the default cache path to a /cache/ folder in the current theme directory.
add_filter( 'alm_cache_path', function(){
return get_stylesheet_directory() . '/cache/';
} );
PHPNOTE: When filtering the cache location you must update both alm_cache_path and alm_cache_url hooks.
alm_cache_url
The alm_cache_url filter can be used to update the URL path to the Ajax Load More Cache – this URL is used when rendering the cached content of an Ajax Load More query.
The following snippet will adjust the default cache URL to a /cache/ folder in the current theme directory.
add_filter( 'alm_cache_url', function(){
return get_stylesheet_directory_uri() . '/cache/';
} );
PHPNOTE: When filtering the cache URL you must update both alm_cache_path and alm_cache_url hooks.
alm_clear_cache
The alm_clear_cache action can be used by authenticated users to clear the full Ajax Load More cache or by a specific cache ID.
// Clear full cache.
do_action( 'alm_clear_cache' );
// Clear cache by specific cache ID.
do_action( 'alm_clear_cache', '<your_cache_id>' );
PHPDynamic Cache ID
In some instances, you will need to create the cache_id dynamically. In the case of taxonomy archives (category.php, tag.php) or when filtering by a querystring the cache_id should be generated on the fly to avoid issues with cache duplication.
Archives
The following example uses the current taxonomy and slug to create a unique cache_id for an archive template.
if ( is_archive() ) {
$obj = get_queried_object();
if ( isset( $obj->taxonomy ) && isset( $obj->slug ) ){
$taxonomy = $obj->taxonomy;
$taxonomy_term = $obj->slug;
echo do_shortcode( '[ajax_load_more archive="true" cache="true" cache_id="' . $taxonomy . '-'. $taxonomy_term .'"]' );
}
}
PHPQuerystring
The following example parses the URL querystring of a WooCommerce shop to create a unique cache_id for sorted products.
parse_str( $_SERVER['QUERY_STRING'], $qs ); // Parse Querystring
$cache_id = 'products';
if( $qs ) {
// Create new Cache ID
$sort = $qs['orderby'] ? $qs['orderby'] : false;
$cache_id = $sort ? $cache_id . '_' . $sort : $cache_id;
}
echo do_shortcode( '[ajax_load_more cache="true" cache_id="' . $cache_id . '" post_type="product"]' );
PHPTemplate Variables
The following template variables can be injected into a cache shortcode to dynamically set the cache_id.
%post_id% | The current post ID as a string. |
---|---|
%post_slug% | The current post slug as a string. |
/**
* https://example.com/my-page-slug/
*
* Output: cache-my-page-slug
*/
echo do_shortcode( '[ajax_load_more cache="true" cache_id="cache-%post_slug%" post_type="product"]' );
PHP