When I sat down to begin this post, I planned to write something far more involved that I what I’m going to share. Initially, I wanted to walk through one of two things:

  • The Complete Guide To Setting Up a Development Environment,
  • Integrating Code Quality Tools into PhpStorm

The first would be focusing on a variety of other things I’ve talked about, tying them all together, and having a definitive reference. But this is something that I want to take time to put together to make sure it’s done right.

The second is one that I think is important but I’m in a bit of a transitional phase with some of my own tools. Until that’s done, I’d rather not write about it.

Even still, there’s always something to cover, right? So today I’ve opted for something simpler: Breaking down the use of classes, templates, and partials in WordPress plugins using a simple example.

Classes, Templates, and Partials in WordPress

For this particular topic, an immediate question that may come to mind is simple: Why bother talking about this?

Because it’s 2018 and we’re still seeing a gross mix of PHP, CSS, markup, and JavaScript in a single file. This isn’t to knock on other technologies that do this by default (like React). I’m talking specifically about WordPress plugins and writing maintainable code in such a way that it’s easy to [obviously] maintain, write, and read.

A Practical Example

Let’s say that you’re working on a submenu page for something that’s going to appear under a custom menu. You register your page using the WordPress API:

But when you setup a callback function to display the page, you don’t use the function to mix all of the various languages together. Instead, you use it to include a file.

Note that in the file above, I reference a property $this->pluginPath. This is set in the constructor of the class so that I can easily access the root of the plugin.

Anyway, what might this file look like?

A Template and a Partial

In this case, I assume that your template is what’s going to provide information to the user and is going to request their input. The partial is going to be responsible displaying a success, error, or warning message to the user.

A Template

To keep it simple, I’ll keep both the template and the partial as lean as possible. In this case, assume that we’re going to render a page, ask the user to save a value, and if the value saves successfully (and thus exists in the wp_options table), we’ll display the success message.

This means:

  • the template will display the page title, information, input, and save button,
  • the partial will display the success message when necessary.

Take a look at the code for the template below:

Notice that, yes, it has an input, a save button, and a nonce all of which are important (but beyond the scope of this post) for saving information.

But also notice that it has a helper function that i uses to check for the presence of successfully saving information. This function resides in the same class responsible for rendering the template.

Partial

It looks something like this:

And the resulting partial is very simple:

Of course, your implementation of the function may vary. The purpose isn’t so much to show how to implement the function but how to leverage a function for checking information for displaying a partial.

 

There’s More (But Not Here)

Maybe this is something that should be placed at the beginning of the article.

To be honest, I think it’s one of those things where some may find it more helpful to see it from the outset (but then lack context of the code) and others find it more helpful at the end because they can see how it all fits together.

Regardless, I hope that it ultimately provides clarity on how all of this fits together.

Furthermore, there is a lot of room in places for things to be customized such as:

  • checking to make sure the user has permission to save,
  • verifying the nonce value,
  • sanitizing and validating the data,
  • determine what constitutes success, warnings, and errors.

But if I’m to cover all of the above, we’re looking at an extraordinarily long post or a long series of posts. That’s not something I’m against, but it’s also something that I don’t now is worth the effort at this point.

Never hesitate to offer feedback. But in the meantime, I hope this all helps give you a foundation off of which to build when working on your next project.