Polylang and object caching
-
I have noticed an issue with Polylang and persistent object caching. I am using W3 Total Cache. The problem is with the default widgets, for example list of categories. When switching language you still see the previous languages until the cache expires. What would be the best solution to this? Turn off caching for those groups? One idea I had was to use wp_cache_switch_to_blog() to set a unique id for every language, so each language could have it’s own set of persistent object caching.
add_action('init', 'set_language_cache'); function set_language_cache(){ $current_language_slug = pll_current_language( 'slug' ); $default_language_slug = pll_default_language( 'slug' ); if ( $current_language_slug == $default_language_slug ) { return; } $languages = pll_the_languages(array('raw' => true)); foreach ( $languages as $language ){ if ( $language['slug'] == $current_language_slug ) { $current_language_id = $language['id']; break; } } if ( ! empty( $current_language_id ) ) { $blog_id = get_current_blog_id(); $new_blog_id = (int)($blog_id . '000' . $current_language_id); wp_cache_switch_to_blog( $new_blog_id ); } }
Something like this seems to work but not sure if it is a good solution, so would be interesting to hear how others solve this. Maybe other caching plugins got better solutions with Polylang support?
- The topic ‘Polylang and object caching’ is closed to new replies.