Code snippets are small pieces of code you can add to your WordPress website. They’re a quick way to enhance features or apply fixes without needing to install a separate plugin.
Think of them as tiny, custom improvements for your site.
Code snippets
- Lazy load YouTube videos.
- Remove comment author link.
- Hide tags with less than 10 posts.
- Disable SRCSET for images.
- Disable /feed/ link for post comments.
- Disable emojis π
- After content CTA
- Google Analytics
- Google Adsense
- Limit post revisions
- Remove language switch from login page.
How to use Code Snippets in WordPress (video tutorial)
Watch tutorial on YouTube.
Where to add code snippets?
Code snippets can be added in 2 ways:
- Add code snippets to
/wp-content/themes/YOUR_THEME/functions.php
file. - Add code snippets using specific plugin.
I prefer adding code snippets using a plugin.
Because in the plugin you can:
- Switch any code snippet on/off.
- Prevent code snippet from running in case of PHP error.
- Code snippets will work even when you update or switch theme.
These are 3 main reasons why I choose using plugin for managing code snippets.
How to add code snippets?
There are many plugins that offer using code snippets. I prefer using free version of βCode snippetsβ plugin. It is simple to use and not aggressively promoting PRO features.
After installing plugin, you will see menu in WordPress admin. There you can see list of your existing snippets and add new ones.
- Navigate to “Snippets” page from admin sidebar in WordPress. Page will list existing snippets on your website.
- Click onΒ “Add new” button at the top.
- On opened page add title and paste your code snippet to “Code” field.
- Select where that code snippet should run. In most cases run everywhere or only on front-end.
- You can also set execution “Priority” there. Lower priority means code will run earlier.
- Then provide description for code snippet. It can be instructions or more info about code.
- Add tags for grouping code snippets.
- At the bottom you click on “Save changes and activate” or just “Save changes” button.
How to activate/deactivate code snippet?
You can easily activate/deactivate code snippets on “Snippets” β “All snippets” page with a switch button. This feature works similar to activate feature of plugins in WordPress.
If you manage multiple websites then you can export and import multiple snippets from one website to another.
Why use code snippets instead of separate plugins?
- Plugins need to be updated to latest version of WordPress. If not updated repository will show warning message in plugin repository.
- Plugin may add aggressive marketing links, banners when they get popular.
- Some plugins include multiple features while you may need only single feature to solve problem.
Here is the old plugin warning message. When plugin is not updated for a long time WordPress starts to show warning message.
Some security plugins also will show warning inside admin area suggesting to remove old plugins.
—
What is next:
- Optimize PageSpeed score.
- Automatically Block External Requests that slows down your WordPress website.
- Check how I reduced number of plugins using different solutions including code snippets.
- Native share button also can be added as code snippet.