wordpress theming best practices

39
WordPress Theming Best Practices for Client-Centric Web Development Brian Krogsgard - WordSesh 2 Post Status | poststat.us |@post_status

Upload: krogsgard

Post on 27-Jan-2015

124 views

Category:

Technology


2 download

DESCRIPTION

for client-centric web development

TRANSCRIPT

Page 1: WordPress Theming Best Practices

WordPress Theming Best Practices for Client-Centric Web Development

Brian Krogsgard - WordSesh 2!!

Post Status | poststat.us |@post_status

Page 2: WordPress Theming Best Practices

Structuring your WordPress theme

Page 3: WordPress Theming Best Practices

Assets

• CSS / JS / Images / fonts

• Sprites, minified files

Page 4: WordPress Theming Best Practices

Template Hierarchy

• http://codex.wordpress.org/Template_Hierarchy

• http://wphierarchy.com/

Page 5: WordPress Theming Best Practices

Template Parts• Naming conventions and organization.

• Prefix page templates “page-about-template.php” or nest them in a “page-templates” folder.

• For partial templates, use logical and consistent conventions.

• Lean toward smaller templates.

• Are you repeating yourself? Make it a template part.

• Give good and thorough comment blocks at the top of templates, even partial ones

Page 6: WordPress Theming Best Practices

Hooks vs templates

• Or both?

Page 7: WordPress Theming Best Practices

Code Syntax

Page 8: WordPress Theming Best Practices

Follow the guides

• Seriously, take the time to properly format your code.

• http://codex.wordpress.org/WordPress_Coding_Standards

Page 9: WordPress Theming Best Practices

Entering and exiting PHP

• In templates versus functions / operational code

• When in PHP, stay in PHP

Page 10: WordPress Theming Best Practices

Curly brackets versus alternative syntax for control structures

• http://php.net/manual/en/control-structures.alternative-syntax.php

• http://pippinsplugins.com/please-do-not-use-curly-brackets-in-template-files/

• http://tommcfarlin.com/commenting-code-blocks/

Page 11: WordPress Theming Best Practices

Function naming norms

• get_the_{something} vs the_{something}

Page 12: WordPress Theming Best Practices

Alignment

• Tabs for nesting, spaces for alignment.

Page 13: WordPress Theming Best Practices

Code

Page 14: WordPress Theming Best Practices

Hook Order

• Core Load http://www.rarst.net/script/wordpress-core-load/

• Wowza hooks http://codex.wordpress.org/Plugin_API/Action_Reference

• Special shoutout to template_redirect (conditionals)

Page 15: WordPress Theming Best Practices

Action and filters

• Make your parent themes filterable!

• Intro: http://pippinsplugins.com/a-quick-introduction-to-using-filters/

• You can do it: http://wp.tutsplus.com/tutorials/plugins/writing-extensible-plugins-with-actions-and-filters/

Page 16: WordPress Theming Best Practices

Properly enqueue scripts and styles

• http://wpcandy.com/teaches/how-to-load-scripts-in-wordpress-themes/

• http://wp.tutsplus.com/tutorials/the-ins-and-outs-of-the-enqueue-script-for-wordpress-themes-and-plugins/

• Register, then enqueue

• Only enqueue when you need to

• Conditionals in the enqueue function, or right in the function or template

• Load in the footer most of the time

Page 17: WordPress Theming Best Practices

Theme setup function

• http://justintadlock.com/archives/2010/12/30/wordpress-theme-function-files

• Because priorities, that’s why.

Page 18: WordPress Theming Best Practices

Loading files

• http://justintadlock.com/archives/2010/11/17/how-to-load-files-within-wordpress-themes

Page 19: WordPress Theming Best Practices

WordPress URLs

• The URL tells WordPress what to look up. Always remember this.

• http://wp.tutsplus.com/tutorials/creative-coding/the-rewrite-api-the-basics/

Page 20: WordPress Theming Best Practices

Loops• The default WordPress loop

• WP_Query: http://codex.wordpress.org/Class_Reference/WP_Query

• Best. Presentation. Ever. http://wordpress.tv/2013/03/15/andrew-nacin-wp_query-wordpress-in-depth/

• wp_reset_postdata() vs wp_reset_query() http://www.poststat.us/properly-reset-wordpress-query/

• update_post_meta_cache & update_post_term_cache

• get_posts

• pre_get_posts hook http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

• If you use query_posts(), go home.

Page 21: WordPress Theming Best Practices

Post Meta• get_post_custom() versus lots of get_post_meta().

• Check for existence before output to prevent empty markup.

• Classes / Tools.

• Advanced Custom Fields

• Pods

• CMB

• Core??? http://make.wordpress.org/core/tag/metamorphosis/

Page 22: WordPress Theming Best Practices

Design

Page 23: WordPress Theming Best Practices

Responsive Design• Either do it right, or don’t do it.

• <meta name=“viewport” content=“width=device-width, initial-scale=1”>

• Separate stylesheet? It depends.

• If not mobile first or using a PreProcessor, and building a parent them, then yes.

• Consider navigation http://www.poststat.us/wordpress-responsive-navigation-options/

• Consider what *could* go into different areas

• Breakpoints are arbitrary.

Page 24: WordPress Theming Best Practices

* { box-sizing: Border-box }

• FTW

• http://www.paulirish.com/2012/box-sizing-border-box-ftw/

Page 25: WordPress Theming Best Practices

SVG vs Icon Fonts vs Fallback PNGs.

• Depends on the browser support required

• http://caniuse.com/

Page 26: WordPress Theming Best Practices

General Principles

Page 27: WordPress Theming Best Practices

Let plugins take care of what they are good at

• Breadcrumbs? Pagination? Depends on the project.

• SEO? Let it go.

• Analyze each situation.

Page 28: WordPress Theming Best Practices

Functions.php is not a landfill

• Have either a site-specific plugin or reusable functionality plugins. Or both.

Page 29: WordPress Theming Best Practices

Stay DRY

• Don’t Repeat Yourself

Page 30: WordPress Theming Best Practices

Use things WordPress gives you

• Customizer

• Settings API

• Etc.

Page 31: WordPress Theming Best Practices

Test yourself!• Always have wp_debug on.

• WP Test http://wptest.io/

• Monster Widget http://wordpress.org/plugins/monster-widget/

• Use the Developer plugin http://wordpress.org/plugins/developer/

• Monitor queries http://wordpress.org/plugins/query-monitor/

• Chrome Inspector (Waterfall)

Page 32: WordPress Theming Best Practices

Browser Support

• Keep browser support in mind, always.

• http://www.poststat.us/explicit-browser-support/

Page 33: WordPress Theming Best Practices

Learn from the greats

• Nobody *really* starts from scratch.

• _s https://github.com/automattic/_s

• Stargazer https://github.com/justintadlock/stargazer

• Default Themes

Page 34: WordPress Theming Best Practices

Tools

Page 35: WordPress Theming Best Practices

Tools• CodeKit

• Minifies files

• Checks for errors in CSS and JS

• Live updates locally!

• Compiles Preprocessors

• PreProcessors

• Sass

• Compass http://compass-style.org/

• Bourbon http://bourbon.io

• Neat (Yeah, for real). http://neat.bourbon.io/

• LESS

• Frameworks (UI)

• Foundation by Zurb http://foundation.zurb.com/

• Twitter Bootstrap http://getbootstrap.com/

Page 36: WordPress Theming Best Practices

Tools• Handy JS

• Flexslider flexslider.woothemes.com

• CarouFredSel http://dev7studios.com/plugins/caroufredsel

• Validation

• http://jqueryvalidation.org/validate/

• yoast.com/checkout-field-validation/

• Use stuff WP ships with!

• Icon Fonts

• Dashicons

• Genericons

• Font Awesome

• Typefaces

• Typography.com

• Typekit.com

• google.com/fonts

Page 37: WordPress Theming Best Practices

Tools• Version control / Deployment

• Beanstalk

• Versions (SVN)

• Tower (Git)

• Working Locally

• VVV

• Mamp Pro

• ServerPress

• VIP Quickstart

Page 38: WordPress Theming Best Practices

Tools• Handy sites / Code Snippets

• http://codex.wordpress.org/Template_Tags

• http://queryposts.com/

• http://adambrown.info/p/wp_hooks

• http://www.billerickson.net/code/

• http://generatewp.com/

• http://themergency.com/generators/

• http://wordpress.stackexchange.com/

• http://spritecow.com

Page 39: WordPress Theming Best Practices

WordPress Theming Best Practices

Brian Krogsgard - WordSesh 2!!

Post Status | poststat.us |@post_status

for Client-Centric Web Development