Where to Put Custom Code for Your Website in WordPress

Written by Jean Galea
Written by Jean Galea

Last updated on 26 Sep 2022

Whenever you need to add custom code to your WordPress site, the typical recommendation is to put it in the theme's functions.php file. I don't like doing that as it's easy to forget to copy-paste that code to the new theme when it's time to a refresh. Moreover, if done in that manner, you don't have the ability to switch on or off certain functions. The solution for me has been the following plugins:.

WP Mayor is supported by its audience. If you purchase through a link on our site, we may earn a commission. Here’s why you can trust us.

Get started with our recommendation today by clicking on the button above to get the best price.
1-minute read

Table Of Contents

Table of Contents

Before We Get Started

Do you already have your website set up with a reliable host?

If not, or if you’re looking for a better option (free migration included) we’ve worked with our close friends at WP Engine to get our hands on an incredible hosting offer that’s ideal for WordPress sites.

It includes performance optimizations for your site, security features, automated updates, 1-click backups, and a bunch of free tools.

Get it here and save over $100 in your first year!

Introduction

Whenever you need to add custom code to your WordPress site, the typical recommendation is to put it in the theme’s functions.php file.

I don’t like doing that as it’s easy to forget to copy-paste that code to the new theme when it’s time for a refresh.

Moreover, if done in that manner, you don’t have the ability to switch on or off certain functions.

The solution for me has been to use a simple plugin.

#1 Code Snippets

Code Snippets is an excellent plugin that lets you create snippets of code as functions that can be switched on or off at will, in the same way as you would with normal plugins.

Simply install the free plugin, create a snippet, paste your custom code, and save it.

Source: Code Snippets

You can then turn on or off any custom code at will without the risk of breaking your site.

#2 Custom Plugin

Alternatively, you can create your own custom plugin that stores your custom code.

In the past, we used to use Pluginception which let you create plugins from within the WordPress dashboard itself, but it hasn’t been updated in a while now.

It was useful when I just wanted to create one plugin to dump all the custom functionality into and have it protected whenever I change themes.

You can still do this by creating your own custom plugin, but if you’re not comfortable with code, go for option #1 and play it safe.

<span style="font-weight: 400">Written by: </span>Jean Galea
Written by: Jean Galea

Jean Galea is an investor, entrepreneur, and blogger.

He is the founder of WP Mayor, the plugins WP RSS Aggregator and Spotlight, as well as the Mastermind.fm podcast.

His personal blog can be found at jeangalea.com.

WP Engine

Quick Links

Did you find this article helpful?

Share Your Discovery

4 Responses

Leave a Reply

Your email address will not be published. Required fields are marked *

 Discover more tips and tools. 
Join 5000+ designers, developers, and website builders that come to WP Mayor to find the best guides, tools, and services for their next website. One email every week.
WP Mayor Newsletter

What's missing?