Nexcess Blog Logo
September 11, 2018

The Right Way To Add Custom Functions To Your WordPress Site

WordPress is rightly famed for the vast array of plugins and themes it makes available to site owners. If you want to add a feature to your WordPress site, you will almost certainly find a plugin that does the job.

But, on occasion, you may find yourself in need of a minor tweak or piece of bespoke functionality that isn’t available as a plugin. The solution is to add a snippet of custom code to the site. WordPress is a PHP application and WordPress plugins and themes are written in the PHP programming language. As a WordPress hosting client, you have access to the same hooks and tools WordPress developers use.

You don’t even have to be a PHP expert to do this. There are thousands of pre-made snippets around the web that you can adapt to your own purposes. Take care though, there are security implications to adding code to your site and badly written code can stop your WordPress site from working altogether. Make sure you know what a function does and that it is compatible with your version of WordPress before you add it to your site.

Once you have discovered the need for a function and written it from scratch or adapted a prewritten function, where should you put it?

There is a wrong way and a right way to do this. If you do it the “wrong” way, your function may work initially, but it is likely to stop working when you update your site.

How Not To Add Custom Functions to WordPress

The two most common “bad” ways to add custom functions to WordPress are editing an existing plugin or editing the functions.php file.

Don’t edit plugin files. If your snippet changes the functionality of a plugin, it might seem sensible to add the new code directly to the plugin. But, when you update the plugin, the files you have changed will be overwritten and your code will disappear.

The functions.php file is not a general purpose dumping ground for custom code. The functions.php file belongs to your theme. If the code you want to add is theme-specific, then functions.php is a good place to put it. But, when you switch themes, the new theme will not have the custom code. Avoid putting general-purpose custom code in functions.php.

  Get started with WordPress hosting and make development easier. 

The Right Way To Add Custom Functions

There are a couple of ways to add custom functions to WordPress that will last beyond your next update or theme switch.

The Code Snippets Plugin

The Code Snippets plugin is designed for exactly this purpose. It provides a graphical interface for adding code snippets to a WordPress site. You can add as many snippets as you want, enable and disable them easily, and export them in a format that can be imported into other WordPress sites with the Code Snippets plugin.

The Beginner’s Guide to WordPress Performance Optimization >>

Build A Custom Plugin

You might find the idea daunting, but it is not difficult to build a custom plugin that can be installed on a WordPress site alongside third-party plugins.
The basic structure of a minimal plugin looks like this:

  • A folder with the same name as your plugin, e.g. my-plugin. This is not essential but it’s useful if you want to add more files in the future.
  • A PHP file inside that folder called my-plugin.php

In the my-plugin.php file, add the following text:

Plugin Name: Example Plugin

That is essentially all you need to create a plugin, although it won’t do anything yet. To make it useful, you need to add your custom function to the PHP file and then upload the folder to the plugin directory of your WordPress site, usually wp-content/plugins/.
If you need to add new functions, you can simply overwrite the old version with your changes.

Accelerate WordPress With Nexcess Fully Managed Hosting

If your WordPress host is not providing you with the speed you need, consider fully managed WordPress hosting from Nexcess.

Nexcess can make your WordPress site faster with:

  • A built-in CDN with 22 locations.
  • Advanced caching for ultra-fast loading.
  • Image compression to lower browser load times.
  • And much more.

Get started with managed WordPress for free. Start your free two-week trial today.

Avatar for Nexcess
Power up your sites and stores with custom-built technology designed to make every aspect of the digital commerce experience better. Make your digital commerce experience better with Nexcess. Visit today and see how we can help.