Hello everyone! Today's tutorial focuses on teaching you how to safely paste various custom code snippets into WordPress. Here are some of the main things that I'd like to share with youOur TeamExperience in pasting custom code snippets.
What is the best way to add code snippets to WordPress?
As before, before you start adding code snippets to your WordPress site, you should make a change to theWordPress Website BackupThe
This keeps your WordPress site safe and you can always restore your site from a backup if something goes wrong.
When adding code snippets to WordPress, it is generally possible to find instructions for adding code snippets to WordPress theme template files (e.g. index.php, single.php, etc.).
These snippets are only useful for those specific topic files, so adding them directly orCreating a WordPress child themeThe
However, most code snippets are added to the WordPress theme'sfunctions.php filein the file. Some tutorials may add code snippets directly to the file, and those skilled in the operation can do so. However, our tutorial is for newbies, so it will use theWordPress PluginThe
This way, if the theme is changed, the customization will not be removed.
There are various ways to edit WordPress files, depending on the method chosen to add custom code snippets to your WordPress site.
Method 1: Add custom code using the WPCode plugin (simple)
Using the Code Snippets plugin is the safest and most novice-friendly way to add code to WordPress.
WPCodeThe plugin makes it easy to add and manage custom code snippets on your website.No need to edit theme filesThe
It hasIntelligent Code Fragment Verification Function, can help avoid common code errors. This prevents the website from being affected when code snippets are added.
WPCode also has a built-in code library that makes it easy to find all the most popular WordPress code snippets. Add these snippets with a few clicks.
The best part is that it allows administrators to manage all code snippets from a central screen and even organize them using tags.
Attention: The WPCode method is useful to add to thefunctions.php
Code snippets in files are useful. This method is used if code snippets are requested to be added to other theme files.
How to Edit and Add WordPress Code with WPCode
If you want to add code to WordPress using the WPCode plugin, then you can add the code directly from the WordPress admin dashboard.
First, install and activate the free WPCode plugin.
Once activated, go to the WordPress Admin Dashboard in the"Code Snippet""+ Add Code Snippet. Then, click"Add new"Button.
This will take you to a screen where you can select a clip from the pre-made library or add custom code.
To add a custom code, click "Add custom code (new code snippet)" under the optionUse the code snippet button.
On the next screen, start by naming the code snippet to make it easier to remember it. Then, paste the code into the box. Select the correct code type from the drop-down menu on the right.
Next, scroll down to "Insert "Sections. Here, there is an option to use the code snippet as a shortcode and then manually copy and paste it anywhere on the site.
Or, alternatively, there is the option of "Auto-insertion" method to select where to automatically insert the code snippet (WordPress feature).
Selections can be made from positions such as Run everywhere, Front end only, Administrator only, Site wide title, After post, Between post, Before paragraph, etc.
The best thing about WPCode is that in addition to the various auto-insertion options, it comes with conditional logic options for novices. This allows to choose when to load the code.
After adding the code, change the switch in the upper right corner from "inactive"Switch to"maneuver".
Then, clickSaving code snippetsButton.
Once a code snippet is active, it will be automatically added or displayed as a shortcode, depending on the insertion method chosen.
How to tell WordPress Header and Footer Add Code
Sometimes it may only be necessary to address the subjectheader.php
maybefooter.php
The WPCode plugin can also help with this.
When it comes time to add aGoogle AnalyticsThis will come in handy when it comes to tracking codes for Google AdSense, etc.
In addition, it manages all header and footer codes in one place, preventing any manual errors and upgrading or changing the theme without losing it.
Attention:This method is best suited for tracking scripts, custom CSS and JavaScript code.
switch toCode snippet "Header and footerThe code snippet is then entered into the header, body or footer section of the website.
Click "Save Changes" button and then the code snippet will be displayed on the website.
methodologies2: Add custom code to site-specific WordPress plugins
Another flexible option is to use a site-specific WordPress plugin. This is a custom plugin that allows you to create it for your own site and use it to save all your custom code.
The advantage of this method is that the code is not theme dependent and it remains active even if the theme is changed. It is also not affected by any of the site'sWordPress UpdatesThe impact of the
Attention:This method only works if you need to add to thefunctions.php
code snippet of the file.
If using a site-specific plugin, then you can use the built-in WordPress plugin editor to add custom code.
switch toPlugins "Plugin Editorand then from the labeled "Select the plug-in to be editedSelect the plug-in from the drop-down menu in the "Plug-ins:" section.
The editor will load the site-specific plugin. Then, just add the code to the page.
When finished, click "Updated documents" button to save the changes.
If something is missing from the code or could break the site, then the plugin editor will automatically undo the changes.
Another way to add custom code to a site-specific plugin is to use FTP.
Open the site with an FTP client, then right-click on the plug-in file and select "View/Edit"Options.
This will open the file and make it easy to add code snippets. When you save and upload the file again, the changes will be displayed automatically.
methodologies3: Add custom code to Functions.php or other theme templates
Finally, code snippets can be added directly to the themefunctions.php
filein. However, we recommend the use of other methods as this has some drawbacks.
First, if updating yourWordPress ThemesThen all the changes will be gone.
Next, add code that will only work if that particular theme is used.
We look at how to copy and paste code snippets correctly and avoid breaking the site.
If adding code snippets directly to a theme file or any other page template, then navigate to the WordPress admin panel'sAppearance" theme editorfunctions.php
to add code.
Then, select the file from the right hand column and it will open in the editor.
Depending on the tutorial you pick will tell you where you can add the code snippet, if not then the code needs to be added at the bottom of the file underneath all the existing code.
Another option is to add custom code to the theme files using the FTP of the WordPress hosted file manager.
Connect the FTP client to the site and go to thewp-content " themes " your-theme-folderand right-click on the file you need to edit.
Then, click "View/Edit" option opens the file in a text editor and adds the code snippet.
Resolving PHP errors when adding custom code
Newbies make some mistakes when adding custom code snippets to WordPress sitescommon error. The good news is that most mistakes can be avoided and fixed.
Let's take a look at these errors and how to fix them.
1. Incorrect use of PHP start and end tags
WordPress is written primarily in the PHP programming language, which has a specific syntax that tells the server that the following code needs to be processed by PHP. A typical PHP code snippet is shown below:
All PHP code needs to be placed in the<?php
cap (a poem)? >
Tagged within.
PHP end tags are important in files that switch back and forth between PHP and HTML. This includes most WordPress theme files that use PHP markup and HTML.
Be sure that if you paste code into a location where the PHP start tag is not turned off, you need to add code without the start PHP tag.
If the custom code is pasted outside or after the PHP end tag, then the PHP start tag will also need to be added.
The 90% errors are all caused by incorrectly positioned PHP start or end tags. Looking at the code will help you see if you need to add PHP start or end tags to your custom code snippets.
However, many WordPress theme filesfunctions.php
There may be no PHP end tag at all. This means that code can be added at the bottom of the file without the need for a start or end tag.
Here is an example.
Because some tutorials assume that you already know how to use PHP start and end tags. Therefore, the tutorial may just show a code snippet without these tags.
Sometimes, when code snippets are given, they can be added to more than one location so that the PHP end and start tags are not included.
When adding a code snippet like this to your theme file, make sure it's located inside a PHP tag.
Understanding PHP Errors in WordPress
Errors generated when adding custom code to WordPress usually display detailed error messages, most of which aregrammatical error, parsing errors, or due to unexpected charactersfatal errorThe
The good news is that these errors tell you which line in your code is causing the error.
Then, you can go to the exact line of code to examine the code and find out what is missing.
Here, we recommend using the appropriateText editor for code editing, because they have line numbers and syntax highlighting that can help solve problems.
If you use the WPCode plugin, it also comes with an accurate error report that shows the exact line where the error occurred.
When WPCode discovers that it is blocking administrators from accessing the siteManagement areaIt automatically disables the code snippet in case of an error, making it easy to fix the problem safely.
What to do when a WordPress website is not accessible?
Don't panic. All the files of the site are still there and you can access them.
Simply use the FTP client orcontrol panelThe File Manager application connects to the website.
Next, locate the file that added the code that caused the error and open it for editing.
Then, you can try to fix the problems in the code snippet. If the problems cannot be fixed, theRemove the added code snippet, just save the changes.
The site should now be back to normal. If it still shows the error, download a fresh zip file of the WordPress theme and extract it to your desktop.
Then, locate the previously changed file and upload it to the server, overwriting the old file.
For more information on ways to address these issues, see our article on theCommon WordPress Errors and How to Fix ThemThe