Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Breaking up (your code) is hard to do

856 views

Published on

Looking at using template parts and OOP ideas in WordPress

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Breaking up (your code) is hard to do

  1. 1. @alleydev
  2. 2. @alleydev Breaking Up (Your Code) Is Hard To Do Dan Beil @add_action_dan
  3. 3. @alleydev Alley Interactive ● We are a full-service digital agency ● WordPress.com VIP partner ● Hiring!
  4. 4. @alleydev The Problem Navigating WordPress’ code base is difficult Code organization is one large pain point that WordPress has, because it is not regulated
  5. 5. @alleydev
  6. 6. @alleydev
  7. 7. @alleydev
  8. 8. @alleydev The Problem: continued Developers not thinking in an OOP mind set. If this doesn’t resonate with you… Developers not thinking ahead to what can be reused This leads to too much logic being included in top level (core hierarchy) template files
  9. 9. @alleydev The Problem: continued Many developers start coding after finding WordPress and learn from tutorials that, while technically correct, are actually bad practice
  10. 10. @alleydev Benefits of a Standard Structure ● Frontend developers benefit ● Onboarding / collaboration ● Better Code, Period
  11. 11. @alleydev Theme Structure ● theme-name ○ inc/ ■ post-types/ ■ helpers.php ○ static/ ■ css/ ■ js/ ○ template-parts/ ○ index.php ○ single.php ○ etc...
  12. 12. @alleydev Abstracting Code Technically works, could be optimized look for common patterns (source: un-named online tutorial) What if we use this in 5 places throughout our site?
  13. 13. @alleydev Template Parts The reusable output You can now change this HTML All in one place. Template parts should include output functions, not display logic
  14. 14. @alleydev Abstracting WP_Query 1. In functions.php 2. Or better yet in inc/helpers.php 3. Now able to reuse the same logic with one function call
  15. 15. @alleydev Example: continued Our template file would now look like this
  16. 16. @alleydev
  17. 17. @alleydev Consider the following First step towards OOP is becoming more DRY with your code Remember, “If you write it twice” … “you’ve written too much” This will make you a better developer
  18. 18. @alleydev Resources to learn ● My articles on http://addactiondan.me ○ http://bit.ly/1JulWGr ● Design Patterns in WordPress: The Singleton Pattern ○ by Tom McFarlin ■ http://bit.ly/1G80cA0 ● Fieldmanager Code Base ○ by Alley Interactive ■ http://bit.ly/1iALI6r
  19. 19. @alleydev Interested in Working at Alley? ● We’re hiring! ● If you’re interested, please send your resume & code samples to hiring@alleyinteractive.com ● More info here: ○ http://www.alleyinteractive.com/hiring/
  20. 20. @alleydev

×