Rarst / fragment-cache

WordPress plugin for partial and async caching.
Other
140 stars 10 forks source link

Use `pre_wp_nav_menu` for the menu #8

Closed grappler closed 8 years ago

grappler commented 8 years ago

Problem: I am currently adding the language switcher in the menu using wp_nav_menu_items. When I activate this plugin logically the URL stops changing.

I was looking for a way I could hook in after the cache was fetched but I could not find a way.

WordPress 3.9 introduced a new filter pre_wp_nav_menu that can be used to short-circuit the menu process. https://generatewp.com/how-to-use-transients-to-speed-up-wordpress-menus/

If you used the new filter then I could hook into it after the cache was fetched and add the language switcher.

function wpzoo_language_switcher( $object, $args ) {
    if ( ! is_null( $object ) ) {
        $object .= lanaguage_switcher();
    }
}
add_filter( 'pre_wp_nav_menu', 'wpzoo_language_switcher',10, 2 );
Rarst commented 8 years ago

Fixed by https://github.com/Rarst/fragment-cache/commit/f03a27978cad646f2bed6d025933d24c4c2f15d1

Careful with filter since it will fire twice "inside" and "outside" the cache. Not yet sure if it can be handled smoothly with arbitrary amount of things possibly hooked, but it should be pretty rare use case.

grappler commented 8 years ago

Thanks! I have implemented the update on https://wpzoo.ch/en/