Blog

We are a team of results-driven creative geniuses at PMGS Digital Agency that are enthusiastic about expanding your business and have the skills to do it.

Homeblogpaste-snippets-from-web-wordpress

How to paste snippets from the Web into WordPress?

How to paste snippets from the Web into WordPress?

Unlike professionals, beginners often find it hard to copy and paste codes into WordPress theme files.

This article will discuss the steps and functions of how you can copy and paste code snippets from the web to your WordPress (WP) and make sure your site does not get damaged.

Why should Code Snippets be added to your WordPress site?

WordPress is said to be the world’s top website building tool with the number of years it has been offering its products to the world and mainly for the flexible options it has to build up any kind of website you need.

Yes, you can add plugins instead of code snippets. While certain plugins are free, some you need to pay to get. However, using a simple snippet would make it more efficient. Because coding snippets can help you fix errors that often surprise you on WP, it also helps level up the security of your website and modify it with more additional features as you want.

Easiest ways to add code snippets to WordPress

It is highly advised to have a WP backup plugin installed, since this will keep every change and data of your WP site stored and you can always go back if something goes wrong. The WP theme templates will come in file types like index.php, single.php and others, while adding snippets, which will instruct you to add them to WP theme template files. These files can be used only for that exact theme, so this will have to be added directly or by creating a WP child theme.

Child theme is a perfect way to modify your existing WP theme since there is an already existing parent theme which saves your time from coding from the start. Because the formatting and functionalities are already included in the parent theme. There are two benefits to choosing this mode:

It is safe to update your WP theme. You will not lose the customised functions and formats when you update the parent theme, since only the child theme has been customized. If you are satisfied, you can use the same theme on another WP site as well.

Often code snippets will be added into your functions.php file on your WP theme while some professionals would recommend you to add it directly to the file even though there are other options which will enable future-proof changes. Through this, no changes will be made to your custom functionality even if you change the theme.

There are multiple ways to make changes on your WP site, considering which way you would choose to add the custom code snippets to your WP site or blog.

Option 1 (Easy) – How to add custom code with WP Code Plugin?

The safest and best way for a beginner to add new code to the WP site is through the use of the code snippets plugin

wpcode free code snippets plugin

You do not have to edit your theme files to add or manage the custom code snippets on

Your website is also built with smart code snippet validation, which saves you from common code errors that usually pop-up. This helps you control all code snippets from one central screen, and these can be organised accordingly with tags.

This option is useful only if you are supposed to add the snippets into the functions.php file because this will not work if you are supposed to add your code snippets into different theme files.

Steps to add or edit code to WP with WPCode

WPCode will allow you to add the codes directly to your WP admin dashboard.

To start with your WPCode, you first need to install and activate the plug-in. Then go to Code Snippets on the bottom and select Add Snippet and you can click on Add New.

wpcode add new snippet

Then this will take you to a screen where you can select from a previously made code library or create a new custom code.

add custom code new snippet

In order to add a custom code, click on the option that says “Use Snippet below to Add Your Custom Code.” Then, you can choose a name for your snippet with something that will be easier for you to remember. After naming, copy and paste your code into the box, and always make sure to choose the right code type from the drop-down menu on the top.

create custom snippet wpcode

Next, roll down to the Insertion section. With this, you can simply use the snippet code as a short code which can be manually attached anywhere on the website. But if you are willing to do it automatically, you can still do it with the Auto Insert method by selecting the location where you wish the snippet code to be added. WPCode has added benefits since it has a user-friendly option for beginners that allows them to choose when the code should load. If you finish up this process, click on the Save Snippet button, then swap the switch from inactive to active.

Steps to add code to the WordPress header and footer

At some point, you will only be required to add code to either header.php or footer.php files, while WPCode will make the process a lot easier. If you are planning on adding a tracking code for Facebook Pixel, Google AdSense, Google Analytics, and other options to your website, this will make your digital marketing a lot smoother and handier. In addition, you can manage all your header and footer codes from one point while avoiding any manual errors that would pop up. This also allows you to upgrade or change the theme without a hassle.

To follow up on this, navigate to Code Snippets, go to Head and Footer, and enter the snippet code to the location you need. And click on the Save Changes button to make the changes go live.

Option 2: How to Add Custom Code to a Site-Specific WordPress Plugin

Here, we are looking at a method that will not change or remove your changes due to updates or theme changes on your website. You can do this by using a site-specific WP plugin, which is a custom plugin that you can build for your website and save all your custom code with it.

Note that this method will only work if the code snippets are added to the functions.php file.

Custom codes can be added with the built-in WP plugin editor if you have a site-specific plugin. If you wish to do this, then go to Plugins and then Plugin Editor and choose the plugin from the drop-down menu. Then, the site-specific plugin will load, and you can add the code to the page.

Double check if you have clicked the Update File button to ensure that the changes have been saved.

add code to site new cool plugin

In case, something is wrong with the code such as a missing part or can break your site, then the changes will be automatically undone by the plugin editor. You can also add custom code for a site-specific plugin is by using FTP.

Option 3: How to add custom code to Functions.php or other themes?

Even though it is possible to add code snippets directly to your theme’s function.php, we highly recommend using the options above since this option has many of its own drawbacks. Firstly, all your changes will disappear as soon as you update your WP theme. Then, the codes added will only work with selected themes. As we have stated the major issues, Let us see how this can be copied and pasted in the right manner while also avoiding your website from breaking.

You can add codes directly to the theme’s function.php file or other page templates by navigating to Appearance and then go to Theme Editor in the WP admin panel. You can choose your file from the right side for it to appear in the editor.

How to troubleshoot PHP errors while adding custom code?

Common mistakes and simple solutions that are often made by beginners when they add a custom code snippet to their WP websites Below are the most common errors and the simplest way to solve them. PHP begin and end tags are being used in the wrong way.

Below you can see how a PHP code looks

troubleshoot PHP errors

WP is mostly written in the PHP programming language because it has a syntax that passes the message to the server saying that this code needs to be developed by PHP. The end tag in the PHP programming language has more importance for files that move back and forth between PHP and HTML.

At the same time, you also need to ensure that the code has been pasted in a location where the PHP start tag isn’t closed and that the code needs to be added with the starting PHP tag. If you happen to attach your custom code outside or after the PHP end tag, you will have to add the PHP begin tag too.

troubleshoot PHP errors

The majority of the errors occur due to incorrect placements, and if you look at your code, it would help you figure out whether you should or should not add the PHP start or end tags in your custom code snippet. However, the majority of the WP themes, such as functions.php, will not have the PHP end tag at all.

Also, at certain times, the given code snippets can be added to a number of locations, which will not include the PHP start and end tags.

Understanding PHP Errors on WordPress

The most common errors on WP are syntax errors, parse errors or fatal errors which are often caused by unexpected characters. But these errors will show you which line in your code has the issue that causes this error.

php error code example

Now you can drop down to the line and find what is missing, then fix it.

What to do when your WordPress site is not accessible?

Firstly, do not panic. All your information and data will be there and you can access them. Just connect your website using an FTP client or the file manager app in the cPanel of your host-providing account.

Then, find the file to which you added the code and which caused this error, and open it so you can start editing.

Now you should be able to solve it with a code snippet. And if you were not able to do it, then remove the code snippet you added and save your changes. Your website should now be back to normal again. If it still has an error, download a zip file of your WP theme. Then locate the file in which you made the changes previously and upload it to the server while overwriting the old file.

If you think this article was useful to you, then stay tuned with us and subscribe to receive our blogs. If you wish to stay connected with us on social media, you can find us on Facebook, Instagram and LinkedIn