WordPress Plugin Development
Stirred not Shaken
Jonathan Desrosiers Twitter: @Desrosj
WordCamp Providence
1
Break the Ice
Twitter: @Desrosj
✤ From Dartmouth, Massachusetts
✤ Love Sports (Baseball & Hockey are my 1, 2)
✤ Graduated from Johnson & Wales University Providence
✤ Love a good challenge and learning new things
✤ Hobbies include Kayaking, Photography, and long walks on the
beach.
2
WordPress Experience
✤ Developing for WordPress since about 2008 (Can’t remember the love
at first site moment)
✤ Plugins on the WordPress.org Repository
✤ Two+ years WordPress VIP programming experience for sites like
TechCrunch
3
Goals Here
✤ Cover the basics, and introduce some advanced practices and
methods.
✤ Everyone gets something out of this.
✤ Make sense of the many aspects of plugin development.
✤ It is extremely easy to become overwhelmed with the massive
amount of tutorials and resources that are available to you.
✤ Stirred vs. Shaken
4
5
What The Heck’s A Plugin?
✤ Plugins are tools to extend the functionality of WordPress.
6
Examples Please?
✤ Jetpack by WordPress.com
✤ Adds WordPress.com features to your self hosted WordPress sites such as Gravatar
Hovercards, Social Media Sharing and more.
✤ Gravity Forms
✤ Easily create forms in the WP Admin. Provides conversion tracking.
7
How To Make A Plugin
✤ Four Stages
✤ Planning
✤ Implementation
✤ Release
✤ Support
8
Use The WordPress Codex
9
1. Planning
✤ What is the purpose of your plugin?
✤ What does it need to do?
✤ When should these actions be performed?
✤ What are your components? JS? CSS?
✤ Will there be an admin panel?
10
2. Implementation
✤ Follow WordPress Coding Standards
✤ http://codex.wordpress.org/WordPress_Coding_Standards
11
What Makes A Good Plugin
✤ Feel like it is a part of WordPress
✤ If the user can tell where WordPress ends and your plugin begins,
you probably should try to do things differently.
✤ Only does what it is expected to.
✤ It uses WordPress features such as post meta, Settings API, display
classes such as WP_List_Table.
✤ Don’t reinvent the wheel.
✤ Doesn’t break anything else! It just works.
12
Don’t Reinvent The Wheel
13
Naming
✤ Accurately represent your plugin
✤ Be unique - Nobody likes
a copy cat.
14
Plugin Header
✤ Plugin Header
15
Actions & Filters
✤ Actions add functionality.
✤ Filters alter or change information.
16
Actions
✤ Actions are triggered by specific events that take place in WordPress,
such as publishing a post, changing themes, or displaying a page of
the admin panel.
✤ Allows you to only execute code when it is needed.
✤ Each action hook passes specific information to the functions
attached.
17
Action Example
18
Some Useful Action Hooks
✤ save_post
✤ Runs when a post is saved.
✤ init
✤ Used to create widget areas, nav menu locations, etc.
✤ wp_enqueue_scripts
✤ Used to enqueue scripts and stylesheets.
✤ admin_menu
✤ Used to add menus to the admin interface.
19
Filters
✤ Filters are functions that WordPress passes data through, at certain
points in execution, just before taking some action with the data (such
as adding it to the database or sending it to the browser screen).
✤ Filters sit between the database & output, or input and the
database.
✤ Almost every input and output to the site is passed through at least
one filter.
✤ Allows you to alter things to display or save the way you need.
20
Filter Example
21
Some Useful Filters
✤ the_content
✤ Alter the output of the post content
✤ the_excerpt
✤ Alter the output of post excerpt
22
Actions & Filters
✤ Pretty damn useful.
✤ Allow you to alter the output and functionality of the site without
having to alter Core WordPress (which you should never do).
✤ Allows for easy updates when new WordPress versions are
available.
23
Loading Scripts The Right Way
24
Loading Scripts The Right Way
25
Loading Scripts The Right Way
26
Loading Scripts The Right Way
✤ What happens when you have 10 plugins that require jQuery?
27
Loading Scripts The Right Way
28
Works For Stylesheets Too!
29
Admin Only Scripts/Styles
30
Passing Dynamic Info to JS
✤ wp_localize_script() - Pass dynamic information to your JS
31
Input & Output Sanitation
✤ NEVER trust user input. Always make sure your output is formatted
properly.
32
Input & Output Sanitation
✤ All user input should be sanitized.
✤ *_kses() - Strips harmful HTML
✤ santize_text_field() - Sanitizes a string for database storage
✤ All output should be properly escaped for display
✤ esc_attr() - Escapes strings for use in an HTML Attribute.
✤ esc_js() - Escapes strings for output within<script> tags
✤ esc_url() - Sanitizes and escapes a URL.
33
Namespace Your Functions
✤ Non Class Approach
✤ Prepend a string to all of your functions
✤ jwp_function_name()
✤ Prevents fatal PHP errors from duplicate function names.
34
Namespace Your Functions
✤ Class Approach
35
Settings, Post Meta, User Meta
✤ Namespace all settings, post meta, and user meta to avoid conflict!
✤ jwp_user_meta_key
✤ jwp_option_name
36
Only Run Code When Needed!
✤ Be specific with your action & filter hooks.
✤ Most hooks not only run the default, but also run more specific
actions or filters.
37
Only Run Code When Needed!
✤ Be specific with your action & filter hooks.
38
1?IA8@*ts%7 (aka I’m Stuck!)
39
1?IA8@*ts%7 (aka I’m Stuck!)
✤ Make sure WP_DEBUG is turned on.
✤ Debug Bar Plugin & Extensions
✤ Start with a fresh install of WordPress, and a default theme
✤ These are mostly bug free.
40
Uninstalling Your Plugin
✤ We are sorry to see you go ;-(
✤ But we will remove our plugin info for you!
✤ Prevents useless entries in the DB
✤ Helps users keep their sites optimized.
41
Uninstalling Your Plugin
✤ Include uninstall.php in your plugin folder
42
The Next Level
✤ Add admin pages for input, and configuration.
✤ Adding AJAX Actions
✤ Use Transients for temporary data.
✤ Using wp_remote_get() and wp_remote_post()
43
The Next Level
✤ Creating your own hooks and filters
✤ I18n (Internationalization)
✤ Integrate Crons
44
Web Designer's Guide to
WordPress
45
Questions? Comments? Compliments?
✤ Twitter: @Desrosj
✤ http://jonathandesrosiers.com
✤ Slides will be posted soon.
46