🔌Service Providers
Service providers are PressGang's hook for bootstrapping services after the framework has initialised. They run after Timber and the Loader are ready, making them the right place for wiring up filters, registering integrations, or initialising third-party libraries.
PressGang ships with one service provider by default — TimberServiceProvider — which registers context managers, Twig extensions, Twig environment options, and snippet template paths. The framework also provides the opt-in TemplateRoutingServiceProvider for convention-based Template Routing. And you can add your own alongside them.
How They Work
The Interface
Every service provider implements ServiceProviderInterface, which requires exactly one method:
namespace PressGang\ServiceProviders;
interface ServiceProviderInterface {
public function boot(): void;
}Default Configuration
TimberServiceProvider wires up:
Context managers
config/context-managers.php
timber/context
Twig extensions
config/twig-extensions.php
timber/twig
Twig environment options
config/timber.php
timber/twig/environment/options
Snippet template paths
vendor directory
timber/locations
See Context Managers and Twig Extensions for details on each.
Writing a Custom Service Provider
Adding a Provider via Filter
Plugins and mu-plugins can add providers without touching config files:
Guidelines
Service providers boot on every request. Keep boot() lightweight — register hooks and filters only, don't do real work.
Register hooks in
boot(), execute work in callbacks. Theboot()method should only calladd_action()/add_filter()— not perform queries, remote requests, or heavy computation.One concern per provider. If your provider handles both search customisation and email configuration, split it into two providers.
Guard for dependencies. If your provider depends on a plugin (ACF, WooCommerce, etc.), check
class_exists()orfunction_exists()before registering hooks.No constructor arguments. PressGang instantiates providers with
new $class(). UseConfig::get()or WordPress options for configuration.
Hooks
pressgang_service_providers
filter
Modify the list of service provider class strings before boot
Last updated