Slide 1

Slide 1 text

@wpmark https://highrise.digital A Deep Understanding of WordPress Actions and Filters Mark Wilkinson (@wpmark) WordCamp Edinburgh July 2017

Slide 2

Slide 2 text

@wpmark https://highrise.digital Mark Wilkinson Using & building WordPress sites for over 10 years as a WordPress developer Owner of Highrise Digital Follow me: @wpmark

Slide 3

Slide 3 text

@wpmark https://highrise.digital What are actions/filters? How do they work? Examples Making your code extensible

Slide 4

Slide 4 text

@wpmark https://highrise.digital What are actions/filters? How do they work? Examples Making your code extensible

Slide 5

Slide 5 text

@wpmark https://highrise.digital What are actions/filters? How do they work? Examples Making your code extensible

Slide 6

Slide 6 text

@wpmark https://highrise.digital What are actions/filters? How do they work? Examples Making your code extensible

Slide 7

Slide 7 text

@wpmark https://highrise.digital Simply put, hooks are what give us the ability to customize, extend, and enhance WordPress through an API in our themes, plugins, and other custom development efforts. Tom McFarlin, 2012 https://code.tutsplus.com/articles/the-beginners-guide-to-wordpress-actions-and-filters--wp-27373

Slide 8

Slide 8 text

@wpmark https://highrise.digital Simply put, hooks are named events, run in the WordPress code that allow us (developers) to run our own code at these points in time. Mark Wilkinson, 2017

Slide 9

Slide 9 text

@wpmark https://highrise.digital WordPress Page Life Cycle • Inspects the URL • Queries DB for that content • Queries DB for related data (taxonomies, images etc.) • Loads correct template file • Outputs the content within the theme template

Slide 10

Slide 10 text

@wpmark https://highrise.digital @Rarst https://www.rarst.net/ wordpress/wordpress-core- load/

Slide 11

Slide 11 text

@wpmark https://highrise.digital How do they work?

Slide 12

Slide 12 text

@wpmark https://highrise.digital They are a bit like a shopping list

Slide 13

Slide 13 text

@wpmark https://highrise.digital Shopping List Bread Milk Cereal Biscuits

Slide 14

Slide 14 text

@wpmark https://highrise.digital Shopping List Bread Milk Cereal Biscuits Beer

Slide 15

Slide 15 text

@wpmark https://highrise.digital Shopping List Bread Milk Cereal Biscuits Beer Wine

Slide 16

Slide 16 text

@wpmark https://highrise.digital Shopping List Bread Milk Biscuits Wine

Slide 17

Slide 17 text

@wpmark https://highrise.digital Hooks Actions & Filters

Slide 18

Slide 18 text

@wpmark https://highrise.digital Hooks Actions & Filters

Slide 19

Slide 19 text

@wpmark https://highrise.digital do_action(); add_action(); remove_action();

Slide 20

Slide 20 text

@wpmark https://highrise.digital do_action( 'action_name', $args );

Slide 21

Slide 21 text

@wpmark https://highrise.digital add_action( 'action_name', $function, $priority, $accepted_args );

Slide 22

Slide 22 text

@wpmark https://highrise.digital Action Example

Slide 23

Slide 23 text

@wpmark https://highrise.digital save_post(); save_post_{$post_type};

Slide 24

Slide 24 text

@wpmark https://highrise.digital /wp-includes/post.php do_action( 'save_post', $post_ID, $post, $update );

Slide 25

Slide 25 text

@wpmark https://highrise.digital /wp-includes/post.php do_action( 'save_post', $post_ID, $post, $update ); function wpmark_on_save_post( $post_ID, $post, $update ) { update_post_meta( $post_ID, ‘meta_key’, ‘meta_value’ ); } add_action( ‘save_post', ‘wpmark_on_post_save’, 10, 3 );

Slide 26

Slide 26 text

@wpmark https://highrise.digital /wp-includes/post.php do_action( 'save_post', $post_ID, $post, $update ); function wpmark_on_save_post( $post_ID, $post, $update ) { update_post_meta( $post_ID, ‘meta_key’, ‘meta_value’ ); } add_action( ‘save_post', ‘wpmark_on_post_save’, 10, 3 ); do_action( ‘save_post_product’, $post_ID, $post, $update );

Slide 27

Slide 27 text

@wpmark https://highrise.digital Hooks Actions & Filters

Slide 28

Slide 28 text

@wpmark https://highrise.digital apply_filters(); add_filter(); remove_filter();

Slide 29

Slide 29 text

@wpmark https://highrise.digital apply_filters( 'filter_name', 'filterable_thing', $args );

Slide 30

Slide 30 text

@wpmark https://highrise.digital add_filter( 'filter_name', $function, $priority, $accepted_args );

Slide 31

Slide 31 text

@wpmark https://highrise.digital Filter Example

Slide 32

Slide 32 text

@wpmark https://highrise.digital

Slide 33

Slide 33 text

@wpmark https://highrise.digital /wp-admin/edit-form-advanced.php apply_filters( 'enter_title_here', __( 'Enter title here' ), $post );

Slide 34

Slide 34 text

@wpmark https://highrise.digital function wpmark_title_here( $title, $post ) { if ( 'post' === get_post_type( $post ) ) { $title = __( 'Article Title' ); } return $title; } add_action( 'enter_title_here', ‘wpmark_title_here’,10, 2 ); apply_filters( 'enter_title_here', __( 'Enter title here' ), $post ); /wp-admin/edit-form-advanced.php

Slide 35

Slide 35 text

@wpmark https://highrise.digital function wpmark_title_here( $title, $post ) { if ( 'post' === get_post_type( $post ) ) { $title = __( 'Article Title' ); } return $title; } add_action( 'enter_title_here', ‘wpmark_title_here’, 10, 2 ); apply_filters( 'enter_title_here', __( 'Enter title here' ), $post ); /wp-admin/edit-form-advanced.php

Slide 36

Slide 36 text

@wpmark https://highrise.digital Making your own code extensible

Slide 37

Slide 37 text

@wpmark https://highrise.digital Setting up arrays $job_posts = new WP_Query( array( ‘post_type’ => ‘wpmark_job’, ‘posts_per_page => 12 ) );

Slide 38

Slide 38 text

@wpmark https://highrise.digital Setting up arrays $job_posts = new WP_Query( apply_filters( ‘wpmark_job_post_query_args’, array( ‘post_type’ => ‘wpmark_job’, ‘posts_per_page => 12 ) ) );

Slide 39

Slide 39 text

@wpmark https://highrise.digital In Loops while ( $job_posts->have_posts() ) : $job_posts->the_post(); do_action( ‘wpmark_job_post’, $post ); endwhile;

Slide 40

Slide 40 text

@wpmark https://highrise.digital Function returns function wpmark_sub_title( $post_id = 0 ) { return get_post_meta( $post_id, ‘subtitle’, true ); }

Slide 41

Slide 41 text

@wpmark https://highrise.digital Function returns function wpmark_sub_title( $post_id = 0 ) { return apply_filters( ‘wpmark_sub_title’, get_post_meta( $post_id, ‘subtitle, true ), $post_id ); }

Slide 42

Slide 42 text

@wpmark https://highrise.digital Recap

Slide 43

Slide 43 text

@wpmark https://highrise.digital do_action(); apply_filters();

Slide 44

Slide 44 text

@wpmark https://highrise.digital add_action(); add_filter();

Slide 45

Slide 45 text

@wpmark https://highrise.digital Remember Filters - always return the ‘filterable thing’!

Slide 46

Slide 46 text

@wpmark https://highrise.digital Try to make your own code extensible Using do_action() & apply_filters()

Slide 47

Slide 47 text

@wpmark https://highrise.digital Thank you for listening Questions? @wpmark