Download - Building better WordPress applications
Building better WordPress applications
PBWEB.CO.UK
@PHILL_BROWN
Problems with WordPress applications• Business logic in themes
• Repeated code
• Tight coupling
• Portability
Rule #1Do not write your application in
functions.php
Decoupling plugins and themesLogic Templates
Templates
Plugin
Theme
Rule #2Every action should be hooked
function my_plugin_start() {// Fire up application
}
// Wrongmy_plugin_start();
// Rightadd_action( ‘plugins_loaded’, ‘my_plugin_start’ );
Rule #3Use WordPress
Core checklist• Custom post types and post meta
• Roles, capabilities and user meta
• Options and transients
• Scheduler
• Remote HTTP
Rule #4Use plugin dependencies
• Scribu’s Plugin Dependencies
• if ( ! class_exists( ‘plugin_x’ ) ) {wp_die( ‘Plugin Y requires plugin x’ );
}
Rule #5Standardise code
• WordPress coding standards:http://codex.wordpress.org/WordPress_Coding_Standards
• PHPDochttp://en.wikipedia.org/wiki/PHPDoc
Rule #6Choose your PHP version
• WordPress core (v3.0+) – 5.1.1
• 5.2 – DateTime
• 5.3 – Late static binding, closures, namespacing
• 5.4 - Traits
Rule #7Avoid global scoping
add_action( ‘init’, function() {// do something
} );
Closures
namespace wordpress\my_plugin;
class Post {}
Namespacing
class My_Plugin_Post {}
PHP 5.3
PHP 5.1
Rule #8Activation hooks
register_activation_hook( __FILE__, ‘my_plugin_activate’ );
function my_plugin_activate() {// Create tables, roles, capabilities
}
register_uninstall_hook( __FILE__, ‘my_plugin_uninstall’ );
function my_plugin_uninstall() {// Cleanup data
}
Rule #9define( ‘WP_DEBUG’, true );
Rule #10Release your code