A Deep Understanding of WordPress Actions and Filters

A Deep Understanding of WordPress Actions and Filters

WordPress is known for its extensibility – the ability to change the behaviour of the software without breaking the core code. This is added through plugins and themes.

Understanding how to extend WordPress is essential if you want to develop solutions using WordPress. This talk will teach you how actions and filters work, with some practical and theoretical examples to illustrate their functionality.

Attendees should hopefully come away from this talk with a greater understanding on how actions and filters, or hooks as they are collectively known, can unlock the door to building greater things with WordPress, and as I worked out being able to say “Wow, you can do (almost) anything with WordPress!”.

31af50af29856a3e6734bdae0a0d68cf?s=128

Mark Wilkinson

July 22, 2017
Tweet

Transcript

  1. @wpmark https://highrise.digital A Deep Understanding of WordPress Actions and Filters

    Mark Wilkinson (@wpmark) WordCamp Edinburgh July 2017
  2. @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
  3. @wpmark https://highrise.digital What are actions/filters? How do they work? Examples

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

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

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

    Making your code extensible
  7. @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
  8. @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
  9. @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
  10. @wpmark https://highrise.digital @Rarst https://www.rarst.net/ wordpress/wordpress-core- load/

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

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

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

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

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

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

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

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

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

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

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

  22. @wpmark https://highrise.digital Action Example

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

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

  25. @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 );
  26. @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 );
  27. @wpmark https://highrise.digital Hooks Actions & Filters

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

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

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

  31. @wpmark https://highrise.digital Filter Example

  32. @wpmark https://highrise.digital

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

    $post );
  34. @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
  35. @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
  36. @wpmark https://highrise.digital Making your own code extensible

  37. @wpmark https://highrise.digital Setting up arrays $job_posts = new WP_Query( array(

    ‘post_type’ => ‘wpmark_job’, ‘posts_per_page => 12 ) );
  38. @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 ) ) );
  39. @wpmark https://highrise.digital In Loops while ( $job_posts->have_posts() ) : $job_posts->the_post();

    do_action( ‘wpmark_job_post’, $post ); endwhile;
  40. @wpmark https://highrise.digital Function returns function wpmark_sub_title( $post_id = 0 )

    { return get_post_meta( $post_id, ‘subtitle’, true ); }
  41. @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 ); }
  42. @wpmark https://highrise.digital Recap

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

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

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

  46. @wpmark https://highrise.digital Try to make your own code extensible Using

    do_action() & apply_filters()
  47. @wpmark https://highrise.digital Thank you for listening Questions? @wpmark