O slideshow foi denunciado.
Utilizamos seu perfil e dados de atividades no LinkedIn para personalizar e exibir anúncios mais relevantes. Altere suas preferências de anúncios quando desejar.

Introduction to plugin development

64.208 visualizações

Publicada em

Slides from my WordCamp Tampa 2016 presentation -- introduction to WordPress plugin development.

Publicada em: Internet
  • Seja o primeiro a comentar

Introduction to plugin development

  1. 1. @Josh412 Introduction To Plugin Development Josh Pollock | CalderaLabs.org
  2. 2. @Josh412 CalderaLabs.org Hi I'm Josh Lead Developer: CalderaWP I make WordPress plugins I teach about WordPress I wrote a book about the WordPress REST API I am core contributor to WordPress I am a member of The WPCrowd
  3. 3. @Josh412 CalderaLabs.org You Should Make Plugins
  4. 4. @Josh412 CalderaLabs.org Turn Code Off Easy troubleshooting!
  5. 5. @Josh412 CalderaLabs.org Reuse Code Share Between Projects
  6. 6. @Josh412 CalderaLabs.org Share Code Distribute on Github or WordPress.org
  7. 7. @Josh412 CalderaLabs.org Make $$ Sell Plugins
  8. 8. @Josh412 CalderaLabs.org What Is A Plugin?
  9. 9. @Josh412 How To Make A Plugin Step 1: Add a new directory to the plugins directory (optional). Step 2: Add a file in that directory or the plugins directory with a valid plugin header. Step 3: Write code (optional).
  10. 10. @Josh412 CalderaLabs.org Plugins Are Containers For Code
  11. 11. @Josh412 CalderaLabs.org Plugins vs Themes Or Can’t I Put That In functions.php ??
  12. 12. @Josh412 CalderaLabs.org It’s a free software, you can do whatever you want, but...
  13. 13. @Josh412 Plugins vs Themes: Best Practices Themes should be use to present a unique design Everything else should be in a plugin.
  14. 14. @Josh412 Should This Code Go In functions.php ? Is the answer to any of these questions no: Do I ever want to reuse this code apart from this theme? Will I miss this code when I switch themes? Switch for troubleshooting. Switch for new look. Can I use this code for other purposes?
  15. 15. @Josh412 CalderaLabs.org Making Plugins Organization
  16. 16. @Josh412 Plugin Header: Goes In Main File <?php /* Plugin Name: My Plugin */
  17. 17. @Josh412 Main File: The File With A Plugin Header Plugin Header License/ Copyright/ Contact Info Check Dependencies & Requirements Load Plugin Start Plugin
  18. 18. @Josh412 Directory Structure There Are No Rules You Should Have Rules I like PSR-4 :)
  19. 19. @Josh412 Naming Things There Are No Rules You Should Have Rules I like to document naming conventions:)
  20. 20. @Josh412 Naming Things: naming-conventions.txt Plugin Name -- Text domain -- Function prefix -- Class prefix -- Root namespace -- Hook Prefix --
  21. 21. @Josh412 CalderaLabs.org Making Plugins The Plugins API
  22. 22. @Josh412 CalderaLabs.org Hooks Are Events When We Get Here: Do This
  23. 23. @Josh412 CalderaLabs.org Don’t Hack Core Modifying Core/ Plugins / Themes Is A Bad Idea You Will Lose Your Changes On Update
  24. 24. @Josh412 CalderaLabs.org Hooks Let You Change WordPress/ Plugins/ Themes Without Editing Files
  25. 25. @Josh412 CalderaLabs.org Filters Change A Variable’s Value At A Specific Time
  26. 26. @Josh412 CalderaLabs.org Actions Do Something At A Specific Time
  27. 27. @Josh412 Actions Do something Don’t return values* Actions Vs Filters Filters Change something Must return a value*
  28. 28. @Josh412 CalderaLabs.org Use Hooks In Your Plugins To Add New Functionality
  29. 29. @Josh412 CalderaLabs.org Add Hooks In Your Plugins To Make Your Plugin Extensible
  30. 30. @Josh412 do_action() Triggers the action Using Actions add_action() Hooks a function to an action
  31. 31. @Josh412 Using Actions: Print Tracking Pixel function my_prefix_pixel() { echo '<img src="https://adnetwork.com/pixel.gif" width="0" height="0">'; } add_action( 'wp_footer', 'my_prefix_pixel' );
  32. 32. @Josh412 Using Actions: Modify WP_Query function search_filter($query) { if ( !is_admin() && $query->is_main_query() ) { if ($query->is_search) { $query->set('post_type', array( 'post', 'movie' ) ); } } } add_action('pre_get_posts','search_filter');
  33. 33. @Josh412 Using Actions
  34. 34. @Josh412 apply_filters() Creates the filter Using Filters add_filter() Hooks a function to a filter
  35. 35. @Josh412 Using Filters: Add To Content add_filter( 'the_content', 'slug_callback' ); function slug_callback( $content ){ return $content . '<p class="cta">Hey Sign Up for My Thing!</p>'; }
  36. 36. @Josh412 Using Filters: Add To Content add_filter( 'the_content', function( $content ){ return $content . '<p class="cta">Hey Sign Up for My Thing!</p>'; });
  37. 37. @Josh412 Creating Filters: Before function prefix_get_form( $id ){ return get_option( $id, [] ); }
  38. 38. @Josh412 Creating Filters: After function prefix_get_form( $id ){ $form = get_option( $id, [] ); $form = apply_filters( 'prefix_get_form', $form, $id ); return $form; }
  39. 39. @Josh412 Hook Priority add_filter( 'name','callback', 2 ); add_filter( 'name', 'callback_two', 55 );
  40. 40. @Josh412 Hook Arguments $form = apply_filters( 'prefix_get_form', $form, $id ); add_filter( 'prefix_get_form', function( $form, $id ){ if( 'CF1234567' == $id ){ // do something to $form } return $form; }, 10, 2 );
  41. 41. @Josh412 CalderaLabs.org Making Plugins Josh’s Rules For Plugin Development
  42. 42. @Josh412 CalderaLabs.org Security Is Not Optional
  43. 43. @Josh412 CalderaLabs.org Trust No Inputs
  44. 44. @Josh412 HTTP requests That functions will be used how you intended them Data returned from the database Yourself Some Things Not To Trust
  45. 45. @Josh412 CalderaLabs.org Write For Reuse
  46. 46. @Josh412 CalderaLabs.org Use Version Control
  47. 47. @Josh412 CalderaLabs.org Commit Early Commit Often
  48. 48. @Josh412 Single Responsibility Principle Functions should do one thing. Do Not Repeat Yourself (DRY) Functions, not copypaste Making Code Reusable
  49. 49. @Josh412 CalderaLabs.org Abstraction!!
  50. 50. @Josh412 CalderaLabs.org Using Classes Doesn’t Make Your Code Object-Oriented
  51. 51. @Josh412 CalderaLabs.org Use Object-Oriented Programming When Appropriate
  52. 52. @Josh412 CalderaLabs.org Small Simple Classes Are Good
  53. 53. @Josh412 CalderaLabs.org Josh Pollock JoshPress.net CalderaLabs.org CalderaWP.com IngotHQ.com @Josh412