How to Pasting Snippets from the Web into WordPress
Are you interested in learning how to pasting snippets into WordPress?
For expert users, copying and pasting code into WordPress theme files may appear simple, but it might be scary for newbies.
We’ll show you how to copy and paste code from the web into WordPress without destroying your site in this article.
Why Should You Use Code Snippets on Your WordPress Website?
Because of its flexibility, WordPress is the best website builder on the planet. It’s quite simple to add new features and functionality to your site, and it can assist with the creation of practically any form of a website.
We provide a variety of ideas and tactics for improving your website. Some of these suggestions will require you to enter code into WordPress.
While adding custom code snippets to your website is optional, and you can often find plugins that do the same thing, a simple snippet can be more efficient in some cases.
Adding code snippets to your website is a simple approach to fixing many common WordPress mistakes, improving your WordPress security, and adding new functionality.
With that in mind, let’s look at how to safely incorporate code snippets into your WordPress website.
What Are the Best Methods for Adding Code Snippets to WordPress?
It’s worth noting that you should install and use a WordPress backup plugin before you start adding code snippets to your website.
This keeps your WordPress site safe, and if something goes wrong, you can always restore it from a backup.
You may find instructions for adding snippets to WordPress themes template files like index.php, single.php, and others when adding them to WordPress.
Because these snippets are only usable for those specific theme files, you’ll need to add them manually or build a child theme.
The majority of code snippets, on the other hand, will be inserted into your WordPress theme’s functions.php file. Some instructions may advise adding it to the file directly, but there are far superior options that will allow you to future-proof your changes.
Your unique functionality will not be lost if you change themes in the future.
With that in mind, let’s look at some of the easiest ways to add custom code snippets to WordPress for beginners.
1. Use the WordPress Plugin Code Snippets
The safest and most beginner-friendly approach to adding code to WordPress is to use a code snippets plugin.
You can quickly add and manage custom code snippets on your website with the Code Snippets plugin.
It has a fail-safe switch that deactivates a code snippet immediately if it creates an error. When adding a custom code snippet, prevents you from losing access to your website.
Note that the code snippets method is handy for adding snippets to the functions.php file. This strategy will not work if you are asked to include a code snippet in other theme files.
2. Save Custom Code by Creating a Child Theme
Creating a WordPress child theme is another wonderful way to add code to WordPress. When you update your theme, using a child theme will assist maintain any modifications you’ve made to your website intact.
Note that this method can also be used to add code snippets to other theme template files.
3. For custom code, use a site-specific plugin.
Using a site-specific WordPress plugin is another flexible option. This is a custom plugin you can make for your own website to save all of your custom code.
This solution has the advantage of not being dependent on your theme and remaining active even if you switch themes. Any WordPress updates on your website have no effect on it.
Note: This method is only for adding code snippets to the functions.php file.
4. To add custom code, use the WordPress Plugin Insert Headers and Footers.
You may simply need to add code to the header.php or footer.php files of your theme. This is made simple with the Insert Headers and Footers plugin.
This is useful when you need to add tracking code to your website for Google Analytics, Facebook Pixel, Google AdSense, and other services.
It also allows you to manage all of your header and footer scripts in one location, avoid manual errors, and update or modify your theme without worry.
This approach is best used to track scripts, custom CSS, and Javascript code.
4. Add Code to Functions.php File Directly
Finally, adding code snippets straight to your theme’s functions.php file is OK. However, because there are certain drawbacks, we recommend using the alternative solutions listed above.
To begin, all of your changes will be lost if you upgrade your WordPress theme.
Following that, the code you added will only operate if you are using that theme.
Let’s have a look at how to copy and paste code snippets correctly to avoid disrupting your website.
Editing and Adding Code to WordPress Files
Depending on how you want to add custom code snippets to your WordPress blog or website, there are several ways to change WordPress files.
1. Using the Code Snippets Plugin to Add Custom Code
If you utilize the Code Snippets plugin to add code to WordPress, you’ll be able to do it right from your WordPress admin panel.
You must first install and activate the plugin. See our beginner’s guide to installing a WordPress plugin for more information.
Simply navigate to Snippets » Add New in your WordPress admin dashboard after activation.
This will take you to a page where you can type in your custom code. Simply give your snippet a name to make it easier to remember, then enter your code into the box.
Then click the ‘Activate’ button to make your code snippet live on your website.
2. Using Custom Code in a WordPress Plugin for a Specific Site
If you’re using a site-specific plugin, you can add your custom code using the WordPress plugin editor.
To begin, go to Plugins » Plugin Editor and then select your plugin from the ‘Select plugin to edit:’ drop down option.
Your site-specific plugin will be loaded by the editor. Then all you have to do is add code to the page.
When you’re finished, make sure to save your changes by clicking the ‘Update File’ button.
The plugin editor will immediately undo your modifications if something is missing in your code or could break your website.
FTP is another option for adding custom code to a site-specific plugin.
Simply access your website in your favourite FTP client, then right-click on the plugin file and choose ‘View/Edit’ from the drop-down menu.
This will allow you to add your code snippets to the file. Changes will appear automatically when you save and submit the file again.
3. Using the Insert Headers and Footers Plugin to Insert Custom Code
You can add code snippets with the Insert Headers and Footers plugin right from your WordPress admin panel.
You must first install and activate the plugin. See our step-by-step guide on installing a WordPress plugin for more information.
After that, navigate to Settings » Insert Headers and Footers and paste your code snippet into your website’s header, body, or footer sections.
Make sure to save your changes, and your code snippets will appear on your website.
4. Customizing Functions.php or Other Theme Templates with Custom Code
If you’re directly adding code to your theme’s functions.php file or any other page template, go to Appearance » Theme Editor in your WordPress admin panel.
The file will open in the editor after you choose it from the right-hand column.
If the tutorial you’re following doesn’t specify where you should put the code snippet, you should put it at the bottom of the file, beneath all the other code.
Another option is to add custom code to your theme files via FTP through your WordPress hosting file manager.
Connect to your website using your FTP client, then navigate to wp-content » themes » your-theme-folder and right-click on the file that has to be edited.
Then select ‘View/Edit’ to open the file in a text editor and paste your code snippet in there.
When Adding Custom Code, Troubleshooting PHP Errors
Beginners frequently make blunders while adding custom code snippets to their WordPress websites. Fortunately, the majority of these mistakes may be readily prevented and corrected.
Let’s look at these issues and the best strategies to resolve them.
1. Misuse of the PHP Begin and End Tags
WordPress is mostly developed in the PHP programming language, which has a unique syntax that instructs your server to process the following code. A common PHP code snippet appears like follows:
1
2
3
4
5
6
7
|
// PHP Begin Tag <?php // Rest of the code goes here // PHP End Tag ?> |
In scripts that transition back and forth between PHP and HTML, the PHP end tag is critical. This applies to most WordPress theme files that include PHP tags in addition to HTML.
If you’re pasting your code somewhere where the PHP start tag isn’t closed, you’ll need to remove the starting PHP tag.
1
2
3
4
5
6
|
<?php // Some pre-existing code // your custom code ?> |
You must also include the PHP begin tag if you paste your custom code outside or after the PHP end tag.
1
2
3
4
5
6
7
8
|
<?php // Some pre-existing code ?> // Your custom code snippet <?php ?> |
Incorrectly placed PHP start and end tags account for over 90% of all problems. Examining your code will reveal whether or not the PHP start and end tags are required in your custom code snippet.
Many WordPress theme files, such as functions.php, may not even have a PHP end tag. This means you can write your code without using the start or end tags at the bottom of the file.
Here’s an illustration of what that may look like.
1
2
3
4
5
6
7
8
9
10
11
|
<?php // Lots of code in your theme's functions.php file // // // Your custom code function custom_loginlogo() { echo '<style type= "text/css" > h1 a {background-image: url( '.get_bloginfo(' template_directory ').' /images/login_logo.png) !important; } </style>'; } add_action( 'login_head' , 'custom_loginlogo' ); |
Keep in mind that some tutorials presume you’re already familiar with PHP start and end tags. As a result, they might just show you a code excerpt without the tags.
1
2
3
4
5
6
|
function custom_loginlogo() { echo '<style type= "text/css" > h1 a {background-image: url( '.get_bloginfo(' template_directory ').' /images/login_logo.png) !important; } </style>'; } add_action( 'login_head' , 'custom_loginlogo' ); |
When code snippets are offered, they can sometimes be placed in many places, therefore the PHP end and start tags are not included.
If you’re going to include a code snippet in your theme files, make sure it’s inside the PHP tags.
2. Misplaced Nesting Errors
Functions, conditional logic, and loops all have their own syntax in PHP. Curly brackets are used in this syntax to denote when a function starts and stops.
Here’s an example of a simple PHP function:
1
2
3
4
5
|
<?php function wpbeginner_tutorial() { echo "Hello World!" ; } ?> |
If you want to include a custom code snippet that has nothing to do with this method, you must do it outside of it, like follows:
1
2
3
4
5
6
7
8
9
10
11
12
|
// Pre-existing code in your theme file <?php function wpbeginner_tutorial() { echo "Hello World!" ; } // Your custom code function custom_loginlogo() { echo '<style type= "text/css" > h1 a {background-image: url( '.get_bloginfo(' template_directory ').' /images/login_logo.png) !important; } </style>'; } ?> |
If you leave the starting or closing curly brackets out, the code will be broken, and you’ll get an error page.
Understanding WordPress PHP Errors
Errors generated by custom code in WordPress are frequently accompanied by a comprehensive error message. The majority of them are syntax, parsing, or catastrophic mistakes caused by unexpected characters.
The good news is that these errors will inform you which line of code in your program caused the problem.
Then you may go to the exact line and look at the code to see what you missed.
We recommend using a good text editor for code editing because they contain line numbers and syntax highlighting, which can assist you in quickly resolving the problem.
What Should You Do If Your WordPress Site Isn’t Working?
First and foremost, take a deep breath and relax. You still have access to all of the files on your website.
Simply use an FTP client or the file management software in your WordPress hosting account’s cPanel to connect to your website.
Next, open the file where you put the code that produced the problem and make any necessary changes.
Then you can use the code snippet to try to fix the problems. If you’re still having problems, simply remove the code piece you added and save your changes.
Your website should now be back to normal. If the error persists, download a new copy of your WordPress theme and extract the zip file to your PC.
Then, while overwriting the old file, identify the file where you made the changes previously and upload it to your server.
See our guide to the most common WordPress mistakes and how to fix them for more information. If it doesn’t work, undertake a step-by-step diagnosis using our WordPress troubleshooting guide.
We hope that this post has taught you how to put code snippets from the internet into WordPress.
Comments are closed.