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 Slide

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

    View Slide

  3. SECOND
    The query isn’t

    available until wp

    View Slide

  4. THIRD
    Never build a

    manual link again

    View 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 Slide

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

    View Slide

  7. Link Functions
    •home_url(  '/'  )




    Many more in

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

    View Slide

  8. FOURTH
    Escaping and

    Sanitization

    View Slide

  9. FIFTH
    Nonces

    View Slide

  10. SIXTH
    Ajax is easy

    with WordPress

    View Slide

  11. 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 Slide

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

    View Slide

  13. SEVENTH
    Database Interactions

    View Slide

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

    View Slide

  15. 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 Slide

  16. EIGHTH
    Enqueue All

    The Things

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  20. NINTH
    Cache All

    The Things

    View Slide

  21. Caching
    • Transients
    • Object Cache

    View Slide

  22. TENTH
    Remote Requests

    View Slide

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

    View Slide

  24. ELEVENTH
    Miscellany

    View Slide

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

    View Slide

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

    View Slide