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

Juliette Reinders Folmer

October 04, 2013
Tweet

More Decks by Juliette Reinders Folmer

Other Decks in Programming

Transcript

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

    View full-size slide

  2. Juliette Reinders Folmer
    Email: [email protected]
    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

    View full-size slide

  3. 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

    View full-size slide

  4. 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

    View full-size slide

  5. The Loop
    if ( have_posts() ) {
    while ( have_posts() ) {
    the_post();
    //
    // Post Content here
    //
    } // end while
    } // end if
    ?>

    View full-size slide

  6. 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

    View full-size slide

  7. Avoid conflict
    function_exists()
    class_exists()
    Jquery no conflicts mode
    Use bundled libraries

    View full-size slide

  8. 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

    View full-size slide

  9. 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()

    View full-size slide

  10. 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

    View full-size slide

  11. Be worldly
    GetText
    UTF-8
    Impact on data validation

    View full-size slide

  12. 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!

    View full-size slide

  13. Starting a plugin
    /*
    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
    */

    View full-size slide

  14. 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
    ...

    View full-size slide

  15. Hooks
    Offer your own action hooks
    Offer your own filter hooks
    Document using the @api tag

    View full-size slide

  16. 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

    View full-size slide

  17. 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/

    View full-size slide

  18. 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/

    View full-size slide

  19. Keep in touch!
    (I’m self-employed, you can hire me ;-) )
    Juliette Reinders Folmer
    Email: [email protected]
    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!

    View full-size slide