You may enhance the functionality of your website by using the built-in hooks offered by WordPress and most of its plugins. You will very certainly need to use code snippets to make advantage of the WordPress API. In this post, you will learn about alternatives to the code snippets plugin, such as using the functions.php file in a child theme.
Is it always a smart idea to install another plugin every time you need to update a certain element of your website or add something new? Naturally, finding and installing a new plugin in a few clicks would be much more convenient. There are at least two drawbacks to this strategy: security and page loading speed. To begin with, if you install an old or low-quality plugin, you run the danger of hackers exploiting backholes in the code to infect your website. Second, each new plugin might bloat your website with more CSS and JS files, potentially slowing it down.
There is no disputing that the majority of users are completely unaware of their existence and would prefer to install extra plugins that would help them to make changes to their website. Overall, having a separate plugin may not be the most efficient solution in many circumstances, and code snippets may be a better alternative. In this tutorial, we will show you how to use and add custom snippets to your WordPress website in three different ways.
Can code snippets be used instead of plugins in WordPress?
Is it better to utilize a single code snippet or install a new plugin whenever you need to alter the functionality of your website? The answer is simple if you have at least a basic understanding of the PHP programming language. Instead of adding a plugin, you may use a simple snippet to embed Google Analytics tracking code in the footer of your website, for example.
As you are undoubtedly aware, WordPress’s default editor for writing content is Gutenberg. Many people, however, prefer to utilize the old “classic” editor. What would you do if you wanted to turn it off and go back to the previous editor? Using a simple code snippet, you may deactivate the Gutenberg editor without the need for a plugin (such as “Disable Gutenberg“).
Three methods to use WordPress code snippets
In this section, we will look at the specifics of the three most frequent methods for adding custom WordPress code. Naturally, we mentioned not just the most common method, but also an alternative to WordPress Code Snippets plugin.
So, what is the best method for adding code to a WordPress website? To help you choose, we have outlined three separate ways.
How to insert PHP code in WordPress without plugin?
If you are familiar with the WordPress admin dashboard, adding extra PHP code to your website using the functions.php file in a child theme directory is a secure and extremely dependable method. This method is also an excellent alternative for Code Snippets plugin, which is discussed later in this post.
WordPress allows you to set-up “child themes” that you may use to edit the “parent theme” without risking losing your custom code. A child theme is meant to keep the user’s code modifications isolated from the parent theme’s so that the parent theme may be updated without losing these changes. This allows you to update the main theme without having to worry about your custom code in the child theme.
Now that we have covered the basics of what a child theme is, we will go through how to change functions.php to add code snippets. WordPress includes a tool called Theme Editor, which allows you to alter theme files straight from the dashboard. You may access the theme code editor by going to “Appearance -> Theme Editor“.
In the right sidebar menu, choose “Theme Functions” (functions.php) and then paste the snippet into your child theme. Make certain that the files are properly edited, and use caution while inserting code snippets. A useful feature of the WordPress theme editor is that if you attempt to save changes to a file and a fatal error is detected, the change is not saved and a warning message is presented that indicates where the issue occurred.
How to add code snippet using “barebone” plugin?
A “barebone” plugin is another WordPress alternative to the Code Snippets plugin for individuals who are not sure how to use a child theme. Aside from the “Theme Editor” WordPress also allows you to change your plugin files in the same fashion. There is one possible benefit to this solution: by creating a separate mini-plugin, you may clone your code along with the plugin file and utilize it on numerous websites, regardless of theme.
To use it, just download the ZIP file containing the barebone plugin and then install it from the “Add plugin” section of the plugin’s settings page.
Now that the plugin has been installed and enabled, you may use WordPress to make changes to its content. For this, go to “Plugins -> Plugin Editor” page and choose “My custom code snippets” in the “Select plugin to edit” field.
There is an initial snippet included in the mini-plugin that lets you install Google Analytics tracking code, but you can easily delete it and replace it with your own tracking code if you like.
How to add code snippet using Code Snippets plugin?
On the surface, the idea of using an additional plugin to remove an additional one may appear illogical. Although this is true, it is sometimes beneficial to replace a few plugins with a proven one. If you are unfamiliar with the WordPress framework, this is unquestionably more convenient. Among them, “Code Snippets” is the most widely used and simplest to use.
The Google Analytics tracking code should only be shown in the front-end, therefore pick the “Only run on site front-end” option below the code editor.
Useful WordPress code snippets
If you would like to see some sample code snippets, I have put some of them on Gist. They may be used to deactivate the Gutenberg editor or to delete Emoji scripts from your website’s HTML code.