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

Extensible WordPress Plugins

Extensible WordPress Plugins

Some hints and tips about how to make your plugins and themes more extensible to other developers so they can build on them or modify them safely.

Mark Wilkinson

October 15, 2014
Tweet

More Decks by Mark Wilkinson

Other Decks in Technology

Transcript

  1. markwilkinson.me | @wpmark
    Extensible WordPress
    Plugins
    WordPress Cumbria – 14th October 2014
    #wpcumbria

    View Slide

  2. mark wilkinson?

    View Slide

  3. markwilkinson.me | @wpmark
    father

    View Slide

  4. markwilkinson.me | @wpmark
    teacher

    View Slide

  5. markwilkinson.me | @wpmark
    WordPress developer

    View Slide

  6. markwilkinson.me | @wpmark

    View Slide

  7. markwilkinson.me | @wpmark
    this talk will cover
    what is extensibility?
    why should you be making extensible plugins?
    how to make your plugin extensible?
    examples of good practice

    View Slide

  8. markwilkinson.me | @wpmark
    this talk will cover
    what is extensibility?
    why should you be making extensible plugins?
    how to make your plugin extensible?
    examples of good practice

    View Slide

  9. markwilkinson.me | @wpmark
    this talk will cover
    what is extensibility?
    why should you be making extensible plugins?
    how to make your plugin extensible?
    examples of good practice

    View Slide

  10. markwilkinson.me | @wpmark
    this talk will cover
    what is extensibility?
    why should you be making extensible plugins?
    how to make your plugin extensible?
    examples of good practice

    View Slide

  11. markwilkinson.me | @wpmark
    Plugin and / or Theme

    View Slide

  12. markwilkinson.me | @wpmark
    Extensible Plugins

    View Slide

  13. markwilkinson.me | @wpmark
    “one that can be
    modified or extended
    without changing the
    plugin code itself ”

    View Slide

  14. markwilkinson.me | @wpmark
    why?

    View Slide

  15. markwilkinson.me | @wpmark
    https://www.flickr.com/photos/damndirty/10166197684

    View Slide

  16. markwilkinson.me | @wpmark
    preserve future
    updatability

    View Slide

  17. markwilkinson.me | @wpmark
    how?

    View Slide

  18. markwilkinson.me | @wpmark
    actions & filters

    View Slide

  19. markwilkinson.me | @wpmark
    do_action()

    View Slide

  20. markwilkinson.me | @wpmark
    do_action(
    $tag,
    $args
    );

    View Slide

  21. markwilkinson.me | @wpmark
    add_action()

    View Slide

  22. markwilkinson.me | @wpmark
    add_action(
    $hook,
    $function,
    $priority,
    $accepted_args
    );

    View Slide

  23. markwilkinson.me | @wpmark
    example

    View Slide

  24. markwilkinson.me | @wpmark
    $q = new WP_Query( $q_args );
    while( $q->have_posts() ) : $->the_post();
    ?>

    endwhile;

    View Slide

  25. markwilkinson.me | @wpmark
    $q = new WP_Query( $q_args );
    while( $q->have_posts() ) : $->the_post();
    do_action( ‘wpmark_before_post’, get_the_ID() );
    ?>

    endwhile;

    View Slide

  26. markwilkinson.me | @wpmark
    $q = new WP_Query( $q_args );
    while( $q->have_posts() ) : $->the_post();
    do_action( ‘wpmark_before_post’, get_the_ID() );
    ?>

    do_action( ‘wpmark_after_post’, get_the_ID() );
    endwhile;

    View Slide

  27. markwilkinson.me | @wpmark
    function wpmark_add_before_post( $post_id ) {
    $meta = get_post_meta( $post_id, ‘key’, true );
    /* do something with $meta */
    }
    add_action(
    ‘wpmark_before_post’,
    ‘wpmark_add_before_post’,
    10,
    1
    );

    View Slide

  28. markwilkinson.me | @wpmark
    apply_filters()

    View Slide

  29. markwilkinson.me | @wpmark
    apply_filters(
    $tag,
    $value,
    $var
    );

    View Slide

  30. markwilkinson.me | @wpmark
    add_filter()

    View Slide

  31. markwilkinson.me | @wpmark
    add_filter(
    $hook,
    $function,
    $priority,
    $accepted_args
    );

    View Slide

  32. markwilkinson.me | @wpmark
    example

    View Slide

  33. markwilkinson.me | @wpmark
    $q_args = array(
    'post_type' => ’post',
    'cat' => 1
    );

    View Slide

  34. markwilkinson.me | @wpmark
    $q_args = apply_filters(
    ‘wpmark_q_args’,
    array(
    ‘post_type’ => ‘post’,
    ‘cat’ => 1
    )
    );

    View Slide

  35. markwilkinson.me | @wpmark
    function wpmark_edit_q_args( $args ) {
    $args[ ‘year’ ] = ‘2014’;
    return $args;
    }
    add_filter(
    ‘wpmark_q_args’,
    ‘wpmark_edit_q_args’
    10,
    1
    );

    View Slide

  36. markwilkinson.me | @wpmark
    conclusion

    View Slide

  37. markwilkinson.me | @wpmark
    when making plugins &
    themes, remember to…

    View Slide

  38. markwilkinson.me | @wpmark
    do_action()
    &
    apply_filters()

    View Slide

  39. markwilkinson.me | @wpmark
    Thank you!
    Questions: @wpmark (or now!)
    Slides: markwilkinson.me (soon!)

    View Slide