How to Add Scripts to your WordPress Website

You may need to add some scripts to your WordPress site to improve the functionality, make modifications to your website’s appearance or add new features to the site particularly if you are using 3rd-party services such as Google Analytics, CRMs, Ad Snippets, Tracking Pixels, and more. There are many ways to add scripts to your site:

  • There are some third-party plugins that allow you to add scripts to your site, but it might be difficult to locate one that perfectly meets your needs. Third-party plugins might potentially pose a security risk if they are not kept up to date on a regular basis.
  • You may copy/paste them directly into your website by editing your theme.

Adding Script to Your Site with Header Footer Code Manager

When modifying your website’s core code, even a single error might cause your site to cease operating entirely. Downtime might result in lost clients. When you edit the code, it is critical to pay strict attention to detail. You should always keep a backup of your site, if anything goes wrong you can restore the previous state by restoring the backup. There are many ways to take backup of a WordPress site.

If you use a plugin, it will add your scripts to your site. You don’t have to go and edit the theme files and mess up the site. The best plugin for this job is Header Footer Code Manager. You can add your scripts easily to your website with HFCM and manage them. Let’s see how you can do that.

First, you need to install and activate the plugin.

 

 

  1. After installing and activating the plugin, navigate to HFCM on the left navbar in the WordPress Dashboard area.
  2. Click on HFCM > Add New
  3. Enter a suitable name for your snippet, which would help you distinguish it from other snippets. For Example: Google Analytics Snippet (if you are entering a google analytics code snippet)
  4. Select the appropriate Snippet Type (HTML/CSS/Javascript, or PHP [PHP in PRO VERSION ONLY]).  If your snippet has opening and closing “<script>” and “</script>” tags, most likely your snippet is Javascript.  If your snippet has opening and closing “<?PHP” and “?>” tags, then most likely your snippet is PHP. Adding the PHP opening and closing (“<?PHP” and “?>”) tags are optional. IMPORTANT NOTE: If you select PHP (Pro Version Only) – Please make sure the code you’re adding is absolutely correct. Adding bad or improperly written code may result in site crashes causing you to be locked out of your site. Do this at your own risk.
  5. Choose the display options as per your requirements, and then add the code snippet in the Snippet / Code textarea.
  6. Click on the Save button to save your snippet to the database. You can navigate to HFCM > All Snippets to view/edit/delete your saved snippet

 

 

You need to put your code in this box and save it. Then turn on the snippet and that’s it. The snippet will now show in the header of the site.

Inserting A Script by Editing Your Theme

You can use one of the built-in WordPress options to edit your theme and add the script to your functions.php file.  This method can break your site though if you don’t put in the exact code that is necessary.  An errant period or comma may completely break your site and that is why this method is not recommended.

Conclusion

Be very careful about copying snippets from sources you don’t trust. Always take backup of your site and if you don’t know what the code is doing it’s better not to use it. Malicious codes can harm your website.

Sign Up For Our Newsletter

This field is for validation purposes and should be left unchanged.