2017 © Pedro Peláez
 

wordpress-plugin menu-cache

Easily cache rendered menus using the Transients API.

image

inpsyde/menu-cache

Easily cache rendered menus using the Transients API.

  • Tuesday, October 24, 2017
  • by inpsyde
  • Repository
  • 12 Watchers
  • 63 Stars
  • 1,802 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 8 Versions
  • 8 % Grown

The README.md

Inpsyde Menu Cache

Version Status Build Downloads License, (*1)

Easily cache rendered menus using the Transients API., (*2)

Introduction

The wp_nav_menu() function calls _wp_menu_item_classes_by_context(), which again, depending on the context, calls wp_get_object_terms(), which is not cached, multiple times. With lots of taxonomies, terms and menu items, this can lead to a fair amount of (totally redundant) database queries., (*3)

This plugin lets you cache rendered menus (assuming they don't have dynamic components) for re-use., (*4)

Installation

Install with Composer:, (*5)

$ composer require inpsyde/menu-cache

Requirements

This package requires PHP 5.4 or higher., (*6)

Usage

Once activated, the plugin caches all menus, by default for five minutes. The menus to be cached, as well as the expiration, can be customized by using the appropriate filter., (*7)

Filters

Need to customize anything? Just use the provided filters., (*8)

Please note: when you use the below class constants for the filters, make sure that the class is actually available. This can be as easy as guarding your customization with if ( class_exists( 'Inpsyde\MenuCache\MenuCache' ) )., (*9)

Inpsyde\MenuCache\MenuCache::FILTER_EXPIRATION

The Inpsyde\MenuCache\MenuCache::FILTER_EXPIRATION filter allows you to define the expiration for all cached menus. The default value is 300, which is 5 minutes., (*10)

Arguments:, (*11)

  • int $expiration: Expiration in seconds.

Usage Example:, (*12)

<?php

add_filter( \Inpsyde\MenuCache\MenuCache::FILTER_EXPIRATION, function () {

    // Cache menus for 10 minutes.
    return 600;
} );

Inpsyde\MenuCache\MenuCache::FILTER_KEY

The Inpsyde\MenuCache\MenuCache::FILTER_KEY filter allows you to customize the cache key on a per-menu basis. The default value is constructed using a predfined prefix and the MD5 hash of the serialized args object., (*13)

Arguments:, (*14)

  • string $key: Current key.
  • object $args: Menu args.

Usage Example:, (*15)

<?php

add_filter( \Inpsyde\MenuCache\MenuCache::FILTER_KEY, function ( $key, $args ) {

    // Construct the key based on the theme location only.
    return "cached_menu_{$args->theme_location}";
}, 10, 2 );

Inpsyde\MenuCache\MenuCache::FILTER_KEY_ARGUMENT

The Inpsyde\MenuCache\MenuCache::FILTER_KEY_ARGUMENT filter allows you to customize the menu argument name that is used to store the menu key (for later look-up)., (*16)

Arguments:, (*17)

  • string $key_argument: Current key argument name.

Usage Example:, (*18)

<?php

add_filter( \Inpsyde\MenuCache\MenuCache::FILTER_KEY_ARGUMENT, function () {

    // Use argument name with a leading underscore.
    return '_menu_key';
} );

Inpsyde\MenuCache\MenuCache::FILTER_SHOULD_CACHE_MENU

The Inpsyde\MenuCache\MenuCache::FILTER_SHOULD_CACHE_MENU filter allows you to customize caching on a per-menu basis., (*19)

Arguments:, (*20)

  • bool $key: Whether or not the menu should be cached.
  • object $args: Menu args.

Usage Example:, (*21)

<?php

add_filter( \Inpsyde\MenuCache\MenuCache::FILTER_SHOULD_CACHE_MENU, function ( $should_cache_menu, $args ) {

    // Cache all menus for a bunch of dynamically created theme locations.
    return 0 === strpos( $args->theme_location, 'some_prefix_here_' );
}, 10, 2 );

Inpsyde\MenuCache\MenuCache::FILTER_THEME_LOCATIONS

The Inpsyde\MenuCache\MenuCache::FILTER_THEME_LOCATIONS filter allows you to define theme locations to restrict caching menus to., (*22)

Arguments:, (*23)

  • string|string[] $theme_locations: One or more theme locations.

Usage Example:, (*24)

<?php

add_filter( \Inpsyde\MenuCache\MenuCache::FILTER_THEME_LOCATIONS, function () {

    // Cache the menus for the "primary" theme location only.
    return 'primary';
} );

License

Copyright (c) 2017 Inpsyde GmbH, (*25)

This code is licensed under the GPLv2+ License., (*26)

The Versions

24/10 2017

dev-master

9999999-dev

Easily cache rendered menus using the Transients API.

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

plugin wordpress wp cache menu transient nav-menu wp_nav_menu

24/10 2017

v1.4.1

1.4.1.0

Easily cache rendered menus using the Transients API.

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

plugin wordpress wp cache menu transient nav-menu wp_nav_menu

28/09 2017

v1.4.0

1.4.0.0

Easily cache rendered menus using the Transients API.

  Sources   Download

MIT

The Requires

 

The Development Requires

plugin wordpress wp cache menu transient nav-menu wp_nav_menu

11/07 2017

v1.3.0

1.3.0.0

Easily cache rendered menus using the Transients API.

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

plugin wordpress wp cache menu transient nav-menu wp_nav_menu

29/05 2017

v1.2.0

1.2.0.0

Easily cache rendered menus using the Transients API.

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

plugin wordpress wp cache menu transient nav-menu wp_nav_menu

19/12 2016

v1.1.0

1.1.0.0

Easily cache rendered menus using the Transients API.

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

plugin wordpress wp cache menu transient nav-menu wp_nav_menu

25/10 2016

v1.0.0

1.0.0.0

Easily cache rendered menus using the Transients API.

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

plugin wordpress wp cache menu transient nav-menu wp_nav_menu

24/10 2016

v1.0.0-alpha

1.0.0.0-alpha

Easily cache rendered menus using the Transients API.

  Sources   Download

MIT

The Requires

  • php >=5.4