Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

The Loop

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

Be worldly GetText UTF-8 Impact on data validation

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

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!

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

Starting a plugin

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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/

Slide 38

Slide 38 text

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/

Slide 39

Slide 39 text

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!