Slide 1

Slide 1 text

How to write extensible WordPress code Mark Wilkinson Co-founder and developer at Highrise Digital https://highrise.digital @wpmark

Slide 2

Slide 2 text

What is extensible WordPress code?

Slide 3

Slide 3 text

“ capable of being extended - dictionary.com

Slide 4

Slide 4 text

“ Code which can be modified or extended, without changing the code itself. - Mark Wilkinson

Slide 5

Slide 5 text

Why write extensibly?

Slide 6

Slide 6 text

So, how do I write extensible WordPress code?

Slide 7

Slide 7 text

Use the force core

Slide 8

Slide 8 text

Use the force core How does WordPress core do extensibility?

Slide 9

Slide 9 text

Hooks

Slide 10

Slide 10 text

Hooks Actions & Filters

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

Actions do_action()

Slide 13

Slide 13 text

do_action( ‘my_action_name’ );

Slide 14

Slide 14 text

Filters apply_filters()

Slide 15

Slide 15 text

apply_filters( ‘my_filter_name’, $filterable_thing );

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

apply_filters( 'enter_title_here', __( 'Enter title here' ), $post );

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

Implementing this in your own code

Slide 20

Slide 20 text

Arrays of data HTML Output Function returns

Slide 21

Slide 21 text

Arrays of data HTML Output Function returns

Slide 22

Slide 22 text

$job_posts = new WP_Query( array( ‘posts_per_page => 6, ‘post_type => ‘hd_job’, ) );

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

Arrays of data HTML Output Function returns

Slide 25

Slide 25 text

while ( $job_posts->have_posts() ) : $job_posts->the_post(); ?>

Slide 26

Slide 26 text

while ( $job_posts->have_posts() ) : $job_posts->the_post(); do_action( ‘hd_before_job_post’ ); ?>

Slide 27

Slide 27 text

while ( $job_posts->have_posts() ) : $job_posts->the_post(); do_action( ‘hd_job_post’, $post ); endwhile;

Slide 28

Slide 28 text

function hd_job_post_content( $post ) { ?>

Slide 29

Slide 29 text

remove_action( ‘hd_job_post’, ‘hd_job_post_content’, 10, 1 );

Slide 30

Slide 30 text

Arrays of data HTML Output Function returns

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

Template overrides

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

Pluggable Functions

Slide 36

Slide 36 text

if ( ! function_exists( ‘hd_function’ ) ) { function hd_function() { // awesome functions stuff here! } }

Slide 37

Slide 37 text

Mark Wilkinson Co-founder & developer at Highrise Digital T: @wpmark & @highrisedigital W: markwilkinson.me & highrise.digital