If you are not a developer, Inserting code snippets on your website may be difficult. Inserting code in the wrong place might break your website design. But In this tutorial, we’ll cover the perfect areas where you can insert code snippets into your WordPress site. We will also make sure that those code snippets perform well and do not break your site design.

“Code snippets are small pieces of reusable code that can be inserted into a larger software program to perform a specific function. They are often used to save time and improve efficiency when writing software code.”

Following are how you can choose to insert code on your wordpress website:

  • Code Snippet Plugins
  • Add Code Snippets To functions.php File
  • Create Plugin For snippets

1:Code Snippet Plugins

Code snippet plugin is one of the most advance and easy ways to insert code on a wordpress website. You can write any code snippet and activate it. I am using the “Code Snippet “plugin to show you an example.
Go to Plugin➡Add New➡Search Code Snippets

Screenshot of code snippet plugin
  • Click on Install and activate it.
  • After Activating the plugin:
  • Click On Add New Snippet.
  • Now Add code.
  • Select the language.
  • Activate the code and save it.
"Screenshot Of Code Snippets activating plugin"

Here’s an example of a simple code snippet in PHP that can be used to display the current date on a webpage:

"How to add Code snippet plugin"

2:Add Code Snippets To functions.php File:

Suppose you don’t like any external sources for adding code snippets on your WordPress website. Then you can add code on Function.php File.

  1. Log in to your WordPress website’s admin area.
  2. Go to Appearance -> Theme Editor.
  3. Select the functions.php File from the list of files on the right-hand side.
  4. Add the following code at the end of the File.
How to add code snippets on theme functions.php

3:Create Plugin For snippets

Here are the steps to create a plugin for adding code in WordPress:

  1. Create a new folder in the wp-content/plugins/ Directory of your WordPress installation. You can name this folder anything you want, but it’s preferable to use a descriptive name for a new plugin.
  2. Inside the new folder, create a new PHP file and name it the same as your plugin folder name, with a .php extension. For example, if your plugin folder is named “my-code-snippets”, name the PHP file “my-code-snippets.php”.
  3. In the PHP file, add the following code to create a new WordPress plugin:
/*
Plugin Name: My Code Snippets
Description: A plugin for adding custom code snippets to WordPress
Version: 1.0
*/

// Add your code snippets here

This code creates a basic WordPress plugin with a name, description, and version number.

  1. Add your code snippets to the Plugin. You can add your snippets to the Plugin by defining functions, shortcodes, or filters. Here’s an example of a shortcode that displays the current date:
function current_date_shortcode() {
    $current_date = date('F j, Y');
    return 'Today is ' . $current_date;
}
add_shortcode( 'current_date', 'current_date_shortcode' );

This code creates a shortcode named “current_date” that displays the current date when used in a post or page.

  1. Save the changes to the PHP file.
  2. Upload the plugin folder to the wp-content/plugins/ directory of your WordPress installation.
  3. Activate the Plugin from the WordPress admin area by going to Plugins -> Installed Plugins and clicking the “Activate” link next to your Plugin.

Now your Plugin is active, and you can use your code snippets in your WordPress site by using the shortcodes or functions defined in the Plugin.

In short: In this post, we described how to add codes snippet to your WordPress website using the above three methods. A code snippet plugin is the easiest method if you are not good at coding. You can use also other two methods for adding code. Just be careful while inserting code. If you have any questions comment below.Thanks!

Categorized in: