Your SlideShare is downloading. ×
WordPress hooks - WPLDN July 2013 Meetup
WordPress hooks - WPLDN July 2013 Meetup
WordPress hooks - WPLDN July 2013 Meetup
WordPress hooks - WPLDN July 2013 Meetup
WordPress hooks - WPLDN July 2013 Meetup
WordPress hooks - WPLDN July 2013 Meetup
WordPress hooks - WPLDN July 2013 Meetup
WordPress hooks - WPLDN July 2013 Meetup
WordPress hooks - WPLDN July 2013 Meetup
WordPress hooks - WPLDN July 2013 Meetup
WordPress hooks - WPLDN July 2013 Meetup
WordPress hooks - WPLDN July 2013 Meetup
WordPress hooks - WPLDN July 2013 Meetup
WordPress hooks - WPLDN July 2013 Meetup
WordPress hooks - WPLDN July 2013 Meetup
WordPress hooks - WPLDN July 2013 Meetup
WordPress hooks - WPLDN July 2013 Meetup
WordPress hooks - WPLDN July 2013 Meetup
WordPress hooks - WPLDN July 2013 Meetup
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

WordPress hooks - WPLDN July 2013 Meetup

317

Published on

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
317
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
8
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. WordPress Hooks Scott Cariss • @l3rady • Big Fish Ltd
  • 2. What I'm going to cover The WordPress page lifecycle. What are hooks and how do they fit into the page lifecycle? Why use hooks? What are actions What are filters Examples
  • 3. The WP page lifecycle "A page life cycle is nothing more than a combination of the events that take place from when a browser requests a page to when the server returns the page to the browser." Lets look at loading a single page in WP - Look at the requested page ID - Query the database - Get associated data (Categories, tags, images, etc) - Return all of the data to the browser
  • 4. The WP page lifecycle The template files and the calls to the API functions are then responsible for rendering, styling and position the data on the screen Sounds simple but this is just a simple page load. Think of more complex sites WP sites you have been on. You can now understand how intensive this particular process can be.
  • 5. The WP page lifecycle "While WordPress is running its series of queries and preparing to render data back to the browser, it’s looking at all of the custom hooks – that is, the actions and filters – that have been written and is passing data through those filters before returning data to the browser." At this point it's important to define what hooks are and look at the difference between action and filter hooks and how they play into this whole lifecycle
  • 6. What are hooks and why use them? They are the backbone of WordPress. They enable developers to "hook" into the WP lifecycle to change how it works without modifying the core code. Developers hook code is separate from WP core so that WP updates don't remove or destroy the developers code.
  • 7. Different functions for hooks Action Hooks - do_action() - add_action() - remove_action() - has_action() - do_action_ref_array() - did_action() - remove_all_actions() Filter Hooks - apply_filters() - add_filters() - remove_filters() - has_filter() - current_filter() - merge_filters() - remove_all_filters()
  • 8. Using action hooks do_action( 'hook_name', $arg1, $arg2, ... ); add_action( 'hook_name', 'your_function_name', [priority], [number_args_accepted] ); remove_action( 'hook_name', 'function_to_remove', [priority], [number_args_accepted] );
  • 9. Using filter hooks $value = apply_filters( 'hook_name', $value, $value2, ... ); add_filter( 'hook_name', 'your_filter_function', [priority], [number_args_accepted] ); remove_filter( 'hook_name', 'filter_function_to_remove', [priority], [number_args_accepted] );
  • 10. Examples Changing the excerpt length on the home page: function wpldn_excerpt( $length ) { if ( is_home() ) { return 20; } return $length; } add_filter( 'excerpt_length', 'wpldn_excerpt', 999 );
  • 11. Examples Adding Google Maps JS to contact us page function wpldn_maps_js() { if ( !is_page( 'contact-us' ) ) { return; } wp_enqueue_scripts( 'google_maps_api', '[PATH_TO_GOOGLE_MAP_API]', array(), false, true ); wp_enqueue_scripts( 'my_map_js', '[PATH_TO_YOUR_JS]', array( 'google_maps_api' ), false, true ); } add_action( 'wp_enqueue_scripts', 'wpldn_maps_js' );
  • 12. Examples Load a different theme template file at will function wpldn_any_file() { if( !is_page( "about-us" ) ) { return; } include( '/path/to/any/file.extension' ); exit(); } add_action( 'template_redirect', 'wpldn_any_file' );
  • 13. Examples Using pre_get_posts to change the main query function wpldn_change_query( $query ) { if( $query->is_home() && $query->is_main_query() ) { $query->set( 'posts_per_page', 20 ); } } add_action( 'pre_get_posts', 'wpldn_change_query' );
  • 14. Examples Remove that pesky capital_P_dangit function remove_filter( 'the_title', 'capital_P_dangit', 11 ); remove_filter( 'the_content', 'capital_P_dangit', 11 ); remove_filter( 'comment_text', 'capital_P_dangit', 31 ); * Check /wp-includes/default-filters.php to see what functions WP is applying to its own filters
  • 15. Resources WordPress Codex - Action Reference
  • 16. Resources Debug Bar plugin & Action Hooks add on
  • 17. Don't be scared! Rummage around in core code to find filters and hooks that are not documented. A good knowledge of PHP will help you to find and understand WordPress hooks. IDEs can help in navigating through WP core code.
  • 18. Questions?

×