Handy Underpin Helper Functions That Make WordPress Development Easier

Underpin is a powerful WordPress plugin framework that can make WordPress plugin development faster, more-consistent, and easier to scale. But it also introduces some unique helper functions that can make your life easier. This post gets into a few of my personal favorite Underpin functions.

Make_Class

Fundamentally, Underpin is a big registry of PHP classes. When registering anything in Underpin, you’re actually registering a PHP class. For example, when registering a new custom post type, you could do something like this:

And reference an Underpin Custom_Post_Type class:

Underpin uses this basic pattern for absolutely everything it does. Shortcodes, blocks, scripts, stylesheets, custom database tables, batch tasks, whatever – it all gets registered against a loader (the custom_post_type() part of the function above), and is registered by providing a specified class to add. The problem is, this is really verbose, and in some-cases it’s downright overkill to have to create a whole new PHP class. Because of this, it is possible to register just about anything in Underpin using an array of arguments that gets converted to a class instance for you. This makes it possible to register things in a much-more succinct manner without sacrificing the power of object-oriented programming. Kind-of like this:

Or, if you want to specify your own PHP class to use instead, you can do that like this:

All of this flexibility comes from a static method called Underpin::make_class(). This method will take the arguments given to it, and will either create the specified classinstantiate the class from a string, or create the default class when no class is specified. This is really handy when you’re making your own loaders, and provides an easy, consistent way to allow people to extend your plugin.

Filter

As mentioned before, Underpin is fundamentally a big ol’ registry of PHP classes. Along with registering items, it also comes with ways to retrieve items from the registry quickly and easily. The filter method makes it possible to get a subset of registered items in Underpin’s registry. This allows you to-do a lot of useful things with the registry.

For example, let’s say you wanted to get all of the registered REST endpoints with a namespace of plugin-name/v1, you could do something like this:

And $rest_endpoints would become an array of Rest_Endpoint classes that have a rest_namespace set to plugin-name/v1.

Another example – let’s say you have a custom admin interface, and you want to make it possible to enqueue custom scripts on this page easily. You could add a custom parameter called screen to your registered Script classes, filter against that, and enqueue the filtered scripts.

There are so many ways you can use the filter method to your advantage. When used correctly, this functionality can completely remove the need for a hook, and instead allow people to simply register to your plugin, and let the plugin actually do what it needs to do with the registry.

Find

Like filterfind allows you to get a subset of items from the registry, with a key difference – this will always return the first registry item it finds. In some cases, you know that a registry value is unique, and you just need to retrieve the class based on that value.

In my beer plugin course, we create a custom registry of SRM values. Each SRM value has an SRM ID (1 through 40) and a color associated with it. Because of how SRM values work, we’re able to use the find class to get the SRM color based on the SRM ID, like this:

In the example above, $color would either be a hex color value, or an WP_Error object. This is because find will return a WP_Error object if it could not find anything from the value. You can use this to check the registry to see if certain items exist.

For example, let’s say you created a custom registry called Integrations in your plugin, and third-party plugins register themselves against your plugin through this registry. Perhaps they do something like this:

This gives you an easy way to check to see if the integration is active, like-so:

Export

When you have a distributed plugin that you support, it’s important to have a way to get information about the installation you’re trying to support. This varies wildly from plugin to plugin, but as support tickets flow in, you start to learn what information you need in-order to solve problems quickly. Underpin has an export API in-place to help make getting that information easier.

By default, Underpin has a built-in export function that will dump an array of all registered items in your plugin. When this is combined with error logs, you can get a good grasp on everything about this plugin.

Over time, you’ll find that certain pieces of data are extra-important, and you’ll probably find that you need more information about certain things. To help with this, each Underpin loader has its own export_registered_items method that gets called when this exporter runs, and because of this, each loader can customize what they export. This gives you control over how the resulting export will look.

Exporting everything would look something like this:

Or, you can export just a specific loader registry, if you’d rather do that, like so:

Conclusion

This is far from an exhaustive list of the tools and methods available to you when using Underpin, but it is a solid start. If you’re looking to learn more about how to use Underpin, I suggest that you check out this plugin development course, or look at Underpin’s documentation here.

Comments

Leave a Reply

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