How to Use Code Snippets in WordPress

How to use code snippets in WordPress

WordPress provides a wide range of exceptional features to build various types of websites. While plugins and themes offer efficient ways to improve website functionality, behavior, and appearance, the addition of custom code snippets stands out as the best and easiest method to introduce additional functionalities to your WordPress website. Code snippets are small sets of codes, which you can use on your website to elevate its functionality or tailor its behavior. They can also be used to customize the functionality of plugins and themes. In this article, we’ll delve into a detailed discussion of what code snippets are, why to use code snippets, how to use code snippets, and much more.

What are code snippets in WordPress?

As aforementioned, code snippets are small, self-contained pieces of code that can be inserted into your themes functions.php file, a custom plugin, or a self-dedicated code snippets plugin. They act as a core element to improve your site’s functionality, appearance, and performance.

Why Use Code Snippets?

Code snippets can be used for various purposes like fixing common site errors, improving security, and adding new features to the website. Instead of using single-feature plugins that overload your website or child themes, you can leverage code snippets and bring whatever functionality you want to add to your website. It lets you introduce new features, or tweak existing features without hiring a developer. They integrate with external services or platforms like adding Google Analytics code, social media buttons, or other functionalities to your website. You can bring changes to your themes, enhance navigation, and create engaging user interactions, thus lifting up the overall user experience.

Different Types of Code Snippets

Below are the three types of primarily used code snippets.

1. PHP code snippets: You can embed PHP codes on your website using this snippet. These are powerful and enable you to change the core functionality and design of your website.

2. CSS code snippets: These snippets are useful to add custom styles to the elements on your website. This may include fonts, colors, layouts, and responsiveness. Without editing the theme files, you can customize the look and layout of your website.

3. JavaScript code snippets: Using this type of snippet, you can include dynamic and interactive features to your website, such as animations, sliders, or pop-ups.

WP Ultimate CSV Importer Pro

WP Ultimate CSV Importer Pro

Get Ultimate CSV/XML Importer to import data on WordPress faster, quicker and safer.

How to Add Code Snippets in WordPress

Adding code snippets to WordPress includes two methods. 

1. Manually adding codes to themes functions.php file

2. Using the Code Snippets plugin

Let’s discuss both in detail.

Manual method to add Code Snippets

Before manually adding the code snippets to the theme file, you should take a backup of your website both files and database using any WordPress backup plugin. By this way, you can ensure your website’s safety and revert the backup in case of any errors.

Adding code snippets manually in WordPress involves editing the theme files or functions.php file directly. It’s better to add the code to the child theme’s functions.php file rather than the parent theme to avoid losing changes when the theme update happens.

Now follow the below steps to add code snippets manually in the functions.php file.

  • First, we need to access the theme file. You can access it via the WordPress admin dashboard, FTP client, or using cPanel file manager.
  • If you want to add from the admin dashboard, log in to your WordPress site and navigate to Appearance > Theme File Editor. On the right side, select the ‘functions.php’ file to add the code.
  • If you’re using the FTP/cPanel file manager, navigate to ‘/wp-content/themes/your-theme/’. Download the functions.php file in your local machine to edit the file.
  • Open the file in any text editor and add the custom snippet code at the end of the file. But before the closing PHP tag ?> if it exists.
  • Save the changes. If you’re using the FTP or cPanel, upload the file back to the path ‘/wp-content/themes/your-theme/’ and it will overwrite the old file.
  • Once all the steps are down, check your website for any changes thoroughly and make sure it didn’t break the site. If something goes wrong, debug the log by adding the code ‘define(‘WP_DEBUG’, true);’ in your wp-config.php file to see any errors. Otherwise, you can restore your website from the taken backup.

Note that always test it with the development or staging site before implementing it in your production site to avoid site breakage issues.

Add Code Snippets using Plugins

Introducing code snippets using plugins to your website is the safest and easiest way compared to the theme file editing method. This eliminates editing your site files and losing changes even after the theme update. Here in this article, we’ll discuss the two popular code snippets plugins that are:

1. Code Snippets

2. WPCode – Insert Headers and Footers + Custom Code Snippets – WordPress Code Manager

Using Code Snippets Plugin to add custom code snippets

Code Snippets plugin enables easy to add and manage code with its user-friendly interface. Install and activate the Code Snippets plugin on your website and follow the steps below to add the code.

  • Go to Snippets from the left sidebar of your admin page.
  • Click Add New to add your custom code.
  • It asks you for the title of the code. Provide the relevant name as a title for the code and paste the set of codes inside the code area.
  • Next choose the location where you want to run the code. It provides options like run snippets everywhere, only run in administration area, only run on site front-end, and only run once.
  • Choose one option and click ‘Save Changes and Activate’. That’s all, your changes are successfully run on live.