If you run a WordPress website, there will certainly come a time when you deal with code snippets. Since the maintenance of code snippets can cause unnecessary work if they are not integrated correctly, you can use code snippet plugins. But there are also other ways to integrate code snippets into your website. In this article, we compare the best ways to add code snippets to your WordPress website.
Why Code Snippet Plugins at all?
For many WordPress websites, the code has been customized, but sometimes in a way that is not sustainable. Code snippets should not be included directly in the "Parent" or the Main WordPress theme. Because they will be overwritten and deleted as soon as you update the theme.
The same applies to plugins. If you change the code of a plugin directly, your changes will probably be deleted during an update. The only way to preserve your changes is to save them or make a backup elsewhere. Some plugin updates only add new features, but others also include security improvements. These need to be applied correctly to make your website less vulnerable to attacks. Therefore, applying code snippets incorrectly may even become a security risk, as you may get the idea to leave out the update because of the code snippet.
I will now introduce you to three ways of inserting code to be used in a functions.php file. One of them is code snippet plugins. This does not cover every situation where you want to customize a plugin or a theme , as it depends on the existence of appropriate hooks for the necessary changes. Hooks are places in the code for interaction with the WordPress core, like a kind of "hanger", where plugins and themes can "hook in". So if there is no hook for a certain desired function, code snippets (in functions.php) are the most sensible way.
Creating a WordPress plugin
This method requires some work on the file and the website itself, so be careful and make a backup save before making any changes. A plugin includes at least one folder for the PHP file. Try to think of a unique name for your folder and files (you can add a prefix based on your name or the name of your company, or something similar (for example "Raidboxes.io Disk Usage Sunburst").
Another thing you need in this file is a header with the plugin information. You also need a declaration of your plugin name, like the following:
<?php /** * Plugin Name: your plugin name */
Well done! You have now created your first plugin. Following the header, simply add your code snippets (the ones you want to include in functions.php) and upload the folder to your /wp-content/plugins/ folder on the server. Your plugin should now appear under plugins in the admin area.
If you're interested in learning more about creating WordPress plugins, read the Codex post about it.
I prefer creating a custom plugin, because it's great for adding code to functions.php. It's also easy to set up.
Use a WordPress plugin
For those of you who don't dare to create your own plugins, one of the code snippet plugins is a great way to manage your code snippets, or to add custom code to WordPress websites in general. You're probably already used to installing third-party plugins, so this may be easier than the first option.
Simply install the desired plugin and navigate to the new menu item that appears in the administration area. Note that different plugins can be structured differently. But most of them are simple and easy to understand. You should rename each code snippet you add which makes it easier to find a particular code snippet. This approach is highly recommended and can save you some trouble with many code snippets.
Decisive factors for the selection of a good plugin that allows you to run code snippets on your website are, among others, the influence on the performance of your website and user-friendliness. I will now introduce you to some WordPress code snippet plugins.
The best Code Snippet plugins for WordPress
- You can use PHP code in widgets, pages and posts
- Add PHP to create contact forms and process any kind of input or upload
- Add PHP to create user-optimized content
- With PHP you can customize every aspect of your WordPress installation
This plugin was developed by xyzscripts.com and is a popular extension that is updated regularly. With this plugin you can use PHP code that can be easily inserted into the normal editor of WordPress, as it creates snippet shortcodes for each code added. Pasting also works with a shortcode in the sidebar of the text widget of the site.
- The TinyMCE editor gives you a smart dropdown menu
- Convert PHP snippets to shortcodes for easy implementation
- Simple and flexible functionality
With the Custom CSS Pro WordPress plugin you can easily add custom CSS code to your WordPress website. This free WordPress plugin offers some handy features, including real-time preview and line numbering to make editing the code a little easier.
- Visually, like the environment in Visual Studio Code or Sublime Text
- It's easy to add custom CSS code to WordPress
- Live preview in real time
This kind of plugin makes it easy to insert code into the header or footer. The extension was developed by WPbeginner, who also developed the famous form plugin WPforms. With the plugin you can easily insert code snippets for the integration of services like Google Analytics and Facebook Pixel.
- Quick set up
- Easily insert headers, scripts and other code
- Is the simplest option for inserting tracking codes
If you use the page builder Elementor as a pro version, you do not need to install an additional plugin. Elementor has a built-in tool for inserting tracking codes and the like. Elementor also supports renaming of all scripts you add to your WordPress website. To find Elementor Custom Code, follow these steps. Go to Dashboard > Elementor Custom Code. Paste the script you want to place on your site there. Note that you must have an active Elementor Pro license to use the custom code tools built into Elementor.
- Simple and logical structure
- Integrated into your page builder
Use a child theme
This method is the least recommended for adding code snippets, but it still works well. It is a little more complicated than creating a plugin. A child theme should include code and styling that applies to the layout and look of your website. But a snippet is usually used to change the functionality and thus does not really belong to the "responsibility" of a child theme.
To create a child theme , create a folder with the same name as your parent theme and with the suffix "-child" (if the parent theme folder is named "raidboxes.io-theme", the child theme folder would be "raidboxes.io-theme-child").
In this folder you need at least two files, the stylesheet file called style.css and the PHP file called child.php.
The style.css file must contain a header with the following minimum specifications:
/* Theme Name: Raidboxes.io Theme Child Template: Raidboxes Theme Version: 1.0.0 */
Then add your code snippet to the end of the functions.php file and upload the child theme folder to the server at /wp-content/themes/.
If everything is OK, you can open the menu Design → Themes in your admin area and activate the new child theme. If you like to learn more about this, we explain how to create a child theme in the magazine.
This article provides you with some basic information about your options for integrating code snippets into WordPress. The decision whether creating your own WordPress plugin or child theme, or using a code snippet plugin is up to you - and depends only on your preferences, as the three methods functionally fulfill the same thing. It is best to use what suits you best.