A Guide to Using Custom Functions Plugins in WordPress

Over a decade as a WordPress backend developer, I have used various methods to adjust WordPress to my and others’ needs. The most common one is by writing WordPress custom function plugins. As you will see below, this is not the only option and not always the right one. While I dedicate this post to developers, project managers will also learn how these methods match their team’s needs and when and how to use them. These are the methods that I am using to customize WordPress: Add your code to the theme’s functions.php Write a WordPress custom function plugin Use a plugin to manage custom code snippets Bonus: Must Use plugins Let’s take a deeper look at each of these methods. Add code to the functions.php In the “old” days, when WordPress mainly used PHP throughout frontend and backend, the quickest way to make some changes to your site was by adding the code to the theme’s functions.php. You can still do this, and if you dare, use the Theme File Editor in the WordPress dashboard for it. Over time, this went from my preferred way of customizing WordPress to my least favorite one. The main reason is that the functions.php is attached to a theme. If that is not a custom theme and someone updates it, my adjustments are gone. The solution is to create a child theme that contains your changes related to the parent theme. My main reason for not adding as much custom code to the functions.php file anymore is that, in reality, most of this code was plugin territory, adding functionality to the site in general and not to the theme only. There are still two situations in which I write custom code into the functions.php : The code relates to the custom or child theme. Testing quick changes before I move them to another place since the functions.php is often easy to access for this. Also, keep in mind that the functions.php is not the place to host many changes since a single file can become overly complex. Especially when needing multiple PHP files and other file types in my changes, I opt to write a WordPress custom function plugin. Writing Custom Function Plugins As mentioned earlier, most of the code that I add to my site is actually building or extending functionalities that are independent of the theme. Once I know I will add more customizations to a site, I start a custom function plugin. It usually carries the site’s name, like I would call one for WP Mayor, the “WP Mayor Custom Functions” plugin. Naming is crucial! First, it allows other admins to see that this plugin relates to the site and probably hosts only changes for it. Second, keeping the plugin folder and filename unique will prevent your WordPress site from accidentally overwriting it with a plugin that has the same slug in the wordpress.org plugin repository. Creating a WordPress custom function plugin is easy. All you need is one PHP file within wp-content/plugins . However, you should create a new folder first since you often need more than one file to structure your code and might want to add files for JavaScript and CSS code. Following our example, let’s say we now have the wp-content/plugins/wp-mayor-custom-functions/wp-mayor-custom-functions.php plugin folder and main file. Let’s add a header to the file to tell WordPress what our plugin is about. While the “Plugin Name” is enough for it to work technically, let’s take a look at a more advanced example: /**
* Plugin Name: WP Mayor Custom Function
* Plugin URI: https://wpmayor.com
* Description: All the cool features we custom-built for us.
* Version: 1.0.0
* Requires at least: 6.2
* Requires PHP: 8.1
* Author: Thomas Maier

This should already show up under “Plugins” in our WordPress dashboard. Now, we can go wild and add actual PHP code to our file. Tip: when I don’t have FTP access to a site or am just lazy, I use the Pluginception plugin to quickly spin up something that I can later fill with code via the Plugin File Editor in the WordPress dashboard. Should you now put all the code for a specific site into a single dedicated custom function plugin? It depends. When I know the custom code is used on multiple projects, I separate it into a different plugin. At the same time, I gather code that is dedicated to a specific site in a single plugin. The fewer plugins, the fewer code repositories I need to maintain. Sounds great so far? Well, while managing a larger plugin company over the last years and making over a hundred adjustments to the site, I noticed that these custom plugins are still very developer-focused. So, let’s look at another approach in the next section. Using a custom function plugin for code snippets For most of my time in large plugin businesses, I was responsible for the technical side of our plugin store. I put any code that was not theme-specific into a single custom function plugin. All the adjustments to individual plugins and customizations, like redirecting empty cards to the pricing page, upsell pitches, or Black Friday offers, were documented and spread over various files with readable names. Well, “documented” and “readable” for anyone with access to the code. Even though I was always around to make adjustments or answer questions about them, I felt this was not an efficient and sustainable way to handle most custom functions. With more and more people involved in managing the store and website, I wanted to give them a chance to know about the adjustments without having to keep a dedicated page in our knowledge base up to date. The solution was to install a WordPress custom function plugin that manages all or most of the code snippets in the WordPress dashboard. My personal favorite is the free Code Snippets plugin, but there are others around as well. With Code Snippets, there is a dedicated menu item in the dashboard with all the snippets. Now, anyone with admin access can see the adjustments. I use descriptive names for each snippet, use the notes field to describe it in depth, and assign tags to group changes. My tags are usually the name of the plugin or theme that is adjusted and the section of the page this concerns, like “WooCommerce” and “Checkout.” If you choose good names and a good structure, anyone should be able to find and understand adjustments. Myself included. I use this method even on my personal sites since it provides so much comfort. Using a custom function plugin is especially useful for code snippets you only need temporarily, like some cleanup code that only needs to run once every now and then or some frontend-facing action like enabling a promotion. Is this safe? By default, WordPress allows anyone with the “admin” role to edit plugin and theme files in the backend. Using a custom code plugin isn’t different from that. So, the old rule to be defensive and only give everyone the needed role still applies. Developers might want to know that some very early hooks on a page request are unavailable when managing code through a plugin since the plugin itself needs to load first. If you need your custom code to load before any other plugin, check out the Must Use plugins option below. Custom WordPress functions in Must Use plugins So-called “Must Use” plugins are a special breed among plugins. Since they execute before any other “normal” plugin, I use them to turn on or off specific plugins for a given page. You could use such code to speed up pseudo APIs or calls to admin-ajax.php . Another feature of Must Use plugins is that they are enabled by default, and no one can disable or change them through the WordPress dashboard. This makes them ideal for essential code that not even admin users should be able to access. Creating a mu-plugin Must Use plugins are hosted in the folder wp-content/mu-plugins . This folder does not exist in a new WordPress installation. Some plugins or hosting companies might add content here dynamically, though. To add a new plugin in the mu-plugin folder, you have to create a PHP file on your computer with the plugin file header I showed you above for our custom plugin. This time, it is not important to choose a unique name since Must Use plugins are not updated automatically. Once you are happy with your new custom function plugin, manually upload it via FTP to the wp-content/mu-plugins folder. Create the mu-plugins folder if it doesn’t exist. While you cannot change Must Use plugins through the WordPress admin panel, you can see them listed under Plugins > Must-Use. Ideally, Must Use plugins are single-file plugins. WordPress cannot find them in subfolders, though you could reference additional files in the main PHP file. But if you create a complex Must Use plugin, think about moving the main code into a regular plugin and use the MU plugin only to load what is needed to run before all other plugins. Conclusion We’ve explored the various methods for adding custom code to WordPress, from the traditional addition of code to a theme’s functions.php file, standalone plugins, and WordPress custom function plugins to the use of Must Use (mu-plugins). Each method offers its own set of advantages and potential drawbacks, tailored to different needs and scenarios. As you can see, this has been a journey for me as a developer and project manager. I still use all methods nowadays, depending on the type of change and who should have access to it. I hope this article helps you, my fellow junior and senior developers, make your own decisions about the right way to add code as a custom function plugin in WordPress. Let me know in the comments if I missed anything.

This content was originally published here.