We use cookies to understand how you interact with our site, to personalize and streamline your experience, and to tailor advertising. By continuing to use our site, you accept our use of cookies and accept our Privacy Policy.

Your Digital Commerce Experts
Nexcess Logo

WordPress Functions: How to Create Custom Function PHP in Wordpress

September 11, 2018
WordPress Functions: How to Create Custom Function PHP in Wordpress

WordPress is rightly famed for the vast array of WordPress 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 need to tweak a WordPress function. Sometimes there isn't a plugin available to do this. The solution is to add a snippet of custom code to the site. Learning how to create custom function PHP in WordPress will be key.

Related reading: WordPress Cleanup: 9 Easy Ways To Clean Up and Optimize Your WordPress 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 WordPress 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 WordPress function may work initially, but it is likely to stop working when you update your site.

Related reading: How to Customize a WordPress Theme >>

The Wrong Way to Create Custom Function PHP in WordPress

You want to learn how to create custom function PHP in WordPress. When creating custom WordPress functions, don't use these two methods. 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.

The Right Way To Add Custom WordPress 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 WordPress functions, you can simply overwrite the old version with your changes.

Power Your WordPress Site With Fully Managed Hosting

If your WordPress host is not providing you with the speed, scalability, and support 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 fully managed WordPress. Check out our plans to get started today.

Nexcess
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 Nexcess.net today and see how we can help.