Code Snippets Tutorial (Where to Insert the Code)

I have a plenty of code tutorials on my blog but almost in none of them I describe in details where to insert the code in WordPress. It is because I write mostly for developers who already know something so I usually skip the basic part.

But I came to a decision that it would be super-useful to have a tutorial like this on my blog.

So here we are – right now I am going to show you 3 ways how you can use code snippets on your WordPress or WooCommerce website.

1. “Code Snippets” Plugin

Usually I am trying to be a super-minimalist when it comes to using third-party plugins, but if you’re not familiar with code a bit, using “Code Snippets” plugin could be a game-changer for you.

  • It is free and can be installed directly from WordPress admin.
  • You can turn on and turn off specific snippets from plugin settings when needed.
  • Snippets won’t be added in case the code has errors.

And I am not advertising it, I really find it simple and easy to use.

First, please go to Plugins > Add New and search for “Code Snippets” plugin there.

code snippets plugin for WordPress

Install and activate the plugin, then you can just go to Snippets > Add new. Type any name for a snippets, paste the code and that’s actually all you need to do.

add code snippet to WordPress functions.php

For example you can use the snippet from my website that allows to auto-update the cart.

example of adding a code snippet
I know you might think – what is the point of using cart autoupdate snippet if we don’t have WooCommerce installed. But it is just an example guys.

Now let’s go to Snippets > All snippets and as mentioned above you can see a toggle that allows to turn on and turn off specific snippets when you need them or not.

active snippet added with the plugin

So, it was the easiest way to use PHP code snippets on your WordPress website. The ways described below require a little bit more knowledge from you. At least you have to know how to connect to your website with FTP.

2. Creating a Plugin for Snippets

This is more developer-like way of adding PHP code snippets to your website. It definitely has its advantages, for example a huge advantage that the snippets aren’t going to be stored in database and they are located directly in the code, in the exact place they are meant to be.

All you have to do for this method is to create an empty .php file in wp-content/plugins directory. The file content should be like this:

	 * Plugin name: Snippets by Misha
	 * Description: My snippets
	if ( ! defined( 'ABSPATH' ) ) {

	// below can be your code snippets

3. functions.php

And finally my favourite way to use code snippets.

Why my favourite? Because I rarely use ready themes, I prefer to develop them by myself. So, there is not point of inserting some hooks (snippets) into a separate plugin when I can use it directly in my theme files.

But there is also a moment with Child Themes you have to keep in mind. I hope the table below will make it clear for you.

SituationWhat to do
You’re developing WordPress themeFeel free to insert snippets directly to functions.php file or to any file included into it. Actually I think if you’re a theme developer you already know what to do.
You’re using a theme by another developerIf your theme receives updates from time to time, you have to create a child theme for it and insert the code into functions.php of a child theme. If your theme never gets updates, you can actually use functions.php of the theme.
There is already an active child theme and it gets updates from time to time.Then you have to use Method 1 or Method 2.
Misha Rudrastyh

Misha Rudrastyh

Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.

Need some developer help? Contact me

Follow me on X