PHP Snippets Instead of Another Plugin
A PHP snippet fits a job that is one reversible hook, and a plugin fits one that stores data. Where to put the code and how to swap a plugin out safely.
A client asks for one small thing: shorter excerpts on the blog grid, a changed login logo link, a field removed from checkout. It’s tempting to search the plugin directory and install the first match, adding another dependency to maintain.
Sometimes a few lines of PHP are the better answer, and sometimes the plugin is. This post is for WordPress developers and small studios who build client sites, and it explains how to tell which is which, where the code should live, and how to swap an existing plugin out without a nasty surprise.
Use a PHP snippet instead of a plugin when the job is one hook with a reversible effect, nobody on the client side needs a settings screen, and the code is reviewed, kept in version control and has a named owner. Choose a plugin when the feature stores data, needs setup or cleanup, or gives the client a workflow.
- Check for a theme or block setting first. A snippet that repeats a setting you already have is extra code for nothing.
- Four common places can hold your PHP: a child theme, a regular site plugin, a must-use plugin and a dashboard snippet manager. They differ in what happens on a theme switch and in who can switch the code off.
- A must-use file has no admin deactivation switch and no regular plugin update notices, so write down how to remove it before you ship it.
- Replace a plugin on staging with the old one deactivated, and read its uninstall behaviour before you delete it.
- If “snippets” means Google rich results, you need structured data, not a snippet manager, and the FAQ rich result stopped showing on May 7, 2026.
Can a PHP snippet replace this plugin?
A PHP snippet can replace a plugin when the plugin does one narrow thing through an existing WordPress filter or action and leaves no data behind that you would have to migrate or clean up.
That covers more than people expect. Changing excerpt length, adjusting a login screen link, removing an unused admin menu item and adding a body class are each a callback on a hook. The WordPress reference describes add_filter() plainly: you register a function, it returns the filtered value, priority defaults to 10, and lower priorities run first. If a feature fits that sentence, you probably do not need a plugin from the directory.
Start with what the client actually needs, then work down this table.
| The need | Best home | Why |
|---|---|---|
| A look or layout change | Theme or block setting | No code to maintain. Check the Site Editor and theme options before anything else. |
| One hook, theme-specific | Child theme | It only matters while that theme is active, so it belongs with the theme. |
| One hook the client needs after a redesign | Site plugin | Survives a theme switch and has one on/off switch on the Plugins screen. |
| Behaviour that must always load | Must-use plugin | Deployment turns it on, and no one can untick it in wp-admin. |
| Custom post types, settings, cron, stored data | Real plugin | It needs an activation and uninstall story, and content has to stay portable. |
| A big feature with a vendor behind it (SEO, forms, commerce) | Maintained third-party plugin | You would be taking on a product, not a snippet. |
A single reversible hook can be your code, and anything with its own data or screens should be a plugin, yours or someone else’s.
If your theme already has a setting, use it. On a block theme such as CanvasWP, layout work happens in the block editor and Site Editor, so plenty of “can you tweak this” requests never need PHP at all.
The cost of a plugin is not the install. It is the update notices, the compatibility check on each WordPress release and the question of who owns it in two years. Our plugin audit covers how to measure that cost before you decide. Be careful with the claim that fewer plugins means a faster or safer site, though. Plugin count and line count alone do not tell you that, so measure the request you care about.
Where should code snippets in WordPress live?
Put theme-specific PHP in the active child theme’s functions.php, theme-independent features in a site plugin, and always-on code in a top-level must-use file.

WordPress loads a theme’s functions.php when that theme is active, and with a child theme the child’s file loads before the parent’s, with both executed. A regular plugin sits under wp-content/plugins/ and loads when it is active. Must-use plugins sit in wp-content/mu-plugins/ and load automatically, alphabetically, before regular plugins, and only top-level PHP files there are picked up by default. Files in subdirectories need a loader at the top level.
| Home | Survives a theme switch? | Who can switch it off | Rollback |
|---|---|---|---|
| Child theme | No | Anyone who can change the theme, or you via file access | Remove the lines or revert the commit |
| Site plugin | Yes | Users with plugin deactivation permissions on the Plugins screen | Deactivate it |
| Must-use plugin | Yes | No one in wp-admin | Remove the file through deployment or file access |
| Dashboard manager | Yes, if the manager stores snippets separately from the theme | Whoever can deactivate snippets | Toggle the snippet or use Safe Mode if available |
Pick a site plugin by default, because it gives the next maintainer a visible switch and survives redesigns.
Be honest about the must-use option. According to the must-use plugins documentation, these files get no admin deactivation switch and no regular update notifications, and moving a plugin into that folder skips its activation hook. They do not become regular plugins with a better workflow. Disabling them requires deployment or file access, which is what you want for a security tweak and not what you want for something a client should be able to turn off.
What does a one-hook site plugin look like?
A one-hook site plugin is a single PHP file with a plugin header and one add_filter() call, saved under wp-content/plugins/ and activated like any other plugin.
Here is a worked example. Say the blog grid uses the standard PHP excerpt and the client wants to change the default limit of 55 words in English to 35. Save this as wp-content/plugins/client-excerpt-length/client-excerpt-length.php on staging:
<?php
/**
* Plugin Name: Client Excerpt Length
* Version: 1.0.0
* Update URI: https://example.com/client-excerpt-length/
*/
add_filter( 'excerpt_length', static function ( $length ) {
return 35;
} );
A regular plugin’s main file needs the Plugin Name header. Version and Update URI are optional, and the header reference lists the rest. Use a URI you control for a real deployment. The callback runs at the default priority of 10. If you see a different length, check for other filters and how the grid renders excerpts.
Now test the two cases that behave differently:
- A post with enough excerptable text and an empty excerpt field. The generated excerpt should now trim to 35 words in English.
- A post with a hand-written excerpt. It will not change, because a supplied manual excerpt skips the generated branch.
That second result can look like a bug. The filter does not touch manual excerpts.
There is a third case. If the grid uses the Excerpt block, the global filter does not control it. On WordPress 7.1.5 the Excerpt block renderer sets its own limit and trims with the block’s excerptLength attribute, which defaults to 55 and has a documented editor maximum of 100. For that layout, change the block’s Excerpt length setting in the editor. The markup in a template that supplies post context looks like this:
<!-- wp:post-excerpt {"excerptLength":35} /-->
This is why you check for a setting first. On a grid that uses the Excerpt block, the plugin above would leave the grid’s excerpts unchanged. If the snippet goes into a child theme or an editor that supplies its own PHP tag, paste only the add_filter() call. To roll back, deactivate the plugin or remove the lines. If it must be always on, move the file to the top level of mu-plugins after deactivating the regular plugin. Removing the must-use file is the rollback.
When is a WordPress code snippets plugin useful?
A snippet plugin is useful when authorised maintainers need to add, disable and export small pieces of PHP from wp-admin without touching files or a deploy pipeline.
Code Snippets is one option. Its 4.1.1 release requires WordPress 5.5 or later and PHP 7.4 or later, and its developer lists WordPress 7.1.5 as tested. The free plugin supports PHP and HTML content snippets, a Safe Mode, exports including Export to PHP, and an optional file-based execution mode. Dedicated CSS and JavaScript snippet types are Pro features, and so are revisions and flat-file storage meant for version control.
Three things to check before using any manager on a client site:
- Where the code lives. Code Snippets stores snippets in the database by default, separately from the theme, so they are outside Git unless you put a copy there. File-based execution generates executable files, which is not the same as having your code in version control.
- Who can edit. A snippet manager is a PHP editor in the dashboard. Anyone who can edit active PHP snippets can run code on the site, so tie it to your security checklist.
- What Safe Mode covers. It helps you recover, but do not assume it catches every failure. Keep a way in through files.
Read the changelog of anything that executes code. Version 4.1.1 fixed a case where snippets you had deactivated or deleted could run again after file-based execution was switched back on.
My view: for a studio that already ships through Git, a site plugin is easier to review than a dashboard snippet. For a small team with no pipeline, a manager is a fair trade as long as you export regularly and keep editing rights to one or two people.
How do you replace a plugin and keep the snippet stack small?
Replace a plugin on staging first: record what it outputs and stores, switch it off, confirm your code does the same job, and only then change production with a rollback ready.
This is the checklist we would run, in the same flow as our staging and deploy guide:
- Inventory. List the plugin’s front-end output, its settings screens, any shortcodes or blocks used in content, and anything it saves in the database.
- Back up. Files and database, on staging and production.
- Check the platform. WordPress 7.0 raised the core PHP minimum from 7.2.24 to 7.4, and 7.1 keeps it, so confirm the host’s PHP version and avoid syntax or dependencies it cannot run. WordPress 7.1.5 was the latest stable release on December 6, 2026, while 7.2 was still a release candidate, so test against the stable one.
- Deactivate the old plugin, then activate your code. Test with the original off, not with both running.
- Compare. Check the same pages in logged-in and logged-out states, plus any content that used the plugin’s shortcodes.
- Measure if speed is the goal. Time the affected request before and after. Do not promise a gain from removing a plugin without a measurement.
- Read the uninstall behaviour. WordPress separates deactivation (temporary cleanup) from uninstall (permanent data removal), as the activation and deactivation hooks guide explains. Deleting the old plugin may delete its data.
- Keep the rollback. Leave the old plugin installed but inactive until the new code has run in production without trouble.
To keep the stack small afterwards, every snippet gets a one-line record: owner, purpose, dependencies, how to test, how to remove. Keep that next to the code in version control. For a must-use loader, write down the removal route (deploy, SFTP or the host’s file manager), because wp-admin will not offer one. The client handover post covers who can change executable code and how the next person disables a bad change.
If you cannot fill in that record for a snippet, it should be a plugin with a proper owner, or it should not exist.
Move a feature to a plugin when it creeps past one hook. Custom post types are the classic case: WordPress recommends registering them in a plugin so content stays portable across themes, with register_post_type() on init and an identifier of 20 characters or fewer. If only the active theme registers the post type in functions.php, its admin screen disappears when you switch themes.
Do you need a plugin for Google rich results?
You do not need a snippet manager for Google rich results. You need valid structured data for a supported feature that matches visible content, and Google decides whether to show it.
If you searched for Google rich snippets in WordPress or the best rich snippet plugin, you may be looking for rich results. Google’s structured data guidelines recommend JSON-LD and also accept Microdata and RDFa. Use the required properties for the exact feature, mark up what is visible on the page, and test with the Rich Results Test. Eligibility does not guarantee display.
The search gallery lists features such as Article, Breadcrumb, Product and Recipe, each with its own documentation. So choose by the feature, not by the plugin name.
Before installing anything, find out what already emits markup. Yoast SEO 29.0 includes Schema.org output and lists WordPress 7.0 or later and PHP 7.4 or later as requirements. If it owns your schema graph, test the rendered page, then extend it with its documented wpseo_schema_graph_pieces filter rather than adding a second plugin that prints competing markup.
To compare rich snippet plugins, check four things: the exact schema type, how it maps to your real content, its WordPress and PHP requirements, and the validated output on a representative page. And drop one old habit. Google stopped showing the FAQ rich result from May 7, 2026, so FAQ markup alone cannot earn that appearance anymore.
Your first step this week
Pick one plugin on a client site that does a single small job. Open it on staging and write down its output, its stored data and its uninstall behaviour. If that comes to one hook and no data, write the replacement as a site plugin like the excerpt example, test it with the original off, and commit it with its owner and removal note.
You will have a reviewed file in Git and a record the next developer can read. Once you remove the old plugin, you will have one fewer third-party dependency to update. If the answer comes out as “this stores data”, keep the plugin. That is also a result.
Frequently Asked Questions
Can I use a PHP snippet instead of a WordPress plugin?
Yes, for a narrow change made through an existing filter or action. Put it in the active child theme if it is theme-specific, or in a site plugin if it must survive a theme switch. Keep plugins for features with data, setup or client-facing screens.
How do I replace plugin functionality with a PHP snippet?
On staging, note the plugin’s output, settings and stored data, deactivate it, and test your code with the original off. Read its uninstall behaviour before deleting it, and keep it installed but inactive as a rollback until production has run cleanly.
What is a minimal PHP snippet stack on client sites?
A short list of reviewed, version-controlled snippets, each with an owner, purpose, dependencies, a test and a removal method. If a snippet grows past one hook or starts storing data, it graduates to a proper plugin.
Is a custom PHP snippet better than installing a plugin?
It is better when the job is one reversible hook that you are happy to own. A maintained plugin is better when it needs setup, cleanup, a settings screen for the client or portable content such as custom post types.
How do I add code snippets in WordPress?
Add theme-specific PHP to a child theme’s functions.php, use an activated site plugin for theme-independent features, or put a top-level file in mu-plugins when deployment should always load it. A snippet manager plugin is the dashboard alternative.
Does the free Code Snippets plugin include CSS and JavaScript snippets?
No. The free version supports PHP and HTML content snippets, while dedicated CSS and JavaScript snippet types are Pro features. It does include Safe Mode and exports, including Export to PHP.
Which rich snippet plugin is best for WordPress?
There is no single best one. Choose by the Google feature you need, such as Article, Product or Recipe, and by what already outputs markup on your pages. If Yoast owns your schema, extend it before adding another source, and validate with the Rich Results Test.

