Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Beginning WordPress Plugin Development

17,499 views

Published on

Presented at WordCamp Malaysia 2010.

Slideshare also does not resize my cropped images properly, thus resulting in squished images. This is noticeable on my squished code.

Published in: Technology, Business

Beginning WordPress Plugin Development

  1. 1. Beginning WordPress Plugin Development Aizat Faiz aizat.faiz@gmail.com
  2. 2. Download and view at http://blog.aizatto.com/?p=3729
  3. 3. Creative Commons http://www.flickr.com/photos/kwl/4435490471/
  4. 4. By Attribution http://www.flickr.com/photos/balakov/4571468943/
  5. 5. Aizat Faiz http://www.flickr.com/photos/byte/3397174843/ [email_address]
  6. 6. Your Friends / Resources http://www.flickr.com/photos/balakov/4571468943/
  7. 7. WordPress Codex <ul>http://codex.wordpress.org </ul>
  8. 8. WordPress Plugin Directory
  9. 9. Google <ul>http://www.google.com.my </ul>
  10. 10. What is a Hook? http://www.flickr.com/photos/kwl/4247555680/
  11. 11. WordPress is the Base http://www.flickr.com/photos/kwl/4247555680/
  12. 12. Plugins are the Blocks http://www.flickr.com/photos/sixteen-miles/3757674953/
  13. 13. Hooks are the Pegs http://www.flickr.com/photos/linuxmatt/92802487/
  14. 14. Actions Filters 2 Kinds of WordPress Hooks
  15. 15. Actions
  16. 16. Actions Actions are the hooks that the WordPress core launches at specific points during execution, or when specific events occur. Your plugin can specify that one or more of its PHP functions are executed at these points, using the Action API. <ul>http://codex.wordpress.org/Plugin_API#Actions </ul>http://codex.wordpress.org/Plugin_API/Action_Reference
  17. 17. Actions Actions = “Do Something”
  18. 18. Filters
  19. 19. Filters Filters are functions that WordPress passes data through, at certain points in execution, just before taking some action with the data (such as adding it to the database or sending it to the browser screen). Filters sit between the database and the browser (when WordPress is generating pages), and between the browser and the database (when WordPress is adding new posts and comments to the database); most input and output in WordPress passes through at least one filter. WordPress does some filtering by default, and your plugin can add its own filtering http://codex.wordpress.org/Plugin_API#Filters http://codex.wordpress.org/Plugin_API/Filter_Reference
  20. 20. Filters Filters = Transform
  21. 21. Actions and Filters Actions = “Do Something” Filters = Transform
  22. 23. 1133
  23. 24. 1133 hooks <ul>* as of WordPress v2.9 </ul>
  24. 25. Problem: Finding the Right Hook for the Right Job
  25. 26. Hard to Recommend <ul><li>Look at the resources I gave you </li><ul><li>WordPress Codex
  26. 27. WordPress Plugin Directory
  27. 28. Google </li></ul><li>Action and Filter Reference </li><ul><li>http://codex.wordpress.org/Plugin_API/Action_Reference
  28. 29. http://codex.wordpress.org/Plugin_API/Filter_Reference </li></ul></ul>
  29. 31. Writing Your First Plugin
  30. 32. WordPress Directory Structure All Plugins are stored in: /wp-content/plugins/
  31. 33. Plugin Directory Structure <ul><li>Your Plugin: </li><ul><li>/wp-content/plugins/my-plugin </li></ul><li>Inside “ my-plugin ” </li><ul><li>readme.txt
  32. 34. screenshot-1.png
  33. 35. my-plugin.php </li></ul><li>Always put it in a directory!
  34. 36. Uses 'dashes' and not 'underscores' </li></ul>http://codex.wordpress.org/Writing_a_Plugin#Names.2C_Files.2C_and_Locations
  35. 37. readme.txt http://codex.wordpress.org/Writing_a_Plugin#Readme_File http://wordpress.org/extend/plugins/about/readme.txt Useful only for publishing to WordPress Plugin Directory Information about your plugin: Description, Installation, Changelog, Donation Links, Tags, etc...
  36. 38. screenshot-1.png Useful only for publishing to WordPress Plugin Directory
  37. 39. my-plugin.php Your Plugin Code <ul><li>4 parts to a plugin </li><ul><li>Plugin Header
  38. 40. Hooks
  39. 41. PHP Code
  40. 42. Template Code </li></ul></ul>
  41. 43. File Structure
  42. 44. Plugin Headers http://codex.wordpress.org/Writing_a_Plugin#Standard_Plugin_Information
  43. 45. Plugin Headers http://codex.wordpress.org/Writing_a_Plugin#Standard_Plugin_Information <ul><li>Always on top, no choice
  44. 46. Fill in with your own details </li></ul>
  45. 47. Hooks (Filters)
  46. 48. Hooks (Filters) <ul><li>After plugin headers (my preferance)
  47. 49. Makes it easier to find </li></ul>
  48. 50. PHP Code
  49. 52. Plugin 1 Figure out what you want to do. I want to convert all instances of “WordPress” to “WORDPRESS” in a post's content.
  50. 53. the_content (filter) http://codex.wordpress.org/Plugin_API/Filter_Reference/the_content
  51. 54. add_filter http://codex.wordpress.org/Plugin_API/Filter_Reference/the_content
  52. 55. add_filter http://codex.wordpress.org/Plugin_API#Hook_to_WordPress ② ① Hook Name ② Callback ①
  53. 56. Hook Callback <ul><li>Determines what PHP function to call
  54. 57. Callback can be either: </li><ul><li>String; or
  55. 58. Array of 2 strings (my preference) </li></ul></ul>
  56. 59. Hook Callback <ul><li>String </li><ul><li>Calls a function </li></ul><li>Array of 2 strings </li><ul><li>Calls a static function in a class </li></ul></ul>
  57. 60. They do the same thing
  58. 61. I Prefer Array Callbacks <ul><li>Allows me to segment my code
  59. 62. Lower chances of name conflicts
  60. 63. Easily tell which function belongs to which hook </li></ul>
  61. 64. Filters are Transformations filters have to return a transformation
  62. 65. Filter return Values <ul><li>A filters return value, is the result of the transformation </li></ul>② ① return ② transformation ① http://php.net/manual/en/function.preg-replace.php
  63. 67. Plugin 2 Figure out what you want to do. I want to BOLD all instances of “WORDPRESS” in a post's content.
  64. 68. Hook Priority http://codex.wordpress.org/Plugin_API#Hook_to_WordPress
  65. 69. Priority ② ② Callback ① ③ ② ② ③ Priority (optional) ① Hook Name http://codex.wordpress.org/Plugin_API#Hook_to_WordPress
  66. 70. Which one goes first? WordPress2WORDPRESS#the_content ABolderWordPress#the_content
  67. 71. Default Priority 10 smaller numbers = higher priority larger numbers = lower priority http://codex.wordpress.org/Plugin_API#Hook_to_WordPress
  68. 72. Therefore Order of execution: (10) WordPress2WORDPRESS#the_content (20) ABolderWordPress#the_content
  69. 74. Plugin 3 Figure out what you want to do. I want to add a class, to represent a post that has more than 10 comments
  70. 75. accepted_args http://codex.wordpress.org/Plugin_API#Hook_to_WordPress
  71. 76. accepted_args http://codex.wordpress.org/Plugin_API#Hook_to_WordPress ① ③ ② ④ accepted args (optional) ① Hook Name ② Callback ④ ③ Priority
  72. 77. accepted_args Number of arguments for the filter
  73. 79. Plugin 4 Figure out what you want to do. I want to add a custom stylesheet
  74. 80. init (action) http://codex.wordpress.org/Plugin_API#Hook_to_WordPress
  75. 81. wp_enqueue_style ① ③ ② ① WordPress function ② Style ID ③ Style URL http://codex.wordpress.org/Function_Reference/wp_enqueue_style
  76. 82. Actions “Do Something” Actions do not need to return anything
  77. 84. Actions Filters 2 Kinds of WordPress Hooks
  78. 85. Actions Actions = “Do Something”
  79. 86. Filters Filters = Transform
  80. 87. Actions and Filters Actions = “Do Something” Filters = Transform
  81. 89. MOAR KITTEHS
  82. 90. Beginning WordPress Plugin Development Aizat Faiz [email_address] http://blog.aizatto.com/?p=3729 kthxbai

×