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

What I Wish I'd Known When I Started

What I Wish I'd Known When I Started

WordPress provides many convenient functions, perhaps too many. All too often, new developers reinvent the wheel, as the cliche goes, when Core already has a function or utility to accomplish the same. Five years on and thinking back to when I started building WordPress plugins, I could’ve saved a lot of time and avoided a lot of anxiety if I’d known where to look.

Erick Hitter

June 13, 2015
Tweet

More Decks by Erick Hitter

Other Decks in Technology

Transcript

  1. What I Wish I’d Known

    When I Started
    Erick Hitter
    @ethitter
    https://ethitter.com/

    View full-size slide

  2. FIRST
    Can’t check if the user’s
    logged in until init

    View full-size slide

  3. SECOND
    The query isn’t

    available until wp

    View full-size slide

  4. THIRD
    Never build a

    manual link again

    View full-size slide

  5. Link Functions
    •get_permalink()  
    •get_page_by_path(  'about'  )  
    •get_post_type_archive_link(  'waffles'  )  
    •user_trailingslashit()  
    •get_year_link()  
    •get_adjacent_post()

    View full-size slide

  6. Link Functions
    •add_query_arg()  
    •remove_query_arg()

    View full-size slide

  7. Link Functions
    •home_url(  '/'  )




    Many more in

    wp-­‐includes/link-­‐template.php.

    View full-size slide

  8. FOURTH
    Escaping and

    Sanitization

    View full-size slide

  9. SIXTH
    Ajax is easy

    with WordPress

    View full-size slide

  10. Ajax
    • Hook your function to one of two variable
    actions
    • Use the same action name with the request to
    admin-­‐ajax.php
    •check_ajax_referer() or use a nonce

    View full-size slide

  11. Ajax
    •wp_ajax_{$your_action}  
    •wp_ajax_nopriv_{$your_action}  
    • Hook to both if logged-in state isn’t relevant.

    View full-size slide

  12. SEVENTH
    Database Interactions

    View full-size slide

  13. Database Interactions
    • Use custom post types and custom taxonomies
    instead.
    • If you must, always $wpdb-­‐>prepare() your
    queries.

    View full-size slide

  14. Database Interactions
    •$wpdb-­‐>get_var()  
    •$wpdb-­‐>get_col()  
    •$wpdb-­‐>get_row()  
    •$wpdb-­‐>insert(  $table,  $data,  $format  )  
    •$wpdb-­‐>update(  $table,  $data,  $where,  
    $format,  $where_format  )  
    •$wpdb-­‐>query()

    View full-size slide

  15. EIGHTH
    Enqueue All

    The Things

    View full-size slide

  16. Register, then enqueue!
    •wp_register_style()  
    •wp_register_script()

    View full-size slide

  17. Enqueue!
    •wp_enqueue_style()  
    •wp_enqueue_script()

    View full-size slide

  18. But why?
    • Reusable
    • Dependencies
    • Versioning
    • Minification
    • Concatenation
    • CDN

    View full-size slide

  19. NINTH
    Cache All

    The Things

    View full-size slide

  20. Caching
    • Transients
    • Object Cache

    View full-size slide

  21. TENTH
    Remote Requests

    View full-size slide

  22. WP HTTP API
    •wp_remote_get()  
    •wp_remote_post()  
    •wp_remote_head()  
    •wp_remote_request()  
    •wp_remote_retrieve_response_code()

    View full-size slide

  23. ELEVENTH
    Miscellany

    View full-size slide

  24. Miscellany
    •get_queried_object()  
    •get_queried_object_id()  
    •wp_parse_args()

    View full-size slide

  25. Thanks
    Erick Hitter
    @ethitter
    https://ethitter.com/
    https://eth.pw/wcywg15

    View full-size slide