Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Dave Jesch- WordCamp LA 2014- Writing Code that...

SpectrOMTech
September 04, 2014

Dave Jesch- WordCamp LA 2014- Writing Code that Scales

This presentation is for both theme and plugin developers. Dave Jesch at SpectrOMTech.com shares techniques on how to organize code so it can easily adapt to growth and reduce support after product release and/ or delivery. For other related presentations such as PHP Optimization, please visit http://SpectrOMTech.com/presentations/wcla2014/

SpectrOMTech

September 04, 2014
Tweet

More Decks by SpectrOMTech

Other Decks in Programming

Transcript

  1. © 2014 SpectrOMtech.com. All Rights Reserved. ➔ Web Architecture OOP

    ➔ Everything eCommerce ➔ Custom Plugins Dave Jesch, Lifetime Geek & CTO @ .com Scalability
  2. © 2014 SpectrOMtech.com. All Rights Reserved. 1. Best Practices 2.

    Code Organization 3. Code Techniques Today’s Goals:
  3. © 2014 SpectrOMtech.com. All Rights Reserved. Coding Standards • WordPress

    Coding Standards http://codex.wordpress.org/WordPress_Coding_Standards ◦ Indentation ◦ Placement of Braces ◦ Class / Function / Variable Naming
  4. © 2014 SpectrOMtech.com. All Rights Reserved. Data Handling • Validate

    All Input http://codex.wordpress.org/Data_Validation • Escape Output • Escape Data in SQL Queries http://codex.wordpress. org/Class_Reference/wpdb#Protect_Queries_Against_SQL_Injection_Atta cks
  5. © 2014 SpectrOMtech.com. All Rights Reserved. 1. Admin Only 2.

    Front-end Only 3. Common Separate Code into Logical Areas
  6. © 2014 SpectrOMtech.com. All Rights Reserved. Sample Organization • Main

    Plugin File plugin_directory/my_plugin.php • Admin Code plugin_directory/include/admin.php • Front-end code plugin_directory/include/frontend.php • Common Code plugin_directory/include/some_code.php plugin_directory/include/more_code.php • Assets plugin_directory/assets/css/plugin.css plugin_directory/assets/js/plugin.js plugin_directory/assets/images/logo.png
  7. © 2014 SpectrOMtech.com. All Rights Reserved. Minimize Code Usage •

    Test for Front-end vs. Admin page request • Register scripts/styles; don’t enqueue until you need to • Separate out install/uninstall code • Separate out Cron code
  8. © 2014 SpectrOMtech.com. All Rights Reserved. Determining Page Request •

    is_admin() • if (defined('DOING_AJAX') && DOING_AJAX) • if (defined('DOING_CRON') && DOING_CRON) • Understand WordPress’ Initialization Sequence http://codex.wordpress.org/Plugin_API/Action_Reference
  9. © 2014 SpectrOMtech.com. All Rights Reserved. Sample Techniques (1 of

    8) In main plugin file: define('PLUGIN_FILE', __FILE__); define('PLUGIN_DIR', plugin_dir_path(PLUGIN_FILE)); define('PLUGIN_INCLUDE', PLUGIN_DIR . 'include/'); define('PLUGIN_ASSETS', PLUGIN_DIR . 'assets/'); define('PLUGIN_ASSETS_URL', plugin_dir_url(PLUGIN_FILE) . 'assets/' define('PLUGIN_VERSION', '1.0');
  10. © 2014 SpectrOMtech.com. All Rights Reserved. Sample Techniques (2 of

    8) Check for Plugin Activation: register_activation_hook(PLUGIN_FILE, 'plugin_activation'); function plugin_activation() { require_once(PLUGIN_INCLUDE . 'activate.php'); }
  11. © 2014 SpectrOMtech.com. All Rights Reserved. Sample Techniques (3 of

    8) Load code appropriate for page request: if (defined(DOING_CRON) && DOING_CRON) require_once(PLUGIN_INCLUDE . 'cron_code.php'); else if (is_admin()) require_once(PLUGIN_INCLUDE . 'admin.php'); else require_once(PLUGIN_INCLUDE . 'frontend.php');
  12. © 2014 SpectrOMtech.com. All Rights Reserved. Sample Techniques (4 of

    8) admin.php: add_action('admin_init', 'plugin_initialize'); function plugin_initialize() { add_menu_page('title', 'menu title', 'Administrator', 'plugin-slug', 'plugin_menu_page'); } function plugin_menu_page() { include(PLUGIN_INCLUDE . 'plugin_page_contents.php'); } include(PLUGIN_INCLUDE . 'some_code.php');
  13. © 2014 SpectrOMtech.com. All Rights Reserved. Sample Techniques (5 of

    8) frontend.php: add_action('wp_enqueue_scripts', 'plugin_register_script') function plugin_register_script() { wp_register_script('plugin_javascript', PLUGIN_ASSETS . 'js/javascript.js', array('jquery'), PLUGIN_VERSION, TRUE); } add_shortcode('sample_shortcode', 'plugin_shortcode') function plugin_shortcode($attr) { wp_enqueue_script('plugin_javascript'); return ('<img src="' . PLUGIN_ASSETS_URL . 'images/plugin_image.png" width="16" height="16" />'); } include(PLUGIN_INCLUDE . 'some_code.php');
  14. © 2014 SpectrOMtech.com. All Rights Reserved. Sample Techniques (6 of

    8) some_code.php: add_action('widget_init', 'plugin_register_widgets') function plugin_register_widgets() { include(PLUGIN_INCLUDE . 'widget.php'); register_widget('PluginWidget'); }
  15. © 2014 SpectrOMtech.com. All Rights Reserved. Sample Techniques (7 of

    8) widget.php: class PluginWidget extends WP_Widget { public function widget() { // display widget contents } public function form() { include(PLUGIN_INCLUDE . 'widget_form.php'); } public function update() { include(PLUGIN_INCLUDE . 'widget_update.php'); } }
  16. © 2014 SpectrOMtech.com. All Rights Reserved. Sample Techniques (8 of

    8) Use Transients When Appropriate: // get data from transient if (false === ($menu = get_transient('main_nav_menu'))) { // nothing in transient, generate menu $args = array(..., 'echo' => 0); $menu = wp_nav_menu($args); // save data from menu in transient set_transient('main_nav_menu', $menu, 86400); } echo $menu;
  17. © 2014 SpectrOMtech.com. All Rights Reserved. Scalability Achieved! 1. Organize

    Your Code 2. Load Only What’s Needed 3. Use Your Actions 4. Always Learn More
  18. © 2014 SpectrOMtech.com. All Rights Reserved. Connect with us at

    [email protected] Live as if you were to die tomorrow. Learn as if you were to live forever. ~ Mahatma Gandhi Learning more about... OOP in
  19. © 2014 SpectrOMtech.com. All Rights Reserved. More References PHP Optimization

    Presentation OC WordCamp: http://wordpress.tv/2014/06/26/dave-jesch-php-optimization-getting-the-most- out-of-your-php-code/ http://www.slideshare.net/djesch/php-optimization-35545593