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

The Fundamentals of Creating Sound Plugins

The Fundamentals of Creating Sound Plugins

Thanks to WordPress core making plugins to add functionality is not that difficult. However for each plugin you make, there are some sound principles you should follow in order to make your plugin the best it can be.

This talk will teach you some of the principles to follow when making your plugins, which will make it easier for you to develop and upgrade your plugin, as well as allowing other developers to make changes in a safer way and keep things secure. Ideal for beginners and intermediate developers in plugin development.

Mark Wilkinson

October 28, 2017
Tweet

More Decks by Mark Wilkinson

Other Decks in Technology

Transcript

  1. https://highrise.digital
    @wpmark
    The Fundamentals of
    Creating Sound Plugins
    Mark Wilkinson

    WordCamp Manchester 2017

    View Slide

  2. https://highrise.digital
    @wpmark
    #1 Security

    View Slide

  3. https://highrise.digital
    @wpmark
    sanitize_*()
    sanitize_text_field() / sanitize_email()
    sanitize_option() / sanitize_title_with_dashes()
    sanitize_option()

    View Slide

  4. https://highrise.digital
    @wpmark
    esc_*()
    esc_url() / esc_html()
    esc_attr() / esc_textarea()
    esc_js()

    View Slide

  5. https://highrise.digital
    @wpmark
    Others
    wp_kses_post()
    absint() / intval()
    wp_redirect() / wp_safe_redirect()
    $wpdb->* / is_email()
    array_map( 'absint', $array )

    View Slide

  6. https://highrise.digital
    @wpmark

    View Slide

  7. https://highrise.digital
    @wpmark

    $title = sanitize_text_field( $_POST['title'] );
    update_post_meta( $post_id, 'title', $title );

    View Slide

  8. https://highrise.digital
    @wpmark

    $title = sanitize_text_field( $_POST['title'] );
    update_post_meta( $post_id, 'title', $title );
    $title = get_post_meta( $post_id, 'title', true );
    echo esc_html( $title );

    View Slide

  9. https://highrise.digital
    @wpmark
    #2
    Reliability / Scalability

    View Slide

  10. https://highrise.digital
    @wpmark
    Watch out for
    notices & warnings
    'WP_DEBUG' = true

    View Slide

  11. https://highrise.digital
    @wpmark

    View Slide

  12. https://highrise.digital
    @wpmark
    Efficient Queries
    posts_per_page = -1

    View Slide

  13. https://highrise.digital
    @wpmark
    Efficient Queries
    posts_per_page = -1
    posts_per_page = 100

    View Slide

  14. https://highrise.digital
    @wpmark
    Efficient Queries
    posts_per_page = -1
    posts_per_page = 100
    meta_query = array( … )

    View Slide

  15. https://highrise.digital
    @wpmark
    Efficient Queries
    posts_per_page = -1
    posts_per_page = 100
    meta_query = array( … )
    tax_query = array( … )

    View Slide

  16. https://highrise.digital
    @wpmark
    Efficient Queries
    posts_per_page = -1
    posts_per_page = 100
    meta_query = array( … )
    tax_query = array( … )
    https://10up.github.io/Engineering-Best-Practices/php/

    View Slide

  17. https://highrise.digital
    @wpmark
    Complex Queries
    Cache ‘em
    get_transient()
    set_transient()
    wp_cache_set()
    wp_cache_get()

    View Slide

  18. https://highrise.digital
    @wpmark
    Don’t re-invent
    the wheel!
    Use WordPress functions

    View Slide

  19. https://highrise.digital
    @wpmark
    #3 Maintainability

    View Slide

  20. https://highrise.digital
    @wpmark
    Activation / De-activation
    register_activation_hook(
    __FILE__,
    ‘hd_function_to_run’
    );

    View Slide

  21. https://highrise.digital
    @wpmark
    Activation / De-activation
    register_deactivation_hook(
    __FILE__,
    ‘hd_function_to_run’
    );

    View Slide

  22. https://highrise.digital
    @wpmark
    uninstall.php
    Runs when a user deletes the plugin
    Clean up options, removing DB tables
    https://developer.wordpress.org/plugins/the-basics/uninstall-methods/

    View Slide

  23. https://highrise.digital
    @wpmark
    Use functions / methods
    get_option( 'hd_version' );

    View Slide

  24. https://highrise.digital
    @wpmark
    Use functions / methods
    get_option( 'hd_version' );
    hd_get_version();

    View Slide

  25. https://highrise.digital
    @wpmark
    #4 Compatibility

    View Slide

  26. https://highrise.digital
    @wpmark
    prefix_all
    get_version();

    View Slide

  27. https://highrise.digital
    @wpmark
    prefix_all
    get_version();
    hd_get_version();

    View Slide

  28. https://highrise.digital
    @wpmark
    Enqueue Scripts / Styles
    wp_enqueue_script();
    wp_enqueue_style();

    View Slide

  29. https://highrise.digital
    @wpmark
    #5 Extensibility
    https://wordpress.tv/2014/08/29/mark-wilkinson-easy-extensible-plugins/

    View Slide

  30. https://highrise.digital
    @wpmark
    Template Overrides
    if ( file_exists( STYLESHEETPATH . '/file.php' ) {
    // load template from theme.
    } else {
    // load template from plugin.
    }

    View Slide

  31. https://highrise.digital
    @wpmark
    Function Overrides
    if ( ! function_exists( 'hd_function' ) {
    function hd_function() {
    // some function stuff!
    }
    }

    View Slide

  32. https://highrise.digital
    @wpmark
    Actions & Filters
    do_action();
    apply_filters();

    View Slide

  33. https://highrise.digital
    @wpmark
    #6 Usability

    View Slide

  34. https://highrise.digital
    @wpmark

    View Slide

  35. https://highrise.digital
    @wpmark

    View Slide

  36. https://highrise.digital
    @wpmark
    Mark Wilkinson
    WordPress Developer & Co-founder @ Highrise Digital
    https://highrise.digital
    @wpmark

    View Slide