Better Theme Organization - Brent Schultz at 2014 KC WordCamp

BS
by Brent Schultz2 views·Updated Jul 12, 2014
Powered by AI

Need to present on this topic?

Create your own polished presentation in minutes. Just describe what you need.

Free to try, no credit card required

Transcript

Better Theme Organization

Same Strategy For Pages

Brent Schultz

First Steps To Cleaning Up An Existing Theme

There is no "right" way

Assumptions

  • Blow up your functions file.
  • Organize the scraps into wonderfully named bits of WordPress goodness.
  • Don't leave any page templates or loop templates in the main area.
  • "Enhance"
  • Everything we just talked about is simply a guide.
  • If you've ever had to spend more than a second or two to find a file or function, you should probably move the function or rename the file.
  • If you share or collaborate on development of the theme the importance of these practices increases ten fold.
  • You want to make an open source and free theme.
  • You want to make a theme that you know a fellow developer would be able to extend or edit with minimal effort.
  • You want to be able to find errors and extend your own theme with minimal effort.
  • You want to limit code bloat and increase code reuse.
  • You want to argue with me about the "right" way to do things.

Organization Forces You To Consider Code Cleanliness and Reuse

"Main" Directory File Comparison

  • Keep code simple and manageable by moving repeated sections of code to their own template files.
  • Use line breaks in the right places, correct indentation, correct commenting format
  • AND/OR use a good IDE like PHP Storm that does a lot this for you and makes you *want* to make these practices a habit.

Template

Eighties

BeTheme

Why You Like Organization: Informative Errors

Brent's Suggested Org Chart

Functions.php Level 99

Always Enqueue Styles and Scripts!*

* almost always

<?php wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer ); ?>

  • Let WordPress dynamically include files in your header and footer. This also eliminates the possibility of someone toying with the markup in your header or footer and breaking things.
  • Whenever possible you should load scripts in the footer.

Give me examples!

Why does this matter?

The Functions "file"...blown to bits

  • Organization is GOOD
  • Fellow Developers and troubleshooters will appreciate it
  • You will appreciate it
  • Extendability
  • Promotes clean code
  • Promotes code reuse

Including Plugins/CPT/Widgets With a Theme

  • Custom Post Types and Widgets are often good as plugins, but in circumstances where they aren't going to be used again in another theme, you're better off including them in the theme.
  • File in /inc, template in /templates or parts