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. This post will teach you three alternative methods to use and simply add WordPress code snippets.
Custom snippets may be used in a variety of ways. 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. 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.
Can code snippets be used instead of plugins in WordPress?
Is it smarter to install another plugin instead of utilizing a single code snippet? Yes, provided you are familiar with 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
How to insert PHP code in WordPress without plugin?
One of the most frequent errors WordPress users do is changing the theme files’ core files. Unfortunately, when they upgrade the theme to the most current version, all of their custom code will be overwritten. WordPress enables you to establish “child themes” that you may use to edit the “parent theme” without risking losing your custom code. To put it another way, child themes separate modifications from parent theme functions, allowing parent themes to be upgraded without deleting them.
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?
For those who are not sure how to create a child theme, a “barebone” plugin is an alternative. 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.