• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Startup Hero

The Official Blog Of Name Hero

Learn To Fly Above The Competition Get Started
  • HomeWelcome
  • CoursesVideo Training Center
  • About UsWhat is StartupHero?
  • BlogGet the latest
  • Start HereStartup 101
  • SpeakingPodcast & Media
  • ResourcesTools to help You
    • Reselling WordPress Hosting
    • Resell Hero
    • How To Start A Blog
  • NameHeroCloud Web Hosting

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

By Bhagwad Park on July 3, 2018 6

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

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!

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

Trackbacks

  1. How To Add Google Fonts To WordPress Without A Plugin says:
    July 9, 2018 at 11:52 am

    […] option is to add the code into the WordPress “functions.php” file. Or even better, add it in a custom plugin that you use for all kinds of code snippets. I highly recommend the second option over the first, […]

    Reply
  2. How To Add Google Fonts To WordPress Without A Plugin – My Blog says:
    July 12, 2018 at 7:26 am

    […] second option is to add the code into the WordPress “functions.php” file. Or even better, add it in a custom plugin that you use for all kinds of code snippets. I highly recommend the second option over the first, […]

    Reply
  3. How To Add Google Analytics To Your Site Without A Plugin says:
    July 25, 2018 at 5:44 pm

    […] second way to do it is to add code to your theme’s functions.php at the bottom. Better still, create a custom plugin for PHP code which you’ll use for all future code insertions. It’s totally worth the extra effort, […]

    Reply
  4. How To Remove Theme Footer Links In WordPress | Startup Hero says:
    July 30, 2018 at 3:42 pm

    […] Note: For the code samples below, you need to know how to insert them. Here’s a tutorial on how to paste custom code into WordPress. […]

    Reply
  5. How To Add A Shortcode In WordPress | Startup Hero says:
    August 8, 2018 at 12:19 pm

    […] functions.php, or via a custom plugin. I suggest the latter, and show you how to do this in an earlier tutorial. Once you know how to add code like this, copy and paste the following into either functions.php or […]

    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

Connect With Us!

Superhero Resources

Fix Common Issues

  • How To Setup Free And Automatic SSL Certificates
  • How To Setup Cloudflare With Railgun
  • How To Fix Memory Exhausted Errors In WordPress
  • How To Edit PHP Version/Upload Limit/Add Extensions
  • How To Move/Migrate Your Business To Name Hero

Free Guides

  • How To Setup NameHero Hosting
  • How To Create A Web Hosting Business With WordPress
  • How To Start A WordPress Blog
  • How To Migrate WordPress To A VPS
  • How To Speed Test And Optimize Your WordPress Website
  • Magento 2.X Installation Guide
  • How To Clean Up A WordPress Hack

Training

Recent Posts

  • Why I Don’t Use Two Factor Authentication
  • Filtering Out “replytocom” Bots On WordPress
  • Five Years Straight Of 99.9% Website Uptime – NameHero
  • What Can You Do To Improve Mobile Load Times?
  • The Challenges Of Rate Limiting For Websites
  • When Hosting Location Doesn’t Matter (And When it Does)
Subscribe in a reader
  • Web Hosting
  • WordPress Hosting
  • Reseller Hosting
  • VPS Hosting
  • Twitter
  • Facebook
  • LinkedIn
  • YouTube
  • FTC Disclosure
  • Earnings Disclosure
  • Privacy Policy

Copyright © 2021 · Smart Passive Income Pro on Genesis Framework · WordPress · Log in