• About Us
    • New York
  • Work
  • Capabilities
  • Careers
  • Technology
  • Blog
  • Contact Us
  • About Us
    • New York
  • Work
  • Capabilities
  • Careers
  • Technology
  • Blog
  • Contact Us
April 16, 2015

Building Truly Decoupled WordPress Themes

Posted by Christopher Davis

When you’re building a WordPress site some things belong in themes and some in plugins. Usually anything having to do with display is in the theme and anything that might be used elsewhere should be in a plugin.

As an example: take a custom post type and a custom meta box. Those are things that your end user will probably want to keep around after a theme change. They both belong in a plugin. Because you’re a really good WordPress dev, you don’t use magic strings and instead built a nice set of wrapper methods to fetch those custom meta values for your post type. Maybe as a nice object that someone can pass a post ID into and use.

Should your theme use that class from the plugin? No. Not if you’re building a decoupled WordPress theme it shouldn’t. What happens if your plugin is deactivated? Things break. Instead we should wrap up our API with something in our theme that hides the fact that there’s a plugin involved:

A More Practical Example

The above is a bit obscure, so let’s use something more real world. Let say you’re relying pretty heavily on Advanced Custom Fields (ACF). Should you use get_field like the documentation says?.

Nope. It’s the same idea as above: a deactivated ACF plugin causes the theme to fail with a function does not exist fatal error. Use a wrapper:

But What if My Theme Depends on a Plugin

Depends means that the theme can’t do its job at all without a plugin. That’s okay, just change your wrapper a bit:

Not having a dependency you need is a truly exceptional situation so throw an exception and let your error handler log it on production or show the error on dev.

Why is this different from a fatal error? Context. The exception tells you exactly what’s wrong and how to fix it. This context is not for you or the person who did the ACF integration. It’s for other developers who might not have reviewed the latest changes. Or, more likely, it’s for future you who will have totally forgotten the context by the time the error crops up again.

What About Just Using Actions in Theme Templates?

Here’s an example:

I think this is a bad solution. It’s the theme’s job to do frontend display. Mixing that into plugins is a recipe for an unmaintainable blob. Sometimes it makes sense to do the above, but most times it doesn’t. Let the WordPress template system do its job and decouple by creating nice abstractions within your theme.

pluginswordpress
Previous
Next

Latest White Papers

  • Shifting Plans for 2020 & Beyond
  • Game On: How Brands Can Log Into A Diverse Multi-Billion Dollar Industry
  • What CCPA Means For Brands
  • How Google is Improving Consumer Data Privacy
  • Ways to Prepare for the Cookieless Future
  • See all White Papers

Featured Posts

  • Ad Age Names PMG #1 Best Place to Work in 2021
  • MediaPost Names PMG Independent Agency of the Year
  • PMG Client Portfolio Trends During Amazon Prime Day 2020
  • A Closer Look at the Congressional Big Tech Market Power Report
  • What to Know About Reddit

Categories

  • Consumer Insights
  • Content
  • Creative Design
  • Data Analytics
  • Development
  • Digital TV & Video
  • Ecommerce
  • Industry News
  • Local
  • Mobile
  • Paid Search
  • PMG Culture
  • Programmatic & Display
  • SEO
  • Social Media
  • Structured Data
Fort Worth

2845 West 7th Street
Fort Worth, TX 76107

Dallas

3102 Oak Lawn Avenue
Suite 650
Dallas, TX 75219

Austin

823 Congress Avenue
Suite 800
Austin, TX 78701

London

33 Broadwick Street
London
W1F 0DQ

New York

120 East 23rd Street
New York, NY 10010

Get in touch

(817) 420 9970
info@pmg.com

Subscribe to the PMG Newsletter
© 2021 PMG Worldwide, LLC, All Rights Reserved
  • Contact
  • Privacy Policy
 Tweet
 Share
 Tweet
 Share
 Tweet
 Share
 LinkedIn
We and our partners use cookies to personalize content, analyze traffic, and deliver ads. By using our website, you agree to the use of cookies as described in our Cookie Policy.