Often it is recommended to put custom functions into your theme’s functions.php file; and while that can work quite well, what if you change themes? Or what if you are on a multi-site installation and have a number of custom functions you want to use across sites that use different themes?
This article will discuss how to create a custom function file and load it as a plugin. This way your custom functions stay intact no matter what you do with your theme.
Creating the file
Start by creating a new php file. You can call it whatever you want but make it mean something to you, such as my-functions.php. In this new file, add the following:
<?php /** * Plugin Name: My Functions * Plugin URI: http://yoursite.com * Description: Custom functions that I use. * Author: Your Name Here * Author URI: http://yoursite.com * Version: 1.0 */ // Put your code snippets below this line.
Install your plugin file
Once you’ve created and saved your file, load it to your site. It should be saved in your /wp-content/plugins/ folder. You could also create a folder specifically for this file such as /wp-content/plugins/my-custom-functions/
When you’ve loaded the file to your site, you can now go to the plugin screen in WP and activate it like any other plugin.
Editing your file
While care should be taken to be editing functions on a live site, if you need to make a change or drop something in, you can still edit this file through WP like you would your theme’s functions.php. This can be done by going to Plugins > Editor and selecting your custom plugin file from the dropdown list of plugins.
Keep in mind, use caution when making an edit on a live site. If you don’t know what you are doing, it’s best to make changes locally and transfer your file via FTP so that you can back out of any change that you make in case you made a mistake.
There is now a “Site Specific Plugin” framework available that you can use to establish a plugin specific to your site. The framework is simple to use and all you need to do is install it and add your custom code. It also includes loading custom CSS and JS. More info here.