How to writing extensible WordPress code

31af50af29856a3e6734bdae0a0d68cf?s=47 Mark Wilkinson
September 19, 2018

How to writing extensible WordPress code

Our Co-founder and WordPress developer, Mark Wilkinson spoke to the WordPress Cheltenham meetup group about writing extensible WordPress code. This is code that others can extend and modify in a safe way.

Before viewing this slide deck, it is worth making sure you understand how WordPress hooks work. This is a blog post we previously wrote which may be of use.

https://highrise.digital/blog/wordpress-hooks-action-filters/

31af50af29856a3e6734bdae0a0d68cf?s=128

Mark Wilkinson

September 19, 2018
Tweet

Transcript

  1. How to write extensible WordPress code Mark Wilkinson Co-founder and

    developer at Highrise Digital https://highrise.digital @wpmark
  2. What is extensible WordPress code?

  3. “ capable of being extended - dictionary.com

  4. “ Code which can be modified or extended, without changing

    the code itself. - Mark Wilkinson
  5. Why write extensibly?

  6. So, how do I write extensible WordPress code?

  7. Use the force core

  8. Use the force core How does WordPress core do extensibility?

  9. Hooks

  10. Hooks Actions & Filters

  11. https://highrise.digital/blog/wordpress-hooks-action-filters /

  12. Actions do_action()

  13. do_action( ‘my_action_name’ );

  14. Filters apply_filters()

  15. apply_filters( ‘my_filter_name’, $filterable_thing );

  16. None
  17. apply_filters( 'enter_title_here', __( 'Enter title here' ), $post );

  18. function hd_title_here( $title, $post ) { if ( 'post' ===

    get_post_type( $post ) ) { $title = 'Article Title'; } return $title; } add_filter( 'enter_title_here', 'hd_title_here', 10, 2 );
  19. Implementing this in your own code

  20. Arrays of data HTML Output Function returns

  21. Arrays of data HTML Output Function returns

  22. $job_posts = new WP_Query( array( ‘posts_per_page => 6, ‘post_type =>

    ‘hd_job’, ) );
  23. $job_posts = new WP_Query( apply_filters( ‘hd_job_post_query_args’, array( ‘posts_per_page => 6,

    ‘post_type => ‘hd_job’, ) ) );
  24. Arrays of data HTML Output Function returns

  25. while ( $job_posts->have_posts() ) : $job_posts->the_post(); ?> <h1><?php the_title(); ?></h1>

    <?php endwhile;
  26. while ( $job_posts->have_posts() ) : $job_posts->the_post(); do_action( ‘hd_before_job_post’ ); ?>

    <h1><?php the_title(); ?></h1> <?php do_action( ‘hd_after_job_post’ ); endwhile;
  27. while ( $job_posts->have_posts() ) : $job_posts->the_post(); do_action( ‘hd_job_post’, $post );

    endwhile;
  28. function hd_job_post_content( $post ) { ?> <h1><?php the_title(); ?></h1> <?php

    } add_action( ‘hd_job_post’, ‘hd_job_post_content’, 10, 1 );
  29. remove_action( ‘hd_job_post’, ‘hd_job_post_content’, 10, 1 );

  30. Arrays of data HTML Output Function returns

  31. function hd_sub_title( $post_id = 0 ) { return get_post_meta( $post_id,

    ‘subtitle’, true ); }
  32. function hd_sub_title( $post_id = 0 ) { return apply_filters( ‘hd_sub_title’,

    get_post_meta( $post_id, ‘subtitle’, true ), $post_id ); }
  33. Template overrides

  34. Template overrides https://github.com/GaryJones/Gamajo-Template-Loader

  35. Pluggable Functions

  36. if ( ! function_exists( ‘hd_function’ ) ) { function hd_function()

    { // awesome functions stuff here! } }
  37. Mark Wilkinson Co-founder & developer at Highrise Digital T: @wpmark

    & @highrisedigital W: markwilkinson.me & highrise.digital