How to Use Code Snippet to Avoid Cluttered Plugins
When we want to add specific features to our WordPress site, we often find ourselves installing multiple plugins. Before we know it, we’ve added too many plugins, making the back end cluttered and hard to manage. Although installing plugins works, it can also leave your site messy and difficult to maintain.
Not every problem has to be solved by installing a plugin. Unless the feature you need is very complex and requires extensive options, a simple solution might only require a few lines of code. In such cases, you don’t need a full plugin; you just need to insert a code snippet.
A code snippet is a small piece of code that you can add to your WordPress site to introduce new functionality or modify existing behavior. While it can be added directly into the theme’s core files (e.g.,. in the functions.php file), this method has its drawbacks. If the theme developer releases an update and you apply it, the update can overwrite your changes, wiping out all the customizations and code snippets you’ve inserted.
Using a code snippet plugin to manage your custom code in WordPress
Instead of adding code directly to the theme’s files, use a code snippet plugin for better management. It allows you to keep all your code tweaks in one place, making it more efficient. Rather than having multiple plugins for different functionalities, you can manage them all within a single plugin.
Benefits keeping your custom codes in a code snippet plugin:
- Organization
All your custom code snippets are stored in one location, making it easier to manage and review. - Safety
Your custom code won’t be overwritten during theme updates, ensuring your site remains functional without extra rework. - Efficiency
Reduces the number of plugins needed on your site, which can improve performance and reduce potential conflicts. - Flexibility
Enables you to activate or deactivate individual snippets easily, without affecting others. - Error prevention and handling
The plugin won’t let you save if there is an error in the code. If you save it in functions.php, you will not be aware if there’s an error until you save it and check for issues.
Popular code snippet plugins
Code Snippets
A simple plugin that lets you add code snippets to your site. This is what I will use in this article as a practical example.
WPCode – Insert Headers and Footers + Custom Code Snippets
Allows you to add custom code in headers, footers, and beyond.
FluentSnippets
This plugin lets you add and manage custom code snippets (PHP, JavaScript, CSS, HTML) with an intuitive interface, and includes conditional logic to run certain code only under specific conditions.
Example use case: how to use Code Snippet plugin to exclude pages from search results
Sometimes, you may want your search results to focus solely on posts, for example, when running a recipe blog. It ensures that users find relevant content more easily without sifting through non-recipe pages.
Install and activate the Code Snippet plugin
- Go to your WordPress dashboard.
- Navigate to Plugins > Add New.
- In the search bar, type “Code Snippets“.
- Find the plugin named “Code Snippets” and click Install Now.
- After installation, click Activate.
Add a new snippet
- Once activated, you will see a new menu item called Snippets in your dashboard.
- Click on Snippets > Add New.
Insert your custom code snippet
- Give your snippet a title, such as “Exclude Pages from Search Results“.
- In the code editor, copy and paste the following code:
function my_custom_exclude_pages_from_search($query) {
if ($query->is_search && $query->is_main_query()) {
$query->set('post_type', 'post');
}
}
add_action('pre_get_posts', 'my_custom_exclude_pages_from_search');
(Note: The my_custom_ prefix is random to prevent conflicts with other functions)
Save and activate the snippet
- Scroll down and ensure the checkbox labeled Run snippet everywhere is checked.
- Click the Save Changes and Activate button.
Verify the snippet is working
- Perform a search on your site.
- Verify that only posts appear in the search results, excluding pages.
More use cases
There are various code snippets you can use with the Code Snippet plugin to enhance your WordPress site, such as:
- Installing Google Analytics in the header
Easily add your Google Analytics tracking code to monitor website traffic and user behavior. - Disabling emojis
Improve site performance by preventing the loading of unnecessary emoji scripts. - Adding custom CSS styles
Apply unique styling to elements on your site without editing theme files.
Using code snippets plugin lets you improve your site’s functionality while keeping it clean and well-organized.