Upgrade to Pro — share decks privately, control downloads, hide ads and more …

WordPress Hooks

WordPress Hooks

From WordCamp Philly 2015 - Learn to get comfortable working with Action and Filter Hooks in WordPress

Zac Gordon

June 13, 2015
Tweet

More Decks by Zac Gordon

Other Decks in Technology

Transcript

  1. WordPress Hooks Zac Gordon @zgordon

  2. 2000+ Hooks in WordPress

  3. http://hookr.io/4.1.1/#index=a

  4. 200+ Hooks Actually Use

  5. “Anything, Anywhere, Anytime”

  6. “Anything, Anywhere, Anytime”

  7. “Anything, Anywhere, Anytime”

  8. “Anything, Anywhere, Anytime”

  9. “Anything, Anywhere, Anytime” Filters }

  10. “Anything, Anywhere, Anytime” Actions }

  11. Filters vs Actions

  12. Filters modify something

  13. Actions do something

  14. Filters in Core

  15. $admin_title = apply_filters( 'admin_title', $admin_title ); … <title><?php echo $admin_title;

    ?></title> wp-admin/admin-header.php
  16. Actions in Core

  17. <title><?php echo $admin_title; ?></title> … <?php do_action( 'admin_head' ); ?>

    … wp-admin/admin-header.php
  18. What Is Possible?

  19. Read the Codex

  20. http://codex.wordpress.org/Plugin_API/Filter_Reference http://codex.wordpress.org/Plugin_API/Action_Reference

  21. http://codex.wordpress.org/Plugin_API/Filter_Reference Filters

  22. http://codex.wordpress.org/Plugin_API/Action_Reference Actions

  23. http://codex.wordpress.org/Plugin_API/Action_Reference Runtime

  24. http://codex.wordpress.org/Plugin_API/Action_Reference/init Reference Pages

  25. http://hookr.io http://wpseek.com

  26. $wp_filter

  27. <?php echo "<pre>"; var_export( $wp_filter ); echo “</pre>”;? ?> theme/functions.php

  28. $wp_filter

  29. $wp_filter http://www.rarst.net/wordpress/debug-wordpress-hooks/

  30. Helpful Plugins

  31. Simply Show Hooks https://wordpress.org/plugins/simply-show-hooks/

  32. WP Query https://wordpress.org/plugins/query-monitor/

  33. Back to the Code

  34. Power of Search

  35. SublimeText Ctrl + Shift + F

  36. Okay, Let’s Do Something

  37. Deactivate something

  38. None
  39. <?php remove_action( 'wp_enqueue_scripts', 'wptreehouse_badges_frontend_scripts_and_styles' ); ?> theme/functions.php

  40. None
  41. Order Matters

  42. None
  43. .plugin-override { color: #fff !important } theme/style.css

  44. theme/functions.php <?php remove_action( 'wp_enqueue_scripts', 'wptreehouse_badges_frontend_scripts_and_styles' ); add_action( 'wp_enqueue_scripts', ‘wptreehouse_badges_frontend_scripts_and_styles’, 9

    ); ?>
  45. None
  46. Quick Review

  47. remove_action( ‘hook’, ‘function’ )

  48. remove_action( ‘hook’, ‘function’ ) add_action( ‘hook’, ‘function’, 10 )

  49. Hooks Open WordPress to Your Needs

  50. A Few Examples

  51. function excerpt_length_example( $length ) { return 15; } add_filter( 'excerpt_length',

    'excerpt_length_example' );
  52. function body_class_example( $classes ) { if( is_single() ) { foreach(

    get_the_category( get_the_ID() ) as $cat ) $classes[] = 'cat-' . $cat->category_nicename; } return $classes; } add_filter( 'body_class', 'body_class_example' );
  53. function register_my_menus() { register_nav_menus( array( 'footer_menu' => __( 'Footer Menu',

    'mytheme' ) ) ); } add_action( 'init', 'register_my_menus' );
  54. function my_plugin_page() { add_options_page( 'Page Title', ‘Menu Title', 'manage_options', ‘plugin-slug',

    ‘my_plugin_page_init’ ); } add_action( 'admin_menu' , 'my_plugin_page' );
  55. function publish_post_to_slack( ) { // Connect to Slack API }

    add_action( 'save_post', ‘publish_post_to_slack’ );
  56. https://teamtreehouse.com/

  57. Zac Gordon wp.zacgordon.com @zgordon https://teamtreehouse.com/learn/wordpress