Resources     Blog  

  1. Home
  2. WordPress Tutorials
  3. How to Paste Snippets from the Web into WordPress

How to Paste Snippets from the Web into WordPress

WordPress is an open source program that gives users the ability to modify its coding to customize their sites in countless ways. Part of the reason people choose this program is because it is user-friendly while still offering webmasters lots of options for making sites that look and function exactly the way the site builder envisions. One way webmasters can take advantage of this feature is by taking snippets of code from various places on the web and pasting them into their own site. This beginners guide to pasting WordPress snippets will help users cut and paste codes while avoiding common mistakes that cause websites to malfunction.

Finding Snippets on the Web

There are a lot of places on the internet to get snippets. A simple search for code snippets produces nearly two million results. On top of this, there are multiple plugins that add snippets. When searching the web for codes, be careful about using coding snippets from untrustworthy sources. Improper coding can cause a site to stop functioning and malicious coding can even contain malware or viruses. Just remember that WordPress has thousands of plugins to use. Most of these do not require a single line of code, which is great for beginners.

Locating the Right WordPress File for Snippets

When adding codes, it is crucial to add them to the correct file. WordPress sites contain two different files that are labeled functions.php files. One deals with the site’s core functions, and the other deals with the theme’s functions. When adding snippets, they need to go into the theme function files. Be sure not to put snippets into the core functions.php file. This is found in the folder called /wp-includes/ folder, and it is not the correct place to add coding that adds functions to themes. The correct file to use is called functions.php, and it is located in /wp-content/themes/uniquethemename/ folder. Once the correct file is located, the next step is to make sure the WordPress snippet is pasted into the right file correctly.

Pasting WordPress Snippets Correctly

PHP stands for hypertext preprocessor, and snippets that go into the theme’s functions.php file need to be placed properly in order to function correctly. It can be helpful to understand what opening and closing tags look like. This will help avoid mistakes that involve placing coding into the wrong spot.

Opening tags use the following instructions:

<?php

Closing tags look like this:
?>

Knowing how to identify opening and closing tags does two things. First, it makes it easy to identify coding that fails to use opening and closing tags. It also helps to ensure that the coding gets placed appropriately. If coding does not contain opening and closing tags, there are two ways to deal with that. One way is to add the opening and closing tags manually. Another way is to place the WordPress snippet between coding and closing tags that are already in the theme’s functions file.

Placing Snippets into the Functions File

When the coding looks correct, contains opening and closing tags and is ready to be placed into the functions.php file, it is important that it is put between the closing tags and opening tags of other codes. If it does not contain opening and closing tags, they can be added by putting <?php before the coding and ?> after it. The alternative, to use the code without those tags, requires placing the coding after the opening tags and coding of another function, but before the closing tags.

Mistakes and Fixes for WordPress Snippets

A major mistake can cripple a website and take it offline. A minor mistake will mean the snippet does not work. Because opening and closing tags are crucial in order for the program to recognize coding as commands, this mistake is one of the most common. For that reason, it is the first error to look for when assessing what went wrong when a code fails to work.

Read Instructions Carefully

If the opening and closing tags are present, and the code has not been placed between other opening and closing tags, the next thing to look for is bad coding. For beginners this can be hard to identify, so the best course of action is to cut the pasted code, read the code author’s instructions carefully, and try again. Sometimes coders assume that users will understand more about coding than beginners usually do. However, most of the time, the instructions include special notes regarding things to help to get the code to work properly.

Make Sure to Have All the Required Plugins

Having the required plugins is crucial if a code uses a plugin to function. Make sure to install and activate any plugins in the WordPress snippet code. Most coders will add a conditional statement into the code that checks whether the plugin exists. However, it can be confusing for someone who doesn’t have a great deal of experience with coding.

Get the Final Code Without Instructions

Even simple code can be hard to understand if it is not explained properly. To help with this, many coders will add extra text to explain their code. However, this often leads to mistakes by beginners who blindly copy everything. This sometimes leads to mistakes. Coders who include such tutorials will usually put the final code at the end of their articles. Thus, if you are in a rush look for the complete code near the end of the document.

WordPress provides a versatile site builder for webmasters of all skill levels, and learning how to properly use snippets can allow users to implement lots of functions to their sites without having to code it themselves. Learning some coding basics, however, can make it easier to troubleshoot problems that arise from misplaced codes, bad codes, or lack of proper plugins. Before altering codes on a WordPress site, it is advisable to always create and save a backup of the site before making changes so that the site will remain intact even if mistakes occur.

Author: Kaumil Patel

Kaumil Patel is the Chief Operating Officer of GreenGeeks and has over 13 years of experience in the web hosting industry working for and owning web hosting companies. Kaumil’s expertise is in marketing, business development, operations, acquisitions and mergers.

Was this article helpful?

Related Articles

Leave a Comment

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