Adding custom code snippets is one of the most powerful ways to enhance or extend a plugin’s functionality. One of the easiest and safest ways to add custom code snippets is by using third-party plugins. This article discusses how to add a custom code snippet using the Code Snippets plugin.
The Code Snippets plugin is a powerful tool that helps to add custom code snippets to a WordPress website without modifying the theme’s files directly thereby reducing the risk of compatibility issues and making updates smoother. To add a custom code using Code Snippets plugin, follow these basic steps
- Install and activate the Code Snippets plugin.
- On installation, a Snippets icon will appear on the WordPress dashboard.
- From the WordPress dashboard, navigate to Snippets > Add New
- In the Add New Snippet window type in a suitable Title, Code, Description and Tags in the space provided.
- Make sure that the Functions (PHP) tab is active.
- Select the location to run the code snippet from the four options available.
- Run snippet everywhere: Executes the code throughout the site.
- Only run in administration area: Executes the code within the WordPress admin dashboard.
- Only run on site front-end: Executes the code only on the public-facing part of the website.
- Only run once: Executes the code only once.
- Click on Save Changes and Activate to save and activate the code snippet.
So let’s take an example of adding a custom code to alter the font size in a WooCommerce Address label.
- As an initial step enter the title, code, description and tags in the space provided as shown below.
- Make sure to have the Functions (PHP) tab active.
- Then select the desired area to run the snippet.
- As a final step, click on Save Changes and Activate to save and activate the snippet.
Managing code snippets
- To view and manage the snippets, navigate to Snippets > All Snippets.
- One can view, edit, clone, export and delete snippets under the All Snippets tab.