Slide 1

Slide 1 text

Hooked on WordPress An Introduction to Actions & Filters @shawnhooper - shawnhooper.ca WordCamp Ottawa
 July 2019

Slide 2

Slide 2 text

@shawnhooper - shawnhooper.ca Hi, I’m Shawn • Developing for the web since the mid-90s • Using WordPress since ~ version 2.8 (2009) • Director of IT at Actionable.co • Lives in Ottawa, Canada

Slide 3

Slide 3 text

What is a Hook ? @shawnhooper - shawnhooper.ca

Slide 4

Slide 4 text

What is a Hook ? @shawnhooper - shawnhooper.ca

Slide 5

Slide 5 text

What is a Hook ? @shawnhooper - shawnhooper.ca

Slide 6

Slide 6 text

What is a Hook ? @shawnhooper - shawnhooper.ca

Slide 7

Slide 7 text

What is a Hook ? @shawnhooper - shawnhooper.ca

Slide 8

Slide 8 text

What is a Hook ? @shawnhooper - shawnhooper.ca

Slide 9

Slide 9 text

What is a Hook ? @shawnhooper - shawnhooper.ca Hooks allow you to change the core functionality of WordPress. They are implemented through 
 themes and plugins.

Slide 10

Slide 10 text

What is a Hook ? @shawnhooper - shawnhooper.ca Never Modify Core ( or Parent Themes )

Slide 11

Slide 11 text

What is a Hook ? @shawnhooper - shawnhooper.ca WordPress Plugin API

Slide 12

Slide 12 text

What is a Hook ? @shawnhooper - shawnhooper.ca There are two kinds of hooks in WordPress: Actions and Filters

Slide 13

Slide 13 text

Action Hooks @shawnhooper - shawnhooper.ca Flickr: ifindkarma

Slide 14

Slide 14 text

Action Hooks @shawnhooper - shawnhooper.ca An action is triggered when a specific event takes place in WordPress.

Slide 15

Slide 15 text

Action Hooks @shawnhooper - shawnhooper.ca What can you do in an action? Modify data in the database Send an e-mail Interact with APIs Modify code being sent to the browser

Slide 16

Slide 16 text

Action Hooks @shawnhooper - shawnhooper.ca do_action( $tag ); do_action ($tag, $arg_a, $arg_b, $arg_c );

Slide 17

Slide 17 text

Action Hooks @shawnhooper - shawnhooper.ca do_action ( ‘wp_head’ ); wp-includes\general-template.php do_action ( ‘save_post’, $post->ID, $post, $update ); wp-includes\post.php

Slide 18

Slide 18 text

Action Hooks @shawnhooper - shawnhooper.ca add_action ( $hook, $function_to_add );
 
 add_action( $hook, $function_to_add, $priority, $accepted_args );

Slide 19

Slide 19 text

Action Hooks @shawnhooper - shawnhooper.ca The optional priority parameter allows you to specify the order in which the hooked actions will run. The default value is 10.

Slide 20

Slide 20 text

Action Hooks @shawnhooper - shawnhooper.ca add_action( ‘wp_head’, ‘smh_add_metadesc’); function smh_add_metadesc() { echo ‘’; }

Slide 21

Slide 21 text

Filter Hooks @shawnhooper - shawnhooper.ca

Slide 22

Slide 22 text

Filter Hooks @shawnhooper - shawnhooper.ca Filters are functions that WordPress passes data through, at certain points in execution, just before taking some action with the data. Source: WordPress Codex

Slide 23

Slide 23 text

Filter Hooks @shawnhooper - shawnhooper.ca $filter = apply_filters( $tag, $value ); $value = apply_filters ($tag, $value, $var1, $var2, … );

Slide 24

Slide 24 text

Filter Hooks @shawnhooper - shawnhooper.ca $content = apply_filters ( ‘the_content’, $content ); wp-includes\post-template.php $atts = apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers', 'attachments' ) );
 
 wp-includes\pluggable.php

Slide 25

Slide 25 text

Filter Hooks @shawnhooper - shawnhooper.ca add_filter ( $tag, $function_to_add );
 
 add_filter( $tag, $function_to_add, $priority, $accepted_args );

Slide 26

Slide 26 text

Filter Hooks @shawnhooper - shawnhooper.ca The optional priority parameter allows you to specify the order in which the hooked filters will run. The default value is 10.

Slide 27

Slide 27 text

Filter Hooks @shawnhooper - shawnhooper.ca function smh_old_post_notice($content) { $days = floor( ( time() - get_the_date( 'U' ) ) / ( 60 * 60 * 24 ) ); if ( $days > 365 ) { $content = '
This is an older post. Beware of possible out-of-date advice.
' . $content; } return $content; } add_filter(‘the_content’, ‘smh_old_post_notice’);

Slide 28

Slide 28 text

Hooks in Themes @shawnhooper - shawnhooper.ca 99% of the time, you’ll use hooks and filters in plugins. But there are some times where it’s useful to put these functions into your theme’s functions.php file.

Slide 29

Slide 29 text

Hooks in Themes @shawnhooper - shawnhooper.ca Examples: Theme Activation Hook Modifying the “Read More” Text Filtering Menu Content Setting up the Customizer These are all things that are isolated to the active theme. If you change themes, your site won’t break.

Slide 30

Slide 30 text

That’s the basics…. @shawnhooper - shawnhooper.ca

Slide 31

Slide 31 text

Extending Plugins @shawnhooper - shawnhooper.ca WordPress Core isn’t the only thing that can be extended with hooks. A well written plugin can also expose Actions and Filters that will allow you to add, remove, or modify its functionality.

Slide 32

Slide 32 text

Variable Hooks @shawnhooper - shawnhooper.ca Some hooks include variables in their names. These are very powerful hooks that allow you to interact with only specific objects in WordPress. save_post_{$post_type}
 ex: add_action ( ‘save_post_page’, ‘my_function’); would only trigger when a page is being saved, not a post.

Slide 33

Slide 33 text

Pluggable Functions @shawnhooper - shawnhooper.ca A set of core WordPress functions that can be overridden in your plugins. They can all be found in wp-includes\pluggable.php NOTE: No new pluggable functions are being added. They are being replaced with filters, a more flexible solution.

Slide 34

Slide 34 text

Pluggable Functions @shawnhooper - shawnhooper.ca Only one plugin can make use of these functions. For safety, wrap the function with the function_exists() check: if ( ! function_exists( ‘wp_mail’ ) ) {
 function wp_mail($to, $subject, $message, $headers = ‘’) { // do stuff }
 }

Slide 35

Slide 35 text

Removing Hooks @shawnhooper - shawnhooper.ca You can also remove hooks that have already been put into place. remove_action( $tag, $function_name, $priority ); remove_filter ($tag, $function_to_remove, $priority );

Slide 36

Slide 36 text

Removing Hooks @shawnhooper - shawnhooper.ca You can also remove ALL hooks that have already been put into place. remove_all_actions( $tag, $priority ); remove_all_filters ($tag, $priority );

Slide 37

Slide 37 text

Naming Conventions @shawnhooper - shawnhooper.ca Function names must be unique (or you risk the White Screen of Death!)

Slide 38

Slide 38 text

Naming Conventions @shawnhooper - shawnhooper.ca PRO TIP: Put your plugin in a class. add_action( ‘save_post’, array( $this, ‘send_email’) );
 
 add_filter( ‘the_content’, array( $this, ‘old_post_warning’) );

Slide 39

Slide 39 text

@shawnhooper - shawnhooper.ca Thank You!

Slide 40

Slide 40 text

@shawnhooper - shawnhooper.ca Questions ? Twitter: @shawnhooper
 
 WordPress Slack: @shooper
 
 Slides & Notes Posted on:
 www.shawnhooper.ca
 
 E-Mail:
 [email protected]