3 min read

Wordpress North East

As a queue of people waited with umbrellas outside the nearby City Hall to watch young Gosforth performers, a small band of people made their way through the maze of Northumbria University’s Sutherland Building for the inaugural WordPress North East.

Building a Better WordPress Application

Regular NE developer event speaker Phil Brown extended his speaker repertoire to WordPress. He pointed out a lot of bad practice in WordPress such as content logic in the theme, repeated (i.e. sloppy code), and difficulty in portability.

His rules:

1. Do not write your application in functions.php

Do it in a custom plugin instead, and keep your theme for presentation! Otherwise, any custom post types etc will disappear should you change your theme. It could be that Themeforest practices are to blame for this being so widespread. For an example of how to do this well, see Woocommerce.

2. Every action should be hooked

function my_plugin_start(){
//fire up app
} 
//wrong 
my_plugin_start(); 
//Right add_action('plugins_loaded', 'my_plugin_start'

3. Use WordPress

Look at options API and posts for not only storing content but as a generic data store for anything that has multiple items, as it’s a powerful API.
Other ones of interest are

  • Custom post types and post meta
  • roles/capabilities, user meta
  • Options and transients
  • Scheduler
  • Remote HTTP

4. Use plugin dependencies

Unlike a lot of plugins (e.g .Drupal), WordPress unfortunately doesn’t allow for plugin dependencies out of a box.

However, there are ways around it, check out Scribu e.g .

if ( !class_exists ('plugin_x') )){
wp_die ('Plugin Y requires plugin x');
}

5. Standardise code

While this isn’t a given, it helps with building on the site (and more employable!)
Check out the WordPress Coding Standards (and it doesn’t hurt to peruse the PHPDoc either!)

6. Choose your PHP version

Decide this early, as it can be a pain to change, particularly if you want to sell a theme or plugin. The standard right now is 5.1.1 but most people are now on more recent versions of PHP. Phil uses 5.3 as most have it (and allows for namespacing etc), though 5.3 also looks interesting with an inherit-like ‘trait’ class.

7. Avoid global scoping

A bad habit of WP coding is that a lot of code has global scope, which can lead to conflicts.
One way to get around it if you have PHP 5.3 is closures (as per jQuery)

add_action('init', function() {
 // do something
});

or with older versions, namespacing.

8. Activation hooks

Activation hooks are useful for once-only (and be able to be ported)

9. define( ‘WP_DEBUG’, true)

This will show you issues that wouldn’t come up usually.
Other good debuggers are DebugBot(?).

10. Release your code

It gives you an oppportunity to show your stuff, and even get feedback. Good plugins to look at include WordPress SEO by Yoast, and Advanced Custom Fields.

Optimising WordPress Images


Wordpress Northeast event organiser Stephen Jones spoke of many of the issues for image uploads. They are:

  • File sizes: restrict image size (WP Image Size Limit), reduce baseline (Imsanity), or optimise (WP Smush.it). For theme images: use the right image types (easier for devs than users!) and optimise them e.g. with Codekit “the best $25 I ever spent”
  • Getting images at the right size for slideshows etc: can be done with commands (add_image_size—though needs the Regenerate Thumbnails plugin for any changes!) or a plugin (WPThumb which unlike the beleaguered TimThumb, should be secure!)
  • Per device: can be done with media-queries, or the Hammy plugin. Zurb Foundation is also playing in the space for solutions.
  • Delivery method: get it faster with a CDN e.g. CloudFront, MaxCDN; use sprites e.g. SpriteCow (but don’t go overboard on them!). Also think about using Lazyload, icon fonts.

There was an interesting discussion about the WordPress CDN that is available via Jetpack’s Photon: while it has some advantages (particularly if you’re on shared hosting), it is quite slow.

Slides

The next event is TBC but will feature Richard Carter talking about responsive theming (and other talks are welcome). In the interim, over at Lancaster they’re holding a WordCamp on July 13-14 2013.