22
Best practices for plugin development Create a WordPress plugin from scratch

Saigon Wordpress Meetup - Best practices for plugin development - A WordPress plugin from scratch - Andrea

Embed Size (px)

DESCRIPTION

Hướng dẫn cách phát triển một plugin cho Wordpress

Citation preview

Page 1: Saigon Wordpress Meetup - Best practices for plugin development - A WordPress plugin from scratch - Andrea

Best practices for plugin development

Create a WordPress plugin from scratch

Page 2: Saigon Wordpress Meetup - Best practices for plugin development - A WordPress plugin from scratch - Andrea

Andrea Sciamanna

present● Lead developer for WPML

● responsible and active developer of the WPML plugin and add-ons

past● Web and desktop application

developer (mostly using .net framework)

Page 3: Saigon Wordpress Meetup - Best practices for plugin development - A WordPress plugin from scratch - Andrea

Agenda

● Quick introduction about:a. code readabilityb. documentationc. patterns

● Some references● Creating a WordPress plugin from scratch● (time allowing) Introducing IDEs and OOP

Page 4: Saigon Wordpress Meetup - Best practices for plugin development - A WordPress plugin from scratch - Andrea

Code readability

1. Use coding standardsa. Use WordPress PHP Coding Standards: http://goo.

gl/FTMm0ab. Comment with PhpDOC: http://www.phpdoc.orgc. Don't repeat yourself (DRY): http://blog.codinghorror.

com/curlys-law-do-one-thingd. Don’t go crazy trying to follow standards: just get used

to them.

Page 5: Saigon Wordpress Meetup - Best practices for plugin development - A WordPress plugin from scratch - Andrea

References

● WordPress Codex: http://codex.wordpress.org● Stack Overflow: http://stackoverflow.com

○ WordPress Development: http://wordpress.stackexchange.com

● Google: seriously, do I need to tell you?

Page 6: Saigon Wordpress Meetup - Best practices for plugin development - A WordPress plugin from scratch - Andrea

Create a WordPress plugin from scratch

Page 7: Saigon Wordpress Meetup - Best practices for plugin development - A WordPress plugin from scratch - Andrea

A “Fancy” plugin for your galleriesIntegrate Fancybox in the standard WordPress gallery.

Page 8: Saigon Wordpress Meetup - Best practices for plugin development - A WordPress plugin from scratch - Andrea

Planned Functionalities

● Show images in a Fancybox/lightbox, rather than opening them in a different page

● Do not interfere with the active theme● Make it “SEO proof”● Pseudo-Watermark images without modifying

them● Allow some customization

Page 9: Saigon Wordpress Meetup - Best practices for plugin development - A WordPress plugin from scratch - Andrea

Goals

● Learn how to:○ enqueue scripts and styles○ use actions and filters○ use WordPress Settings API

● Learn best practices, of course!● ...and the Fancybox plugin

Page 10: Saigon Wordpress Meetup - Best practices for plugin development - A WordPress plugin from scratch - Andrea

Standard plugin structure

● Single file: unique file name (my-own-plugin.php)

● Complex structure:○ unique folder name (/wp-content/plugins/my-own-

plugin/)○ bootstrap script: plugin.php or any name

● Main plugin file: Standard Plugin Information● readme.txt file

Page 11: Saigon Wordpress Meetup - Best practices for plugin development - A WordPress plugin from scratch - Andrea

Skeleton

Page 12: Saigon Wordpress Meetup - Best practices for plugin development - A WordPress plugin from scratch - Andrea

Main plugin file

● Define useful constants, for later use:○ Plugin version○ Plugin path○ Plugin folder○ Plugin URL

● Include dependencies○ Shared functions○ Core class○ Options class

➔ Remember to namespace your functions, by either using unique plugins, or unique class names

➔ Do not use actual PHP namespaces!

Page 13: Saigon Wordpress Meetup - Best practices for plugin development - A WordPress plugin from scratch - Andrea

Hooks

● What are they?○ Actions -> They do things○ Filters -> They return something

● How they must be used?○ “Hook” a filter, or action to a custom function

● When?○ At the right time: it’s a bit tricky and needs practices,

some cheat sheet, or asking the community

Page 14: Saigon Wordpress Meetup - Best practices for plugin development - A WordPress plugin from scratch - Andrea

Hook to some actions and filters

● The loader class: why?○ Decoupling functionalities○ Specialized classes

● Hook to actions:○ ‘plugins_loaded’ -> the actual entry point (usually)○ ‘wp_enqueue_scripts’ -> scripts and CSS goes here

● Hook to filters:○ ‘wp_get_attachment_link’

Page 15: Saigon Wordpress Meetup - Best practices for plugin development - A WordPress plugin from scratch - Andrea

Implement hooks

● Hook to actions○ Add scripts and styles

■ Main script and styles■ Fancybox, helpers and more■ Pass data to scripts: wp_enqueue_script()

● Hook to filters○ Tweak the gallery item link

Page 16: Saigon Wordpress Meetup - Best practices for plugin development - A WordPress plugin from scratch - Andrea

Plugin options definitions and handling

● Implement plugin options○ A new class => a new file○ The options loader class

Page 17: Saigon Wordpress Meetup - Best practices for plugin development - A WordPress plugin from scratch - Andrea

WP Settings API

● WP Settings API○ register_setting()○ add_settings_section()○ add_settings_field()○ callbacks○ options_do_page

● Some thoughts about the APIs:○ Counterintuitive○ Old

Honestly, we ought to take the time to learn the Settings API however the steep the learning curve, and start working with it. It is the definitive way to go about creating options pages, and it is the current version of the API that we have available.

Tom McFarlin (http://tommcfarlin.com/wordpress-settings-api-wrapper)

Page 18: Saigon Wordpress Meetup - Best practices for plugin development - A WordPress plugin from scratch - Andrea

Plugin options page

● Show the plugin options page○ OTGS_Fancybox_Gallery_Options::get_setting static

method: why is static?○ Add a menu item in “Settings” menu○ Add some javascript and enqueue the script in the

back-end

Page 19: Saigon Wordpress Meetup - Best practices for plugin development - A WordPress plugin from scratch - Andrea

Refine the main class

● Handle plugin options● Take advantage of $wp_object_cache in

OTGS_Fancybox_Gallery::get_attachment_link

Page 20: Saigon Wordpress Meetup - Best practices for plugin development - A WordPress plugin from scratch - Andrea

The main gallery.js

● Do not use $ (in WordPress)!● Implement jquery.fancybox()● Cache jQuery elements● Play with plugin options

Page 21: Saigon Wordpress Meetup - Best practices for plugin development - A WordPress plugin from scratch - Andrea

Test it

Page 22: Saigon Wordpress Meetup - Best practices for plugin development - A WordPress plugin from scratch - Andrea

Questions?