3. Whatʼs the big deal?
WordCamp Raleigh • Customization of core functionality
2011
4. Whatʼs the big deal?
WordCamp Raleigh • Customization of core functionality
2011
• Separation between 3rd party code and
the core codebase
5. Whatʼs the big deal?
WordCamp Raleigh • Customization of core functionality
2011
• Separation between 3rd party code and
the core codebase
• Content Management Systems
• Customer Relationship Management systems
• PHP frameworks
• Robust social networks
• Micro blogging sites
• E-commerce shops
• Online gaming frameworks
• Software as a Service frameworks
• Multimedia galleries
6. Whatʼs the big deal?
WordCamp Raleigh • Customization of core functionality
2011
• Separation between 3rd party code and
the core codebase
• Content Management Systems
• Customer Relationship Management systems
• PHP frameworks
• Robust social networks
• Micro blogging sites
• E-commerce shops
• Online gaming frameworks
• Software as a Service frameworks
• Multimedia galleries
22 million estimated installs of WordPress
8. Important Components
WordCamp Raleigh
2011
Hooks
Special WordPress functions created to give other
code access to the PHP script or to the data at
specific points during its execution.
9. Important Components
WordCamp Raleigh
2011
Hooks
Special WordPress functions created to give other
code access to the PHP script or to the data at
specific points during its execution.
examples:
• apply_filters
• do_action
• apply_filters_ref_array
• do_action_ref_array
10. Important Components
WordCamp Raleigh
2011
Actions
Special WordPress functions that are created for
the purpose of altering the original course of the
PHP script.
11. Important Components
WordCamp Raleigh
2011
Actions
Special WordPress functions that are created for
the purpose of altering the original course of the
PHP script.
examples:
• wp_update_plugins
• wp_old_slug_redirect
• _publish_post_hook
• smilies_init
12. Important Components
WordCamp Raleigh
2011
Filters
Special WordPress functions that are created for
the purpose of parsing or altering the data
handled by the PHP script.
13. Important Components
WordCamp Raleigh
2011
Filters
Special WordPress functions that are created for
the purpose of parsing or altering the data
handled by the PHP script.
examples:
• sanitize_email
• capital_P_dangit
• wpautop
• strip_tags
14. Important Components
WordCamp Raleigh
2011
Tags
Tags are unique strings used by WordPress to
associate specific instances of a hook with
specific action or filter functions
15. Important Components
WordCamp Raleigh
2011
Tags
Tags are unique strings used by WordPress to
associate specific instances of a hook with
specific action or filter functions
examples:
• init
• template_redirect
• the_content
• wp_footer
16. Important Components
WordCamp Raleigh
2011
$wp_filter
A special WordPress variable that contains all
hook tags used added on the current page load
along with any associated filter or action functions.
17. Important Components
WordCamp Raleigh
2011
Utility Functions
Special WordPress functions developed to help
manage and maintain the association between
hooks and filter for action functions.
18. Important Components
WordCamp Raleigh
2011
Utility Functions
Special WordPress functions developed to help
manage and maintain the association between
hooks and filter for action functions.
examples:
• add_action • add_filter
• remove_action • remove_filter
• remove_all_actions • remove_all_filters
• has_action • has_filter
• current_filter
19. Quick Review
• WordPress is a PHP script.
WordCamp Raleigh • Hooks that provide access to other code blocks are
2011 placed throughout the script.
• Tags are used in association with these hooks to identify
the location of each instance within the script.
• Filters and actions are special functions able to modify
the data or the course of the script without altering the
core code.
• $wp_filter is an associative array that stores tags along
with their associated filter or action functions.
• Utility functions exist to help manage and maintain
those relationships.
20. Examples
WordCamp Raleigh
2011
Add a basic filter
This filter is very simple. It only uses the
mandatory parameters and it references a
WordPress function.
add_filter( ‘the_title’, ‘wptexturize’);
parent_name = apply_filters( 'the_title', $parent->post_title,
$parent->ID );
21. Examples
WordCamp Raleigh
2011
Add a basic filter with priority
This filter uses a 3rd and optional parameter for
priority. If the 3rd parameter isn’t set, it defaults to
10. The lower the number, the higher the priority.
add_filter( ‘comment_text’, ‘make_clickable’, 9 );
$comment = apply_filters( 'comment_text', $comment );
22. Examples
WordCamp Raleigh
2011 Add an action with the accepted
args parameter
This filter uses a 4th and optional parameter for
accepted params. If not passed, it defaults to 1.
add_action(
‘post_updated’,
‘wp_check_for_changed_slugs’,
12,
3
);
do_action( 'post_updated', $post_ID,
$post_after, $post_before);
23. Examples
WordCamp Raleigh
2011 Attaching a filter to the
apply_filters_ref_array
This hook only passes your filter function one
parameter but it’s much more powerful for a
couple of reasons.
$this->posts = apply_filters_ref_array(
'posts_results',
array( $this->posts, &$this )
);
add_filter( 'posts_results', 'testers' );
function testers ( $query ) {
echo "<pre>";print_r( $query );die();
}
25. Examples
WordCamp Raleigh Attaching an action to the
2011
do_action_ref_array
This hook only passes your action function one
parameter but it’s much more powerful for the
same reasons as apply_filters_ref_array.
do_action_ref_array(
'pre_get_posts',
array(&$this)
);
add_action( 'pre_get_posts', 'testers' );
function testers ( $query ) {
echo "<pre>";print_r( $query );
die();
}
27. Common Mistakes
Deprecated tags
WordCamp Raleigh
2011 http://adambrown.info/p/wp_hooks/hook?old=1
Bad timing
If your plugin calls add_action( ‘wp_head’, ‘...’ ) after the
do_action( ‘wp_head’ ); hooks has been called, your
action won’t ever be fired. Additionally, you can’t use
add_action or add_filter functions before they’ve been
defined by WordPress.
Echoing data in filters
This really will cause the world to end
Bad or No data returned by filter
This is a handshake. Give back what you receive.
28. Common Mistakes
Mismatched parameters in remove_*
WordCamp Raleigh
2011 If you’re using remove_action or remove_filter, make
sure that you’re using the same params used with
add_action and add_filter
Missing parameters
Make sure you’ve used the 4th accepted_args param
with add_action or add_filter.
29. Frequently Asked
Questions
Can I use the hook API outside of WordPress
WordCamp Raleigh
2011 Yes. wp-includes/plugin.php & http://backpress.org/
Are there other ʻhooksʻ in the code?
Yes. http://codex.wordpress.org/WordPress_API's
What if I canʼt find a needed hook
Look harder. Check further up the code.
http://wordpress.org/news/2009/05/ideas/
Can I remove remove WordPress core hooks?
Yes. /wp-includes/default-filters.php
Can I add my own hook tags?
Yes. I would encourage it.