Registry
The Registry package provides a flexible, namespaced key-value store for sharing data across your WordPress application. It supports multiple named registries (using the Multiton pattern) and is useful for managing configuration, caching, or any shared state.
Last modified: June 2, 2025
Features
- Singleton and multiton registry instances
- Simple key-value storage and retrieval
- Namespaced registries for different contexts
Usage
Default Registry
use T2\Registry\Registry;
// Get the default registry instance
$default = Registry::get_instance();
$default->set('key', 'value');
print_r( $default->get_all() );
// Output: [ 'key' => 'value' ]
Named Registries
// Get a named registry instance (e.g., "config")
$config = Registry::get_instance('config');
$config->set('setting', 'on');
print_r( $config->get_all() );
// Output: [ 'setting' => 'on' ]
// Each registry is isolated:
print_r( $default->get_all() );
// Output: [ 'key' => 'value' ]
API
Registry::get_instance( $name = 'default' )
Returns a singleton instance of the registry for the given name.
set( $key, $value )
Stores a value by key.
get( $key, $default = null )
Retrieves a value by key, or returns the default if not set.
get_all()
Returns all key-value pairs in the registry.
has( $key )
Checks if a key exists in the registry.
remove( $key )
Removes a key from the registry.