Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Introduction to WordPress Development - Hooks

57 views

Published on

Presentation slides for the 5 August 2019 WordPress Singapore Meet-up. Visit https://www.edmundcwm.com/presentations/ to view other presentation resources.

Published in: Software
  • Be the first to comment

Introduction to WordPress Development - Hooks

  1. 1. Hooks An Introduction to WordPress Development 5 Aug 2019
  2. 2. Edmund Chan edmund@nerb.com.sg // https://github.com/edmundcwm Developer
  3. 3. Hooks • What is a Hook? – A place in the codebase for custom functions to be added or change WP default behaviour – Functions attached to the hook will run at specific juncture during code processing – Must-know concept in WP Development – Very common way to interact with WP
  4. 4. Hooks • Two kinds of hooks – Action hook – Filter hook
  5. 5. Hooks • Action Hook – Allows custom functions to be run at a specific point – e.g, send email after publishing a post; add a notification bar before the navigation section – do_action() – add_action() – has_action() – remove_action()
  6. 6. Hooks • Filter Hook – Allows data to be changed before it is used – e.g, change WooCommerce ‘add to cart’ text; conditionally append text to the end of the post content – apply_filters() – add_filter() – has_filter() – remove_filter()
  7. 7. Hooks Key Differences Actions Filters Do something Change something Execute functions Manipulate Data Doesn’t need to return a value Must return a value use do_action() and add_action() use apply_filters() and add_filter()
  8. 8. Hooks
  9. 9. Hooks hook: after_nav_menu function: nerb_new_action
  10. 10. Hooks
  11. 11. Hooks • Why hooks? – Recommended way to customize and extend WordPress – Easier to debug – Reduce reliability of external plugins – Better understanding of how things work – Allow for reusable custom functions – Not mess with Core files
  12. 12. Hooks
  13. 13. Hooks do_action( ‘woocommerce_thankyou’ , $order->get_id() ); Action hook Tag/Hook name Args
  14. 14. Hooks /woocommerce/templates/checkout/thankyou.php
  15. 15. Hooks add_action( ‘woocommerce_thankyou’ , ‘nerb_thankyou_info’, 10, 1 ); Priority Tag/Hook name Custom function Args
  16. 16. Hooks add_action( ‘woocommerce_thankyou’ , ‘nerb_thankyou_info’, 10, 1 );
  17. 17. Hooks add_action( ‘woocommerce_thankyou’ , ‘nerb_thankyou_info’, 10, 1 );
  18. 18. Hooks /child-theme/functions.php or /plugin-name/plugin-name.php
  19. 19. Hooks
  20. 20. Hooks apply_filters( ‘woocommerce_thankyou_order_received_text’ , ‘Thank you!’, $order ); Filter hook Tag/Hook name Value Args
  21. 21. Hooks add_filter( ‘woocommerce_thankyou_order_received_text’ , ‘nerb_thankyou’, 10, 1 ); Priority Tag/Hook name Custom function Args
  22. 22. Hooks add_filter( ‘woocommerce_thankyou_order_received_text’ , ‘nerb_thankyou’ );
  23. 23. Hooks /child-theme/functions.php or /plugin-name/plugin-name.php
  24. 24. Hooks
  25. 25. Hooks Key Differences Actions Filters Do something Change something Doesn’t need to return a value Must return a value Execute functions Manipulate data use do_action() and add_action() use apply_filters() and add_filter()
  26. 26. Hooks
  27. 27. Edmund Chan edmund@nerb.com.sg // https://github.com/edmundcwm Developer

×