Beginner’s Guide to Pasting Snippets from the Web into WordPress

Why Add Code Snippets to Your WordPress Site?

WordPress is the best website builder in the world due to the flexibility it offers. It is super easy to add new features and functionality to your WordPress website.

WordPress resource sites, like WPBeginner share tips and hacks that you can add to improve your website. While adding custom code snippets to your website is optional, and you can often find plugins to do the same task, sometimes a simple snippet is more efficient.

Some code snippets can be very handy and can help you fix many common WordPress errors, improve WordPress security, and even add new features to your website.

That being said, let’s take a look at how to safely add code snippets into your WordPress website.

What are The Best Ways to Add Code Snippets in WordPress

First thing you must do on every WordPress website is install a WordPress backup plugin. This keeps your WordPress site safe, and you can always restore it from backup in case anything goes wrong.

Sometimes you may find instructions to add code snippets in WordPress theme templates like index.php, single.php, header.php, and more. These code snippets are useful only in those particular templates, so you will have to add them directly to your theme files or create a child theme.

However, most custom code snippets are added into your WordPress theme’s functions.php file. While the tutorial may recommend you to add it in the theme’s functions.php file, there is a much better alternative that works the same way as a functions file.

Let’s take a look at some of the ways you can add custom code snippets in WordPress.

1. Use The Code Snippets WordPress Plugin

This is the safest and most recommended method for beginners. Code Snippets is a WordPress plugin which allows you to easily add and manage custom code snippets on your website.

It comes with a fail-safe switch which immediately deactivates a code snippet if it causes an error. This protects you from losing access to your website when adding a custom code snippet.

For detailed instructions, see our guide on how to easily add custom code snippets in WordPress.

Note: Code snippets method is useful for snippets that need to be added in a functions file. If you are asked to add a code snippet in other theme files, then this method will not work.

2. Create a Child Theme to Save Custom Code

The second approach is to create a child theme. Using child theme will keep your changes intact when you update your theme. It will also allow you to add code to other theme files if needed without worrying about updates undoing your changes.

For more details, see our guide on how to create a child theme in WordPress.

Note: This method will also work for code snippets that are needed to be added in other theme templates.

3. Use a Site-Specific Plugin for Custom Code

Another flexible option is to use a site-specific WordPress plugin. This is a custom plugin that you can create for your own website and use it to save all your custom code.

The advantage of this method is that your code is not dependent on your theme, and it will remain active even when you change themes. It is also not affected by any WordPress updates on your website.

See our guide on how to make a site-specific plugin for your website for detailed instructions.

Note: This method is only applicable for code snippets that need to be added in functions file.

4. Add Code Directly To Functions File

It is OK to add code snippets in your theme’s functions.php file. However, there are some downsides to it.

  • Your changes will be wiped off when you update your WordPress theme
  • Your code will only work if you are using that particular theme

That being said, let’s take a look at how to properly copy and paste code snippets and avoid breaking your website.

How to Edit WordPress Files?

There are different ways to edit WordPress files depending on which method you choose to add custom code snippets on your website.

1. Adding Custom Code in Code Snippets Plugin

If you are using the Code Snippets plugin, then you can easily add code snippets from WordPress admin area. Simply go to Snippets » Add New page to add your custom code.

2. Adding Custom Code in Site-Specific WordPress Plugin

If you are adding custom code in a site-specific plugin, then you can use the built-in WordPress plugin editor to add custom code.

First, select your site-specific plugin from the drop down menu labeled ‘Select plugin to edit’. The editor will load your plugin file, and you will be able to add code snippets in it.

Once you are done, click on the ‘Update File’ button to save your changes.

If there is something missing in your code or it has potential to break your website, then the plugin editor will automatically undo your changes.

Another method to add custom code in a site-specific plugin is by using FTP.

Simply go to the plugin folder using your FTP client. Right click on the plugin file and then select View/Edit file.

3. Adding Custom Code to functions.php or Other Theme Templates

If you are adding code snippets to your theme’s functions.php file or any other template, then you can add code directly by visiting Appearance » Editor. You can select the file where you need to add the code from the right column.

The instructions you are following will tell you where to paste the code, but if they don’t, then you need to add the code at the bottom of the file.

A better alternative way would be to use FTP to add custom code in theme files. Simply connect your FTP client to your website and go to /wp-content/themes/your-theme-folder/ and right click on the file that needs editing.


Select View/Edit file option to open the file in the text editor.

Troubleshooting PHP Errors when Adding Custom Code

There are some common mistakes that beginners make when adding custom code snippets to their websites. Let’s take a look at some of them and how to avoid them

1. Incorrect Usage of PHP Begin and End Tags

WordPress is written mainly in the PHP programming language which has a specific syntax that tells your server that the following code needs to be processed by PHP. Here is how a typical PHP code snippet looks like:

Tables can't be imported directly. Please insert an image of your table which can be found here.

1 2 3 4 5 6 7    

All your PHP code needs to be inside the <?php and ?> tags.

The PHP end tag becomes more important in files that switch back and forth in HTML. This includes most WordPress themes files which use PHP tags alongside HTML.

You need to make sure that if you are pasting your code at a location where PHP start tag is not closed then you need to add your code without the starting PHP tag.

Tables can't be imported directly. Please insert an image of your table which can be found here.

1 2 3 4 5 6    

If you are pasting your custom code outside or after the PHP end tag, then you need to add the PHP begin tag as well.

Tables can't be imported directly. Please insert an image of your table which can be found here.

1 2 3 4 5 6 7 8    

Almost 90% of all errors are caused by incorrect placement of PHP end or start tags. Studying the code will allow you to understand whether or not you need to add the PHP begin or end tags in your custom code snippet.

Many WordPress theme files particularly functions.php file may not have a PHP end tag at all. This means that you can add your code at the bottom of the file without the start or end tags.

Tables can't be imported directly. Please insert an image of your table which can be found here.

1 2 3 4 5 6 7 8 9 10 11

Remember that tutorial writers may sometime assume that you already know how to use PHP start and end tags. They may simply show you a code snippet without those tags.

Tables can't be imported directly. Please insert an image of your table which can be found here.

1 2 3 4 5 6

Since the tutorial author doesn’t know where you will be adding this code, they have skipped the PHP end and start tags. Now when you are adding such a code snippet in your theme files, you need to make sure that it is inside the PHP tags.

2. Incorrect Nesting Errors

PHP has a particular syntax for functions, conditional logics, and loops. This syntax depends on curly brackets which indicate when a function begins and when it ends.

For example, here is a simple PHP function:

Tables can't be imported directly. Please insert an image of your table which can be found here.

1 2 3 4 5

Now if you want to add a custom code snippet that has nothing to do with this function, then you will need to put it outside this function like this:

Tables can't be imported directly. Please insert an image of your table which can be found here.

1 2 3 4 5 6 7 8 9 10 11 12

If you miss the starting or ending curly brackets, then this will break the code, and you will end up with an error page.

Understanding PHP Errors in WordPress

Errors caused by adding a custom code in WordPress often result in a detailed error message. Most of them are syntax error, parse errors, or fatal errors due to unexpected characters.

The good part is that these errors will tell you which line in your code caused the error.

You can then go to the exact line to review the code and figure out what you missed. For this purpose we recommend using a proper text editor for code editing because they have line numbers and syntax highlighting which can help you fix the issue easily.

What to do When Your WordPress Site is Inaccessible?

First of all don’t panic. Simply connect to your website using a FTP client or File Manager app in cPanel. Next, locate the file where you added the code that caused the error and edit it.

You can try and fixing the issues with the code snippet. If you are unable to fix those issues, then remove the code and save your changes.

Your site should be back to normal again. If it is still showing some error, then download a fresh copy of your WordPress theme and extract the zip file to your computer.

Upload the file where you made the changes from your computer to your website while overwriting the old file.

For more ways to solve these issues, see our guide on most common WordPress errors and how to fix them. If that doesn’t help then follow our WordPress troubleshooting guide to perform a step by step diagnosis.

Was this article helpful?
0 out of 0 found this helpful