$30 off During Our Annual Pro Sale. View Details »

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!”.

Mark Wilkinson

July 22, 2017
Tweet

More Decks by Mark Wilkinson

Other Decks in Technology

Transcript

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

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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 );

    View Slide

  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 );

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  32. @wpmark
    https://highrise.digital

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

  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
    )
    )
    );

    View Slide

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

    View Slide

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

    View Slide

  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
    );
    }

    View Slide

  42. @wpmark
    https://highrise.digital
    Recap

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide