A little over a week ago, I shared a post about one way I try to work well with designers when working with WordPress. That is, I talked about how of stubbing out files they may need in order to get their work done.

Starting Up a Plugin

As mentioned in the post:

There are plenty of tips that can be shared and I’ll probably share more in the future.

So I thought I’d try to make this a regular thing by continuing to share some of the things I’ve learned – both what to do and what not to do – when partnering with designers or I’ve learned when simply working alone or with others on WordPress projects.

And one of the things I’ve found to be helpful is to provide helper functions when needing to call into more complicated functionality.

WordPress Helper Functions For Designers

Generally speaking, I think most programmers are familiar with helper functions. Though you may define it differently, I generally think of them like this:

  • Functions that are responsible for breaking down larger tasks into smaller, more readable (and more testable) pieces.
  • Functions that wrap other more complicated functionality for a caller to use.

When working with designers, I’m talking more about the latter than the former.

A Front-End Class For Meta Boxes

Let’s say, for example, you have a class that represents the front-end functionality of a meta box that you’ve built.

That is, the class provides all of the necessary functionality to:

  1. Retrieve the post meta data
  2. Determine if there’s data to be displayed
  3. Gracefully handle any errors it finds
  4. Parse out any unnecessary to render on the front-end
  5. Return the data so it’s ready to be handled by a caller

Of course, if your primary goal is to display something on the front-end, then having a single function to call (one like we’re all familiar with such as get_the_ID()) is far more helpful.

The Helper Function

So for all of the functionality above, we could take it a step further and write a helper function that does the following:

  1. Instantiates (or retrieves an instance) of the class that’s mentioned above
  2. Passes the necessary parameters to it to retrieve the data
  3. Catches any exceptions it may throw
  4. Echo any information returned by the function

Thus, there are potentially at least four lines of code we’re removing from the person who is building the template and we’re reducing it to a single line.

And that makes for a really elegant solution, in my opinion. But what would this look like, practically speaking?

A Practical Example

Maybe something as simple as this gist:

Note this grabs an instance of one of the classes (which implements the singleton design pattern) and then echoes back the data it returns all the while using get_the_ID() of the current WordPress post.

Sure, helper functions can be more complicated than this, or they may be no simpler.

Regardless, all the person has to do when working on their single post templates as to do to render the data from the meta box is insert acme_get_data() into their code and the rest is handled via the code.

Reading this via RSS? Remember to click through the provided "gist" link to see the code.

Category:
Tips
Tags:

Join the conversation! 4 Comments

  1. Hey Tom,

    Where do you put these helper functions then, in the library/plugin that defines the classes, or in the theme?

    If you put them in the library/plugin, you’re adding additional pollution to the global namespace, whereas adding them to the theme breaks proper separation of concerns.

    I do think that providing a simple way for designer to write templates is very valuable, I just wonder how to best do this.

    Two alternative ways I could imagine are:

    Provide something similar to the facades in Laravel. This can be completely contained within the plugin/library, but would make it very easy to directly deal with the objects in a template. Similar to your example above, you could directly use <?php Acme_Data::get_data( $id ); ?> inside a template.
    Use a better templating language like twig, which would then transform your example into something like {{ Acme_Data.data($id) }}.

    Both of these alternatives are a bit more exotic, though, so they might still give your designers some headaches…

    • Where do you put these helper functions then, in the library/plugin that defines the classes, or in the theme?

      Great question, but it really depends on how tailored the helpers are to the solution at hand.

      If I’m working on a plugin for someone who needs to access the data from the plugin but doesn’t have any OO experience (but is familiar with template functions), then I’ll usually include them in a dependency file as part of the plugin located in the inc directory.

      If it’s in a theme, it may be part of a library or it may just be part of a larger collection of functions located in a helpers.php file in the inc directory.

      But, again, it all goes back to how generalized the helper is. Sometimes I’ll write a small library that I can just drop in (or include as a dependency) and begin using it; other times, it’s a one off for the project at hand.

      I do think that providing a simple way for designer to write templates is very valuable, I just wonder how to best do this.

      Yeah – I wish there was one great way to do it, but if there is I’ve yet to find it :).

      Similar to your example above, you could directly use <?php Acme_Data::get_data( $id ); ?> inside a template.

      This is exactly what I’m basically doing except this assumes the person working with the templates understands classes and static functions. I’d rather provide a single function similar to how WordPress does (like the_title() or get_the_ID()) so it feels more “at home.”

      What you’ve shared though is basically wrapped by the functions I provide.

      Both of these alternatives are a bit more exotic, though, so they might still give your designers some headaches…

      Twig is something that I’m so mixed about. It introduces new syntax and has a learning curve that some people just aren’t going to pick up unless they are developers.

      For many of the people that I work with, it’s not worth it. But if I was building something for myself or with a team of other developers, then I’d definitely be more open to it.

Leave a Reply