writing extensible plugins
DESCRIPTION
Wordcamp Toronto 2013 #WCTO2013TRANSCRIPT
Writing Extensible Plugins in WordPress
#WCTO2013
About Me
Imran Nathani What I do?:
Team lead at Shaw Media for GlobalNews.ca
Past Experience:
For 6 yrs I have been developing print and broadcast news websites.
Of which 4 years, I have spent exclusively developing on WordPress VIP.
Extensibility
is notUsability
Usability
Target audience : All WordPress users
Expectations: Options Page, Shortcodes, Widgets etc.
Extensibility
Target audience: Developers
Expectations: ?????
What is an
extensible
plugin?
A plugin that can be modified and extended beyond its original purpose by another plugin or theme without modifying
original plugin.
Why write an extensible plugin?
Saves time Increases code portability Makes extra money $-) Reduces documentation
Helper function
s
“Helps” re-use code “Helps” make code easier to read
Hooks
What are hooks ?
Traditional function call
What are hooks ?
Wordpress Hook call
What are hooks ?
Wordpress hook call ( Multiple )
What are hooks ?
Hooks are placeholders for
code.
Types of hooks
Action hook
Extending methods
Filter hook
Extend values
Returns a value Returns nothing
do_action( … ) apply_filters( … )
Action hook
Wherever an action hook is placed, it will execute any code that has been “hooked” to it.
Actionhookusage
Usage A :
do_action( ‘hook-name’ );
Usage B:
do_action( ‘hook-name’ , $arg1, $arg2, … );
Usage C:
do_action_ref_array( ‘hook-name’ , $array );
Actions
Utility functions add_action(..)
remove_action(..)
remove_all_actions(..)
has_action(..)
did_action(..)
Actions
Action placements Before a task After a task Append mark up
Actions
Core actions publish_post save_post wp_head wp_foot
ActionsDEMO
Filter hooks
Wherever a filter hook is placed, it will manipulate any value that has been passed into with the function “hooked” to it
Filter hook usage
Usage A :
$value = apply_filters( ‘hook-name’ , $value )
Usage B :
$value = apply_filters( ‘hook-name’ , $value, $args, .. )
Filters
Utility functions add_filter(…)
remove_filter(…)
remove_all_filters(...)
has_filter(…)
current_filter( … )
FiltersFilter placements Before using values
Before value set up
Before returning values
Filters
Core filters the_title
the_excerpt
post_link
the_date
FiltersDEMO
Tips &
Tricks
Improve efficiency
Tips &
Tricks
Improve efficiency
Tips &
Tricks
Prefix hook names
Tips &
Tricks
Do not over extend!
Tips &
Tricks
Validate returns
Tips &
Tricks
Combine Filters
Tips &
Tricks
Combine Filters
Tips &
Tricks
Localize scripts with Filters
Tips &
Tricks
Localize scripts with Filters
Good reads
Wordpress Codex ( How to? )
Wordpress Core ( Why to? )
Questions?