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

Best practices for WordPress plugin development

Best practices for WordPress plugin development

Slides for the first part of this tutorial presented on October 4th 2013 at PHPNW, Manchester, UK.
http://conference.phpnw.org.uk/phpnw13/
---------------------------------------------------------------
With the release of WP3, WordPress has become a serious contender on the CMS market. The WordPress plugin system allows you to add functionality to WordPress in a snap and turn it into much more than ‘just a blogging platform’.
Learn how to develop for WordPress like a pro and take WordPress to the next level.
---------------------------------------------------------------
Tutorial code:
https://github.com/jrfnl/wp-plugin-best-practices-demo

2776198ea9584b6c0d4b494293b8d635?s=128

Juliette Reinders Folmer

October 04, 2013
Tweet

Transcript

  1. Best practices for WordPress plugin development Tutorial @ PHPNW2013 Juliette

    Reinders Folmer
  2. Juliette Reinders Folmer Email: juliette@adviesenzo.nl Web: http://www.adviesenzo.nl/ LinkedIn: http://nl.linkedin.com/in/julietterf Twitter:

    http://twitter.com/jrf_nl GitHub: http://github.com/jrfnl/ WordPress: http://profiles.wordpress.org/jrf
  3. None
  4. None
  5. None
  6. None
  7. Anatomy of WP Core Plugins Themes Languages Js Libraries Post

    Types Taxonomies Content areas: Front-end: Header Main (Loop) Sidebar (Widgets) Footer Back-end: Admin Bar Menu Post Types Metaboxes Settings pages
  8. None
  9. Hooks Actions – do something Filters – filter someting and

    return the result See: Manual Hooks database: http://adambrown.info/p/wp_hooks Debug Bar - Action& filter hooks plugin
  10. None
  11. The Loop <?php if ( have_posts() ) { while (

    have_posts() ) { the_post(); // // Post Content here // } // end while } // end if ?>
  12. None
  13. Don’t reinvent the wheel Dashboard Widgets API Database API HTTP

    API File Header API Filesystem API Heartbeat API Metadata API Options API Plugin API Quicktags API Rewrite API Settings API Shortcode API Theme modification API Theme customization API Transients API Widgets API XML-RPC WordPress API
  14. None
  15. Avoid conflict function_exists() class_exists() Jquery no conflicts mode Use bundled

    libraries
  16. None
  17. Be Unique PHP: Classes Functions global vars (global) constants WP:

    shortcodes option(s) / meta fields nonces settings pages custom post types hooks Filenames HTML/CSS: classes, ids Javascript: I18n object functions Multi-lingual I18n text domain Choose your plugin name carefully & implement consistently
  18. None
  19. Be lazy, be lean Use the available is_...() functions Conditional

    loading of include files Conditional loading of css / js both on admin as well as frontend! Minify js and css New in WP3.6: has_shortcode()
  20. None
  21. Be safe Check early & check often Validation all input

    sanitize_text_field(), sanitize_title(), sanitize_meta() etc function group Escape all output esc_html(), esc_attr(), esc_url(), esc_textarea(), esc_js() etc Use wp_nonce
  22. None
  23. Be worldly GetText UTF-8 Impact on data validation

  24. None
  25. None
  26. Don’t get discouraged WP_DEBUG constants Enable error logging! wp_config.php: @ini_set()

    set_error_handling() to a backtrace function Debug bar to the rescue JS console logging + plugin set_transient() Beware: Lots of templates which do not comply to the WP standards!
  27. None
  28. None
  29. None
  30. None
  31. Starting a plugin <?php /* Plugin Name: Demo Quotes Plugin

    Plugin URI: https://github.com/jrfnl/wp-plugin-best- practices-demo Description: Demo plugin for WordPress Plugins Best Practices Tutorial Version: 1.0 Author: Juliette Reinders Folmer Author URI: http://adviesenzo.nl/ Text Domain: demo-quotes-plugin Domain Path: /languages/ License: GPL v3 */
  32. What every plugin needs Hook your functionality onto actions and

    filters Loading your localization files Load CSS Load JS Do something Add admin page or add to another admin page Help information Activation/ Upgrade routines Uninstall routines ...
  33. None
  34. Hooks Offer your own action hooks Offer your own filter

    hooks Document using the @api tag
  35. None
  36. To sum up: Use classes, class constants and statics Most

    plugins run as singletons Leave as small a footprint in the global namespace as possible Prefix ALL your css classes/ids, js functions and anything else in the global namespace Use WP functions – combined knowledge of thousands
  37. Credits Anatomy - Eva di Martino http://www.pureblacklove.com Bridge - Glenn

    Euloth http://www.flickr.com/photos/eulothg/4956082108/ Conflict - Asaf Antman http://www.flickr.com/photos/asafantman/5134136997/ Help - Green Kozi http://www.flickr.com/photos/themacinator/3445776069/ Hooks - Raul Lieberwirth http://www.flickr.com/photos/lanier67/185311136/ Hooks – Macroman (red background) http://www.flickr.com/photos/macroman/34644959/ Alone – Jon http://www.flickr.com/photos/jb-london/3594171841/ Fun - Justin Beckley http://www.flickr.com/photos/justinbeckleyphotography/8452437969/ Lazy - Kevin Cauchi http://www.flickr.com/photos/kpcauchi/5376768095/ Loop - Gabe Kinsman http://www.flickr.com/photos/auguris/5286612308/ Security – kismihok http://www.flickr.com/photos/kismihok/9686252463/
  38. Credits Unique - Luca Volpi (leafs) http://www.flickr.com/photos/luca_volpi/2974346674/ Unique - David

    Sprinks (birds) http://www.flickr.com/photos/davidspinks/4211976336/ Wheel - Pauline Mak http://www.flickr.com/photos/__my__photos/5025541044/ WordPress - mkhmarketing (crayons) http://www.flickr.com/photos/mkhmarketing/8469030267/ WordPress - Tom Woodward (revolution) http://www.flickr.com/photos/bionicteaching/3048825267/ WordPress - Saad Irfan (core, plugins, themes) http://www.flickr.com/photos/saadirfan/5722057280/ CMS Landscape - Philippe Martin http://www.flickr.com/photos/lafabriquedeblogs/5997969999/ World - Kenneth Lu http://www.flickr.com/photos/toasty/1540997910/ Bike - Pauline Mak http://www.flickr.com/photos/__my__photos/6399028713/ Daisies - Steve Wall http://www.flickr.com/photos/stevewall/4780035332/
  39. Keep in touch! (I’m self-employed, you can hire me ;-)

    ) Juliette Reinders Folmer Email: juliette@adviesenzo.nl Web: http://www.adviesenzo.nl/ LinkedIn: http://nl.linkedin.com/in/julietterf Twitter: http://twitter.com/jrf_nl GitHub: http://github.com/jrfnl/ WordPress: http://profiles.wordpress.org/jrf Please rate this talk on joined.in/ Endorsements and recommendations on LinkedIn are much appreciated too!