There are many tasks in WordPress that are extremely repetitive. Things that get coded over and over again by developers, even though the result is the same every time.
Custom Post Types are a perfect example. The code to make them is the same in the end, it’s the settings that change each time. Taxonomies and meta boxes are similar. This is why tools like GenerateWP exist. You can simply fill out a form and get a fully functioning code block.
Another way to manage this kind of code is with a library. A library is a piece of code that knows how to generate that code the same way every time, accepting only a few settings to build what you want.
Following the Custom Post Types thread, there’s an excellent PHP Library from John Blackbourn called Extended CPTs. If you include it with your plugin or theme, you can simply add a few lines of code and get a full Custom Post Type. It can also handle Taxonomies. Another great example is meta boxes. They can be quite complicated, but a library can standardize the code and help remove errors.
BUT! There's a bit of a problem.
Let's dive in and take a look at the problem and the solution.
When using a library, it must also be installed at the same time as your own code. There are two ways to do this.
- Include the code inside yours
- Ask the site owner to install it
The problem with the first of these is that there’s no mechanism for your own codebase to know if there’s an update to the library. Someone might go years with an old version, not knowing that there’s a security issue in the library.
The problem with the second is that as soon as you depend on the site owner to do something, they won’t. Then your plugin will “break” and they’ll blame you.
Software engineering has had a solution for years, it’s called dependencies. Some code in your own codebase goes looking for a library that it depends on before you call it, and if it’s not there, does something helpful instead of simply crashing.
Unfortunately, WordPress doesn’t have a dependency API built in. Fortunately, someone has written an excellent one, called TGM Plugin Activation. Despite its name, it also works with Themes. (Pro tip: TGM stands for Thomas Griffin Media, and anything written by Thomas Griffin is quality.)
In your own codebase you include a file with a PHP class in it that does all the work. Then include a small (usually) function that declares what plugins are required. Here’s an example:
When your plugin is installed, it lists at the top of the page what those packages are, and provides links to install them directly, so you don’t have to track them down. Here’s an example of that:
The process I described above may seem a little complicated, but it’s really not. Not only that. The TGM site has a generator to help make all the code and files that you need!
- go to the Installation page
- fill out the Generator form
- click Generate button
It will ask you to download a zip file that contains both the TGM class and a file built just for your codebase.
Re-writing code to accomplish a task when there’s a well maintained library available is rarely a good idea. Letting someone else take care of updates and security can be life saving. TGM Plugin Activation makes that process a smooth and safe one for both you and your users.