WordPress custom functions

A Guide to Using Custom Functions Plugins in WordPress

Learn how to create WordPress Custom Function Plugins and add custom code to your theme, a dedicated plugin or managing them via your WordPress dashboard.
Table of Contents

Sponsored Ad

If you purchase through a link on our site, we may earn a commission.

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.

Custom code added to the functions.php file via the Theme File Editor in WordPress dashboard.
Custom code in the theme’s functions.php file via the Theme File Editor.

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:

  1. The code relates to the custom or child theme.
  2. 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.

A WordPress custom function plugin enabled in the Plugins menu.
Our custom function plugin in the Plugins section.

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.

List of code snippets in the Code Snippets plugin.
A list of custom code snippets.

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.”

Example of a custom code snippets on the Code Snippets plugin page.
This example shows a custom code that displays the date of the last update below posts.

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.

A Must Use WordPress custom function plugin listed under Plugins in WordPress admin.
Must Use plugins have their own dedicated tab in the Plugins section.

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.

Thomas Maier grew his publishing business to over 40 MM page impressions, developed the popular Advanced Ads plugin for monetization, and now enjoys programming again by focusing on his Image Source Control plugin.

Sponsored Ad

If you purchase through a link on our site, we may earn a commission.

All suggestions are anonymous.

More from our blog...

4 Responses

  1. This is such a cool idea, and one I can’t wait to implement. Wish i had this a while ago… (been copying and pasting all sorts of snippets… or keeping them in Evernote… sigh…).

    Say Jean… any chance you’d be willing to share some of your more commonly used snippets / functions / plugins with some of us up and coming devs???

    thanks so much,
    eric

    1. I’ll try to write a post with some of the ones I use most, or maybe release a framework at a later stage, thanks for the idea Eric.

  2. WOW, very interesting article and a new technique I haven’t seen before. I’ll have to read it throughly to start adopting it in future projects.

    Thanks alot for sharing πŸ™‚

Post a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Stay updated with WP Mayor's newsletter showcase every week

Stay on top of every new WordPress innovation and latest launches. Receive all our fresh product reviews and expert guides directly in your inbox.

Hosting Survey 2024

Are you happy with your hosting provider or are you over-paying for too little? Have your say below!

"*" indicates required fields

What's the main reason you picked this host?*
How happy are you with your host?*

OPTIONAL: If you'd like to receive the results of this survey, please enter your details below.