• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
NameHero® Blog

NameHero® Blog

Web Hosting Tips & Resources From NameHero

  • AI Agents
    • OpenClaw
    • Claude Code
    • n8n
    • Hermes Agent
    • Open WebUI
    • Docker
  • Hosting
    • Web Hosting
    • WordPress Hosting
    • WooCommerce Hosting
    • Enterprise Hosting
    • Email Hosting
    • HeroicGuard
    • GPU Hosting
    • Website Builder
  • VPS
    • Managed VPS
    • Unmanaged VPS
    • Flex VPS
  • Reseller
  • Gaming
  • Domains
  • Account
  • Blog Home
  • Categories
  • Authors

Where To Put Custom Code In WordPress (Hint: Use a Plugin)

Bhagwad Park

Published on: July 3, 2018

Categories: WordPress 6

You’ll often find code to insert while researching some WordPress problem. Most people who come across this for the first time, have no idea where to place it. The usual response is your theme’s “functions.php”. But this is a bad idea.

Why not Use Functions.php for WordPress Code?

Using functions.php to insert code in WordPress is dangerous for the following reasons.

First, your theme can change. And when it changes, the functions.php file changes as well. This means that you’ll have to port your entire code over to the new theme’s functions.php. It’s an additional step that’s inconvenient, and that you might forget.

The second reason is much more serious. Errors in functions.php will crash your entire site. That’s because the theme itself is corrupt. WordPress doesn’t know how to deal with a problematic functions.php file without bringing down the theme itself. Simple syntax errors will break your site, and that is a very dangerous thing.

For this reason, I prefer to use a custom plugin for adding code instead. And this article will show you how to do it.

Step 1: Create the Directory and File on your Desktop

On your PC, create a folder with a name like “namehero-plugin-code”. Inside that folder, create a new file with a “.php” extension with the same name (namehero-plugin-code.php). Here’s an image of the folder and file:

Directory Structure

Step 2: Paste the Basic Plugin Code into the PHP File

Open the PHP file you created in Step 1, and paste the following code into it:

<?php
/* Plugin Name: NameHero Custom Code Plugin
Description: Place to insert custom code into WordPress
*/

This is the skeleton custom plugin code. After installation, the “Plugin Name” will appear in your list of plugins.

Step 3: Compress the Folder with the File Inside it

WordPress allows us to upload only “zip” files. So right-click the folder, navigate to “Send to”, and select “Compressed (zipped) Folder” as shown here:

Create a Zipped Folder with the Plugin

This will create a single zipped file that we’re ready to upload in WordPress

Step 4: Installing the Plugin via the Zip File

Navigate to your WordPress dashboard, and click “Plugins->Add New” as shown here:

Add New Plugin

Now click the “Upload Plugin” button at the top. This will open up a section allowing you to choose a file from your desktop. Click the button, select the file, and select “Install Now”:

Choose File and Install Now

This will install upload and install the plugin. When that’s done, we just need to activate it as shown here:

Activate Plugin

Now the plugin will appear in the list of your installed plugins.

Step 5: Adding Code to the New Custom Plugin

It’s time to add our code to the plugin!

Sanitize the Code

Let’s say you come across a site that asks you to install some PHP code into WordPress. The first thing you need to do is check if it starts with:

<?php

Or if it ends with:

?>

If the answer is “yes” to either, remove both of them. Basically ensure that the code on the website doesn’t start with <?php , or end with ?> . This is very important.

Edit the Plugin and Insert the Code

Next, go to the WordPress dashboard and navigate to “Plugins->Editor” as shown here:

Plugin Editor

In the next screen, select the custom plugin we just added from the drop-down box on the top right-hand corner as shown here and hit “Select”:

Select and Edit Custom Plugin

This will now allow us to add code to the plugin. Paste the sanitized code you got from the previous step into the text editor as shown here:

Place to Add Custom Code in WordPress

After that, click “Update File” and you’re done! You’ve successfully inserted custom code into your WordPress in a safe and flexible way. This code will remain regardless of which theme you choose, and it won’t crash your site if something goes wrong either!

Bhagwad Park Profile Picture
Bhagwad Park

I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!

Related Posts

How To Start A Newsletter In WordPress

Check out our complete guide on why and how you should start your own newsletter in WordPress.

Blogger vs. WordPress: Which One to Use?

Let's compare WordPress and Blogger across five key areas. Then, we'll show you how to set up a professional blog with WordPress.

How to Eliminate Render-Blocking Resources in WordPress (& Why You Should)

Many things can slow down your site, including render-blocking resources. These are CSS and JavaScript files that delay the loading of content on your site. As a result, visitors will have to wait longer to interact with your page, which could lead to frustration and instant exits.  So, what can you do to eliminate render-blocking […]

How To Back Up Your WordPress Site (And Why You Should Do It Today)

In this post, we’ll take a closer look at why backups matter. We’ll also show you different ways to back up your WordPress website.

Reader Interactions

Comments

  1. Young Lee says

    August 9, 2020 at 7:50 am

    Hello, thank you for this, but when I try it, I get “Scrape nonce check failed. Please try again.” please help.

    Reply

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow & Subscribe

Exclusive promos, content and more!


Most Popular Posts

NameHero’s Recommended WordPress Plugin and Theme Setup

WordPress Hosting vs. Web Hosting – What’s The Difference?

How To Increase The InnoDB Buffer Pool Size

How To Fix A Stuck All-in-One WP Migration Import

How To Add A Subdomain In Cloudflare

Top Categories

  • WordPress
  • WordPress Tutorials
  • OpenClaw Hosting
  • Enterprise Hosting
  • WooCommerce
  • Web Hosting
  • Resellers
  • Website Security
  • Website Development
  • Website Performance
  • VPS Hosting
  • SEO Tips
  • Announcements
  • Domain Registration
NameHero

NameHero® proudly provides web hosting to over 40,000 customers with 99.9% uptime to over 750,000 websites.

  • Master Card
  • Visa
  • American Express
  • Discover
  • Paypal
Products
  • Web Hosting
  • Managed VPS Hosting
  • Unmanaged VPS Hosting
  • Flex VPS Hosting
  • WordPress Hosting
  • WooCommerce Hosting
  • Reseller Hosting
  • Enterprise Hosting
  • GPU Hosting
  • Email Hosting
  • HeroicGuard
  • Domains
  • Website Builder
  • AI Agent Hosting
Help & Support
  • NameHero Blog
  • NameHero Gaming Blog
  • Support
  • Help Center
  • Migrations
  • Affiliates
  • Gaming Affiliates
  • Call 1-855-984-6263
Company
  • About Us
  • Contact Sales
  • Reviews
  • Uptime
  • We're Hiring

Copyright © 2026 Name Hero, LLC. All rights reserved.
NameHero® is a registered trademark.

  • Privacy Policy
  • Terms of Use
  • Acceptable Use Policy
  • Payment Policy
  • DMCA