How to Maximize WordPress Efficiency using get_template_part and the DRY Principle

The DRY principle in any development work is crucial. DRY, or Don't Repeat Yourself, saves designers and developers tons of time when anything needs to be tweaked, edited, or completely changed. While it takes more of a top-down approach to wrap your head around a more efficient process, the efficiency itself will help you make for a much happier coding experience.

If you think about WordPress themes and websites as houses, you can start to get a more basic understanding of how each PHP file can contribute to the entire structure of the site.

Using the DRY Principle is the Same As Being Frugal

Most houses have attics, basements, living rooms, bedrooms, bathrooms. The same as most websites have a header, footer, main content areas, sidebars, etc. On a more detailed scale, most houses also have a bed, which need pillows and blankets. Whenever you look into covering a new bed, you know the basic things you need, and you also know if you already have some components within your house. You ask yourself, do I have extra pillows lying around? Do I have a blanket that's being used elsewhere that I like and can just go out and buy a duplicate?

Why are we talking about bedding? Because the DRY principle is similar to being frugal and smart within your own house and furnishings. Using DRY within WordPress can save you money so you don't waste your time re-coding something that already exists that you may be able to use and tweak easily.

On a small scale, if you already have a newsletter subscribe form somewhere else on your site, you can use the DRY principle to pull in that form elsewhere, then make quick tweaks to your CSS to make it appear slightly different.

I'll be using my personal blog in WordPress as an example.

Currently, I have my newsletter signup form at the bottom of each single post, but I also want to put a signup form in my header.

Step 1: Find the Existing Code

First, I'll open my single.php file. From there, I see that I've already exercised the DRY Principle by utilizing the to pull in the PHP file content-post.php . Therefore, I need to open up my content-post.php file to edit it.

In this content-post.php file I can see that I have a div with an ID of newsletter, with the Mailchimp newsletter content in that div.

Step 2: Pull the Code Into Its Own File

Because I don't want to just copy and paste the newsletter form into my header file, I'm going to instead create a new PHP file and then call it into my theme.

These smaller content parts PHP files need to be named content-whatever.php in order to be called into your PHP files using the function, so I will copy and paste the contents of the newsletter div into a new file named content-newsletter.php .

Step 3: Replace the Long Code With get_template_part

Then, I will replace the cut code in the original file ( content-post.php ) with .

Step 4: Use the get_template_part Function Elsewhere

Because the newsletter code now exists freely within content-newsletter.php , I can call the file into the header.php file easily, and wrap a new div around it so the styling can be easily changed in CSS. The code I added to my header file looks like this:

<div id="topnewsletter" class="widget"> </div>

Step 5: Style with CSS

And then I also added some quick CSS:

#topnewsletter { width: 225px; float: right; background-color: #ccece7; }

And voila, my header now has a newsletter signup box! Of course, I still need tweak the content and styling a bit, but I've easily re-used my existing newsletter code and with some quick CSS I tweaked the look of it by using the get_template_part() function in WordPress and remembering to use the DRY principle.

Conclusion

If you are creating your own WordPress themes or child themes, or tweaking existing themes, I encourage you to think critically and use the DRY principle whenever possible, especially the get_template_part() function, which makes it so easy to call blocks of code into multiple files and quickly edit it once in one file instead of having to open up multiple PHP files.

If you already have it on your site, reuse the code: think frugal and DRY!

What tips do you have for utilizing the DRY Principle in your work, WordPress or otherwise?