How to include custom code on your website

You can add pieces of PHP code snippets into your theme’s functions.php file. This is a common place to store custom code.

If you are unable to edit the file or do not want to touch your theme code you can use a plugin such as WPCode – Insert headers and footers – Free from WordPress.org.

This is a great little tool to add custom code to your website without editing any files in your theme or plugins.

Install the plugin

  • Log in to your website and go to “Plugins” -> “Add New”
  • Search for “my custom functions” and locate the plugin named “WPCode – Insert headers and footers”.
  • Install the plugin using the button “Install Now” and then wait a few seconds until you can click “Activate”.
  • After the plugin is activated, go to “Code Snippets”.
  • Here, you can copy-paste the code you need.

How to add a custom snippet

 

Go to “Code Snippets” and add a custom snippet.

Add new custom snippet

Add your Custom Code (New Snippet)
Add your Custom Code (New Snippet)

On the “Create Custom Snippet” page add a title so you can remember what the code is about later on.

Copy-paste the code.

Make sure to choose PHP Snippet – You can also use the plugin for other types of code, HTML, JavaScript and CSS. This makes it a perfect tool to handle different little features and tweaks vs. having a functions.php that are thousands of lines long or have a custom plugin.

Enable the snippet and press save.

If there are any errors, the plugin will automatically detect and disable the piece of code, preventing downtime for your website even if you make a mistake.

How to add a custom snippet to your website
How to add a custom PHP snippet to your website with a plugin

Was this helpful?

Next Article

Getting started