Snippets
Snippets are PressGang's answer to the WordPress functions.php junk drawer. Instead of dumping unrelated functionality into a single file — analytics scripts next to image size definitions next to admin tweaks — each concern gets its own class, with its own configuration, that can be enabled or disabled with a single line.
Think of snippets as your ship's provisions — pre-packaged, self-contained, ready to be loaded aboard any theme.
Why Snippets Instead of functions.php
functions.phpIn a typical WordPress theme, functions.php grows into a sprawling file that mixes unrelated concerns: analytics tracking, custom image sizes, admin tweaks, WooCommerce overrides, Customizer settings. This creates problems:
Hard to find things. Where's the code that disables emojis? Somewhere in 800 lines.
Hard to reuse. Want the same analytics setup on another site? Copy-paste and hope you got everything.
Hard to disable. Commenting out blocks of code is error-prone and messy.
Hard to share. Distributing a
functions.phpsnippet via Composer isn't practical.
Snippets solve all of these:
functions.php approach
Snippet approach
One file, many concerns
One class per concern
Enable/disable by commenting code
Enable/disable by adding/removing a config line
Copy-paste between projects
Install via Composer, share across all themes
Arguments buried in code
Configuration passed explicitly via $args
No standard structure
Every snippet implements the same interface
Grows without limit
Each snippet stays small and focused
How Snippets Work
The Interface
Every snippet implements SnippetInterface, which requires exactly one thing — a constructor that accepts an array of arguments:
The constructor is where the snippet registers its WordPress hooks. Once constructed, the snippet is fully operational — no additional calls needed.
The Config File
Snippets are activated in your theme's config/snippets.php. Each entry maps a snippet class to its arguments:
To disable a snippet, remove or comment out its line. To reconfigure one, change its $args array. No code changes needed.
Namespace Resolution
PressGang resolves snippet class names in this order:
Fully qualified — if the name starts with
PressGang\or your child theme namespace, it's used directly.Child theme first —
YourTheme\Snippets\SnippetNameis checked, allowing you to override a library snippet with your own version.Parent fallback —
PressGang\Snippets\SnippetNameis used if no child theme override exists.
This means you can reference snippets by short name when they live in a standard namespace:
Template Paths
PressGang automatically adds the pressgang-snippets vendor views directory to Timber's template lookup paths. Twig templates bundled with snippet packages are available to your theme without any manual path configuration.
Anatomy of a Snippet
Here's what a typical snippet looks like — this one adds Google Analytics tracking via the WordPress Customizer:
Key things to notice:
Constructor registers hooks —
customize_registerfor the Customizer UI,wp_headfor the script output.No work in the constructor itself — it only wires up hooks for WordPress to call later.
Renders via Timber — output goes through a Twig template, not inline
echostatements.Guards its output — checks that a tracking ID exists before rendering anything.
Writing Your Own Snippets
Child themes will often need site-specific snippets that don't belong in the shared library. This is expected and encouraged — it's far better to write a snippet class than to add code to functions.php.
Step 1: Create the Class
Place it in your child theme's src/Snippets/ directory:
Step 2: Register in Config
Because your child theme namespace is checked first, you only need the short class name.
Step 3: Add a Twig Template (if needed)
If your snippet renders output, place the template in your child theme's views/snippets/ directory. Render it via Timber::render('snippets/your-template.twig', $context).
Common Snippet Patterns
Customizer + Render
Adds a setting to the WordPress Customizer and renders output based on that setting. Used for third-party scripts, tracking pixels, and theme options that need a simple admin UI.
Examples: GoogleAnalytics, GoogleTagManager, FacebookPixel, Hotjar, GoogleRecaptcha
Hook Filtering
Modifies WordPress behaviour via actions and filters. No UI, no templates — just behavioural changes.
Examples: DisableEmojis, BigImageScaling, SearchExcludePostTypes, RemovePosts
Config-Driven Registration
Receives structured $args and registers WordPress resources. The args array shape mirrors WordPress API conventions.
Examples: ImageSizes, Permalinks, AddQueryVars
Admin Features
Adds functionality to the WordPress admin — row actions, admin notices, editor customisation. Always includes capability checks and nonce verification.
Examples: DuplicatePost, AdminLogo, TinyMceBlockFormats
Twig Function Registration
Registers a callable function into the Twig environment, making it available in templates as {{ function_name() }}.
Examples: Breadcrumb
Guidelines
Snippets are constructed during theme setup and their hooks fire on every request. Keep constructors lightweight — register hooks only, don't do real work.
One concern per snippet. If you're tempted to add unrelated functionality, create a second snippet.
Accept
array $argsand document what keys are supported. Provide sensible defaults.Guard for context. Don't assume you're on the frontend — snippets may fire on admin, AJAX, or CLI requests. Check
\is_admin(),\is_singular(), etc. where appropriate.Guard for dependencies. If a snippet depends on ACF or WooCommerce, check
function_exists()orclass_exists()before calling their APIs.Escape output. Use Twig auto-escaping for templates. Use
\esc_html(),\esc_attr(),\esc_url()for PHP output.Sanitise input. Always apply
sanitize_text_field(),\absint(), etc. to values from$_GET/$_POSTor Customizer settings.Use fully-qualified function calls. Write
\add_action(), notadd_action(), in namespaced code.
PressGang Snippets Library
A curated collection of 45+ ready-to-use snippets is available as a separate Composer package:
This includes snippets for Google Analytics, Tag Manager, Facebook Pixel, emoji removal, image sizes, breadcrumbs, Open Graph tags, JSON-LD schemas, WooCommerce tweaks, and more. See pressgang-wp/pressgang-snippets for the full list.
Last updated