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

How to writing extensible WordPress code

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/

Mark Wilkinson

September 19, 2018
Tweet

More Decks by Mark Wilkinson

Other Decks in Education

Transcript

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

    View full-size slide

  2. What is extensible
    WordPress code?

    View full-size slide


  3. capable of being extended
    - dictionary.com

    View full-size slide


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

    View full-size slide

  5. Why write extensibly?

    View full-size slide

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

    View full-size slide

  7. Use the force core

    View full-size slide

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

    View full-size slide

  9. Hooks
    Actions & Filters

    View full-size slide

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

    View full-size slide

  11. Actions
    do_action()

    View full-size slide

  12. do_action(
    ‘my_action_name’
    );

    View full-size slide

  13. Filters
    apply_filters()

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  17. Implementing this in your
    own code

    View full-size slide

  18. Arrays of data
    HTML Output
    Function returns

    View full-size slide

  19. Arrays of data
    HTML Output
    Function returns

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  22. Arrays of data
    HTML Output
    Function returns

    View full-size slide

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

    endwhile;

    View full-size slide

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

    do_action( ‘hd_after_job_post’ );
    endwhile;

    View full-size slide

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

    View full-size slide

  26. function hd_job_post_content( $post ) {
    ?>

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

    View full-size slide

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

    View full-size slide

  28. Arrays of data
    HTML Output
    Function returns

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  31. Template overrides

    View full-size slide

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

    View full-size slide

  33. Pluggable Functions

    View full-size slide

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

    View full-size slide

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

    View full-size slide