Logo

Adding Custom Code Snippet

Guide for safely adding custom code snippets to WordPress sites using functions.php or child themes via FTP/cPanel or dashboard editor.

Last updated on Jul 15, 2026

Prerequisites

Before adding any code snippet, make sure you have:

Administrator access to your WordPress site

The code snippet you want to add, reviewed and ready

A backup of your site (recommended before editing any files)


Method 1: Add Code to functions.php

You can add code snippets directly to your theme's functions.php file. However, any changes made to a parent theme's functions.php will be lost when the theme is updated. To retain your customizations after updates, always add the code to a child theme's functions.php instead.

If you haven't created a child theme yet, follow this guide first: šŸ‘‰ How To Create WordPress Child Theme

There are two ways to edit the functions.php file:

Option A: Via FTP or cPanel (Recommended)

If you are comfortable using FTP or cPanel, this is the safest approach. Editing files through a file manager bypasses the WordPress dashboard entirely, which means a code error won't lock you out of your site — you can simply re-open the file and remove the problematic code.

Option B: Via the WordPress Dashboard

If FTP or cPanel isn't an option, you can edit the file from within WordPress:

Go to Appearance → Theme File Editor.

Under the Theme Files column on the right, select functions.php.

Scroll to the bottom of the file, paste your code snippet, and click Update File.

A syntax error in functions.php can make your site inaccessible. If that happens, use FTP or cPanel to open the file and remove the incorrect code.

Method 2: Using the WPCode Plugin (Recommended for Most Users)

The WPCode plugin is one of the safest and most beginner-friendly ways to add custom code snippets to your WordPress site. With this method, you won't need to touch any theme files, and your snippets will remain active even after updating your theme or plugins.

WPCode also includes smart code validation that helps catch common errors before saving, reducing the risk of breaking your site.

Step 1: Install and Activate WPCode

Go to Plugins → Add New in your WordPress dashboard.

Search for WPCode – Insert Headers and Footers + Custom Code Snippets.

Click Install Now, then Activate.

Step 2: Add Your Code Snippet

Go to Code Snippets → + Add Snippet.

Hover over Add Your Custom Code (New Snippet) and click Use Snippet.

Enter a clear title for your snippet so you can identify it later.

Select the code type that matches your snippet — PHP, JavaScript, CSS, HTML, or Text.

Paste your code into the Code editor box.

Optionally, add a description to note what the snippet does. This is helpful when managing multiple snippets later.

Set the Location to Run Everywhere unless you have a specific requirement.

Step 3: Activate and Save

Toggle the snippet status to Active.

Click Save Snippet.

Your snippet is now live. Verify that it is working as expected on your site.


Which Method Should You Use?

functions.php via FTP/cPanel

functions.php via Dashboard

WPCode Plugin

Safe from theme updates

āœ… (child theme)

āœ… (child theme)

āœ…

Risk of breaking site

Low

Moderate

Low

Requires technical access

Yes

No

No

Recommended for beginners

No

No

āœ…

Method 1 via FTP/cPanel is best suited for developers who are comfortable working with server files. Method 2 using WPCode is the recommended approach for most users — it requires no file editing, keeps your snippets safe from theme updates, and makes it easy to enable or disable code at any time.


Managing Your Snippets

Once added, you can manage all your snippets from Code Snippets in your WordPress dashboard (WPCode) or by revisiting Appearance → Theme File Editor (functions.php method).

With WPCode, you can:

Enable or disable a snippet at any time using the toggle under the Status column.

Edit a snippet by clicking its title.

Delete a snippet by hovering over it and clicking Delete.

Organize snippets using tags to filter them by type or purpose.

Export snippets via Code Snippets → Tools → Export for backup or reuse on another site.