WordPress Filters and Actions @glennansley WordCamp Raleigh 2011
Whatʼs the big deal? WordCamp Raleigh 2011
Whatʼs the big deal? WordCamp Raleigh • Customization of core functionality 2011
Whatʼs the big deal? WordCamp Raleigh • Customization of core functionality 2011 • Separation between 3rd party code and the core codebase
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
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
Itʼs all about the timing WordCamp Raleigh 2011
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.
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
Important Components WordCamp Raleigh 2011 Actions Special WordPress functions that are created for the purpose of altering the original course of the PHP script.
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
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.
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
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
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
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.
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.
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
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.
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 );
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 );
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);
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(); }
Examples Attaching a filter to the WordCamp Raleigh 2011 apply_filters_ref_array Array ( [0] => stdClass Object ( [ID] => 3456 [post_author] => 2 [post_date] => 2010-12-29 15:20:57 [post_date_gmt] => 2010-12-29 19:20:57 [post_content] => [simplemap taxonomy_field_type='checkboxes'] [post_title] => SimpleMap [post_excerpt] => [post_status] => publish [comment_status] => open [ping_status] => open [post_password] => [post_name] => simplemap [to_ping] => [pinged] => [post_modified] => 2011-05-08 23:13:14 [post_modified_gmt] => 2011-05-09 03:13:14 [post_content_filtered] => [post_parent] => 0 [guid] => http://localhost/wptrunk/?page_id=3456 [menu_order] => 0 [post_type] => page [post_mime_type] => [comment_count] => 0 ) )
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(); }
Examples Attaching an action to the WordCamp Raleigh 2011 do_action_ref_array WP_Query Object ( [query_vars] => Array ( [error] => [m] => 0 [p] => 0 [post_parent] => [subpost] => [subpost_id] => [attachment] => [attachment_id] => 0 [name] => [static] => [pagename] => [page_id] => 3456 [second] => [minute] => [hour] => [day] => 0 [monthnum] => 0 [year] => 0 [w] => 0 [category_name] => [tag] => [cat] => [tag_id] => [author_name] =>
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.
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.
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.
Additional Resources WordCamp Raleigh Online 2011 • http://codex.wordpress.org/Plugin_AP http://codex.wordpress.org/Plugin_API/Filter_Reference • http://codex.wordpress.org/Plugin_API/Action_Reference • http://codex.wordpress.org/Plugin_API/Hooks_2.0.x • http://wpcandy.com/teaches/how-to-use-wordpress-hooks • http://www.nathanrice.net/blog/an-introduction-to-wordpress-action- hooks/ • http://adambrown.info/p/wp_hooks • http://wpengineer.com/1302/define-your-own-wordpress-hooks/ • http://wordpress.org/extend/plugins/wordpress-hook-sniffer/ • http://wp-roadmap.com/demo/ • http://andy.wordpress.com/2008/10/30/wordpress-include-stack/ Offline • Professional WordPress Plugin Development • Professional WordPress • The WordPress Bible
WordPress Filters and Actions @glennansley WordCamp Raleigh 2011

WordPress Filters and Actions

  • 1.
    WordPress Filters and Actions @glennansley WordCamp Raleigh 2011
  • 2.
    Whatʼs the bigdeal? WordCamp Raleigh 2011
  • 3.
    Whatʼs the bigdeal? WordCamp Raleigh • Customization of core functionality 2011
  • 4.
    Whatʼs the bigdeal? WordCamp Raleigh • Customization of core functionality 2011 • Separation between 3rd party code and the core codebase
  • 5.
    Whatʼs the bigdeal? 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 bigdeal? 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
  • 7.
    Itʼs all aboutthe timing WordCamp Raleigh 2011
  • 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(); }
  • 24.
    Examples Attaching a filter to the WordCamp Raleigh 2011 apply_filters_ref_array Array ( [0] => stdClass Object ( [ID] => 3456 [post_author] => 2 [post_date] => 2010-12-29 15:20:57 [post_date_gmt] => 2010-12-29 19:20:57 [post_content] => [simplemap taxonomy_field_type='checkboxes'] [post_title] => SimpleMap [post_excerpt] => [post_status] => publish [comment_status] => open [ping_status] => open [post_password] => [post_name] => simplemap [to_ping] => [pinged] => [post_modified] => 2011-05-08 23:13:14 [post_modified_gmt] => 2011-05-09 03:13:14 [post_content_filtered] => [post_parent] => 0 [guid] => http://localhost/wptrunk/?page_id=3456 [menu_order] => 0 [post_type] => page [post_mime_type] => [comment_count] => 0 ) )
  • 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(); }
  • 26.
    Examples Attaching an action to the WordCamp Raleigh 2011 do_action_ref_array WP_Query Object ( [query_vars] => Array ( [error] => [m] => 0 [p] => 0 [post_parent] => [subpost] => [subpost_id] => [attachment] => [attachment_id] => 0 [name] => [static] => [pagename] => [page_id] => 3456 [second] => [minute] => [hour] => [day] => 0 [monthnum] => 0 [year] => 0 [w] => 0 [category_name] => [tag] => [cat] => [tag_id] => [author_name] =>
  • 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.
  • 30.
    Additional Resources WordCamp Raleigh Online 2011 • http://codex.wordpress.org/Plugin_AP http://codex.wordpress.org/Plugin_API/Filter_Reference • http://codex.wordpress.org/Plugin_API/Action_Reference • http://codex.wordpress.org/Plugin_API/Hooks_2.0.x • http://wpcandy.com/teaches/how-to-use-wordpress-hooks • http://www.nathanrice.net/blog/an-introduction-to-wordpress-action- hooks/ • http://adambrown.info/p/wp_hooks • http://wpengineer.com/1302/define-your-own-wordpress-hooks/ • http://wordpress.org/extend/plugins/wordpress-hook-sniffer/ • http://wp-roadmap.com/demo/ • http://andy.wordpress.com/2008/10/30/wordpress-include-stack/ Offline • Professional WordPress Plugin Development • Professional WordPress • The WordPress Bible
  • 31.
    WordPress Filters and Actions @glennansley WordCamp Raleigh 2011