Rarst / fragment-cache

WordPress plugin for partial and async caching.
Other
139 stars 9 forks source link

Skip cache examples for each object type #11

Closed jb510 closed 6 years ago

jb510 commented 6 years ago

It would be very helpful to have fc_skip_cache examples for each object type, not just widgets.

It's difficult to figure out how best to disable caching in each scenario. Heck, it's hard just to figure out what the object is named, let alone how to target it.

jb510 commented 6 years ago

FWIW, the key one for me is menus. I have no idea how to identify a menu by name/id to exclude it from caching (menu has a login/logout link that toggles).

I've run into this enough times I think the docs should list each object type and how to exclude an example of each by the most common identifier.

Rarst commented 6 years ago

Added more examples, see menu one https://github.com/Rarst/fragment-cache#menu

Please let me know if something it still unclear for your use case. :)

jb510 commented 6 years ago

Thank you for those! I feel I'm missing something obvious...

The menu I'm trying to exclude is actually in a custom menu widget (in the header widget area of a Genesis based theme). In the WP menu admin it's named "User menu".

add_filter( 'fc_skip_cache', function ( $skip, $type, $name, $args, $salt ) {

    if ( 'widget' == $type && is_a( $args['callback'][0], 'WP_Nav_Menu_Widget' ) )
        return true;

    if ( 'menu' === $type && isset( $args['name'] ) && 'User menu' === $args['name'] ) {
        return true;
    }

    return $skip;
}, 10, 5 );

Doesn't work, and I think they both "should" work... I've tried a few variations... Any idea why?

Maybe the cached object still exists from before and I need to manually delete it...

Rarst commented 6 years ago

Note that normally caches don't nest. So if you have menu inside a widget then only "outer" widget cache level applies.

However I am not entirely sure what happens if you cancel "outer" layer in such case. I will try to look into it tomorrow.

jb510 commented 6 years ago

I'm not sure what's going on... but nothing I seem to do let's that login/logout thingy in the menu, inside the widget work. Without the fragment cache plugin active it works... none of my attempts to disable in functions.php work (not by unset, not by filter)

Rarst commented 6 years ago

Ok, I think I screwed up with name example, try this:

add_action( 'fc_skip_cache', function ( $skip, $type, $name, $args ) {

    if ( 'widget' === $type && is_a( $args['callback'][0], 'WP_Nav_Menu_Widget' ) ) {

        return true;
    }

    if ( 'menu' === $type && isset( $args['menu'] ) ) {

        if ( 'User Menu' === $args['menu'] ) {
            return true;
        }

        if ( is_a( $args['menu'], 'WP_Term' ) && 'User Menu' === $args['menu']->name ) {
            return true;
        }
    }

    return $skip;
}, 10, 5 );
jb510 commented 6 years ago

@Rarst thank you very much! That worked. I'd completely forgotten menus were really term relationships under the hood.