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

A Deep Understanding of WordPress Actions and F...

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 Mark Wilkinson Using & building WordPress sites for

    over 10 years as a WordPress developer Owner of Highrise Digital Follow me: @wpmark
  2. @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
  3. @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
  4. @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
  5. @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 );
  6. @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 );
  7. @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
  8. @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
  9. @wpmark https://highrise.digital Setting up arrays $job_posts = new WP_Query( array(

    ‘post_type’ => ‘wpmark_job’, ‘posts_per_page => 12 ) );
  10. @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 ) ) );
  11. @wpmark https://highrise.digital Function returns function wpmark_sub_title( $post_id = 0 )

    { return get_post_meta( $post_id, ‘subtitle’, true ); }
  12. @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 ); }