The WPML language switcher caching functionality allows you to cache the language switcher output to improve the performance of your WordPress website. By caching the language switcher, you can reduce the number of database queries and improve the overall loading time of your site.
To implement this functionality, you can use the wpsnippets_wpml_language_switcher_cache()
function provided below. This function hooks into the icl_ls_languages
filter provided by the WPML plugin and caches the output of the language switcher using the Transients API.
function wpsnippets_wpml_language_switcher_cache( $languages ) {
$cache_key = 'wpml_language_switcher';
$cache_duration = 3600; // Cache duration in seconds (1 hour)
// Check if the cached output exists
$cached_output = get_transient( $cache_key );
// If the cached output exists, return it
if ( $cached_output !== false ) {
return $cached_output;
}
// If the cached output doesn't exist, generate it and cache it
ob_start();
do_action( 'icl_language_selector', array( 'skip_missing' => 0 ) );
$output = ob_get_clean();
// Cache the output
set_transient( $cache_key, $output, $cache_duration );
return $output;
}
add_filter( 'icl_ls_languages', 'wpsnippets_wpml_language_switcher_cache' );
To use this code snippet, you need to add it to your theme’s functions.php
file or create a custom plugin. Once added, the function will be hooked into the icl_ls_languages
filter provided by the WPML plugin.
The function first checks if the cached output exists using the Transients API. If the cached output exists, it returns the cached output. If the cached output doesn’t exist, it generates the language switcher output using the icl_language_selector
action provided by WPML, caches it using the Transients API, and returns the output.
By caching the language switcher output, you can improve the performance of your website by reducing the number of database queries required to generate the switcher. This is particularly useful for websites with a large number of languages or high traffic volumes.
Examples
Example 1: Implementing WPML language switcher caching using transients
This example demonstrates how to implement caching for the WPML language switcher using transients. The code snippet below shows how to cache the output of the icl_get_languages()
function for a specific duration using transients.
function wpsnippets_wpml_language_switcher() {
$cache_key = 'wpml_language_switcher';
$languages = get_transient( $cache_key );
if ( false === $languages ) {
$languages = icl_get_languages();
set_transient( $cache_key, $languages, HOUR_IN_SECONDS );
}
// Output the language switcher
// ...
}
In this example, we first check if the language switcher data is available in the cache using the get_transient()
function. If it’s not available, we retrieve the data using the icl_get_languages()
function and store it in the cache using the set_transient()
function. The language switcher is then outputted. This ensures that the language switcher data is cached and only refreshed after the specified duration.
Example 2: Clearing the WPML language switcher cache on language switch
This example demonstrates how to clear the WPML language switcher cache when the language is switched. The code snippet below shows how to use the icl_language_switched
action hook to clear the cache.
function wpsnippets_clear_wpml_language_switcher_cache() {
$cache_key = 'wpml_language_switcher';
delete_transient( $cache_key );
}
add_action( 'icl_language_switched', 'wpsnippets_clear_wpml_language_switcher_cache' );
In this example, we define a function wpsnippets_clear_wpml_language_switcher_cache()
that deletes the transient cache for the language switcher. We then hook this function to the icl_language_switched
action using add_action()
. Whenever the language is switched, the cache will be cleared, ensuring that the language switcher always displays the latest data.
Example 3: Excluding specific pages from the WPML language switcher cache
This example demonstrates how to exclude specific pages from being cached by the WPML language switcher. The code snippet below shows how to use the wpsnippets_wpml_language_switcher
filter to exclude specific pages from caching.
function wpsnippets_exclude_pages_from_wpml_language_switcher_cache( $languages ) {
$excluded_pages = array( 1, 2, 3 ); // IDs of excluded pages
if ( is_page( $excluded_pages ) ) {
delete_transient( 'wpml_language_switcher' );
}
return $languages;
}
add_filter( 'wpsnippets_wpml_language_switcher', 'wpsnippets_exclude_pages_from_wpml_language_switcher_cache' );
In this example, we define a function wpsnippets_exclude_pages_from_wpml_language_switcher_cache()
that checks if the current page is in the array of excluded pages. If it is, we delete the transient cache for the language switcher. We then hook this function to the wpsnippets_wpml_language_switcher
filter using add_filter()
. This allows us to exclude specific pages from being cached by the WPML language switcher, ensuring that the excluded pages always display the latest language switcher data.