Writing Your First WordPress Plugin or, What I wish someone had told me because it would have prevented me from banging my...
Who am I? – early 2009 <ul><li>Instructional Technologist, Queens College, CUNY </li></ul><ul><li>Philosopher </li></ul><u...
Who am I? – early 2010 <ul><li>Developer, CUNY Academic Commons  http://commons.gc.cuny.edu   </li></ul><ul><li>A zillion ...
1) The structure of WordPress
WordPress is modular Separable pieces: classes, functions, template files Serial: Everything happens in a certain order
WordPress is modular Plugins : New classes, functions, or template files that either replace or augment WP “core”
2) Plugin basics
Where do plugins live? <ul><li>Ask yourself: </li></ul><ul><li>Is my plugin specific to a theme? </li></ul><ul><ul><li>wp-...
Let WP know you exist <?php /* Plugin Name: Boone’s Plugin Plugin URI: http://teleogistic.net Description: This plugin wil...
3) Example: Shortcode
Shortcodes fill in for frequently-typed stuff Say you typed your address a lot:
The WordPress Shortcode API function address_shortcode() { return '<p>Boone Gorges<br /> 24 Gumdrop Lane<br /> Apartment #...
Soup that baby up function address_shortcode($atts) { extract(shortcode_atts(array( 'location' => 'MD' ), $atts)); if ( $a...
There's a Widgets API too See  http://codex.wordpress.org/Widgets_API
4) Example: Hooks
Actions are hooks In the core: do_action( 'loop_start' ); In your plugin: add_action( 'loop_start', 'my_code' );
Example: A hello message  function well_hello_there() { global $user_identity; if ( is_user_logged_in() ) echo &quot;Welco...
Filters are hooks… with benefits In the core: $content = apply_filters('the_content', $content); In your plugin: add_filte...
Example: Makin' it 1337  function make_it_leet( $content ) { $leet_content = str_replace( 'e', '3', $content ); return $le...
5) The plugin dev's toolbox
Resources on the web The WordPress Codex, especially: -  http://codex.wordpress.org/Shortcode_API   -  http://codex.wordpr...
Resources on your computer A good text editor, ideally one with - the ability to save over FTP - good search-and-replace -...
Words of advice The White Screen Of Death - Usually means you have a syntax error (check your punctuation!) - No duplicate...
Booyah! Boone Gorges @boonebgorges [email_address] http://teleogistic.net
Upcoming SlideShare
Loading in...5
×

Wordpress Meetup 2 23 10

2,418

Published on

Writing your first WordPress plugin

Published in: Technology, Business
1 Comment
3 Likes
Statistics
Notes
  • I was unable to attend the Meetup; thank you so much for posting these slides.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
2,418
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
13
Comments
1
Likes
3
Embeds 0
No embeds

No notes for slide

Wordpress Meetup 2 23 10

  1. 1. Writing Your First WordPress Plugin or, What I wish someone had told me because it would have prevented me from banging my head on the desk a lot Boone Gorges @boonebgorges [email_address] http://teleogistic.net
  2. 2. Who am I? – early 2009 <ul><li>Instructional Technologist, Queens College, CUNY </li></ul><ul><li>Philosopher </li></ul><ul><li>Good HTML and CSS knowledge. Some scripting experience </li></ul><ul><li>Moderately high awesomeness </li></ul>
  3. 3. Who am I? – early 2010 <ul><li>Developer, CUNY Academic Commons http://commons.gc.cuny.edu </li></ul><ul><li>A zillion plugins in WP repo http://wordpress.org/extend/plugins/profile/boonebgorges </li></ul><ul><li>Extreme awesomeness </li></ul>
  4. 4. 1) The structure of WordPress
  5. 5. WordPress is modular Separable pieces: classes, functions, template files Serial: Everything happens in a certain order
  6. 6. WordPress is modular Plugins : New classes, functions, or template files that either replace or augment WP “core”
  7. 7. 2) Plugin basics
  8. 8. Where do plugins live? <ul><li>Ask yourself: </li></ul><ul><li>Is my plugin specific to a theme? </li></ul><ul><ul><li>wp-content/[theme-dir]/functions.php </li></ul></ul><ul><li>Should my plugin activate on all blogs? (MU only) </li></ul><ul><ul><li>wp-content/mu-plugins/my-plugin.php </li></ul></ul><ul><li>Otherwise </li></ul><ul><ul><li>wp-content/plugins/my-plugin/my-plugin.php </li></ul></ul>
  9. 9. Let WP know you exist <?php /* Plugin Name: Boone’s Plugin Plugin URI: http://teleogistic.net Description: This plugin will blow your mind Author: boonebgorges Version: 1.0 Author URI: http://teleogistic.net */ wp-content/plugins/boones-plugin/boones-plugin.php See http://codex.wordpress.org/Writing_a_Plugin#File_Headers
  10. 10. 3) Example: Shortcode
  11. 11. Shortcodes fill in for frequently-typed stuff Say you typed your address a lot:
  12. 12. The WordPress Shortcode API function address_shortcode() { return '<p>Boone Gorges<br /> 24 Gumdrop Lane<br /> Apartment #32<br /> Sugarlove, MD 20020</p>'; } add_shortcode( 'bgaddress', 'address_shortcode' ); wp-content/plugins/boones-plugin/boones-plugin.php See http://codex.wordpress.org/Shortcode_API
  13. 13. Soup that baby up function address_shortcode($atts) { extract(shortcode_atts(array( 'location' => 'MD' ), $atts)); if ( $atts['location'] == 'MD' ) return '<p>Boone Gorges<br /> 24 Gumdrop Lane<br /> Apartment #32<br /> Sugarlove, MD 20020</p>'; else return '<p>Boone Gorges<br /> 85 Lollipop Street<br /> Apartment #81<br /> Milwaukee, WI 54112</p>'; } add_shortcode( 'bgaddress', 'address_shortcode' ); wp-content/plugins/boones-plugin/boones-plugin.php See http://codex.wordpress.org/Shortcode_API
  14. 14. There's a Widgets API too See http://codex.wordpress.org/Widgets_API
  15. 15. 4) Example: Hooks
  16. 16. Actions are hooks In the core: do_action( 'loop_start' ); In your plugin: add_action( 'loop_start', 'my_code' );
  17. 17. Example: A hello message function well_hello_there() { global $user_identity; if ( is_user_logged_in() ) echo &quot;Welcome, &quot; . $user_identity; else echo &quot;Welcome, anonymous lurker!&quot;; } add_action( 'loop_start', 'well_hello_there' ); wp-content/plugins/boones-plugin/boones-plugin.php See http://codex.wordpress.org/Plugin_API/Action_Reference
  18. 18. Filters are hooks… with benefits In the core: $content = apply_filters('the_content', $content); In your plugin: add_filter( 'the_content', 'my_code' );
  19. 19. Example: Makin' it 1337 function make_it_leet( $content ) { $leet_content = str_replace( 'e', '3', $content ); return $leet_content; } add_filter( 'the_content', 'make_it_leet' ); wp-content/plugins/boones-plugin/boones-plugin.php See http://codex.wordpress.org/Plugin_API/Filter_Reference
  20. 20. 5) The plugin dev's toolbox
  21. 21. Resources on the web The WordPress Codex, especially: - http://codex.wordpress.org/Shortcode_API - http://codex.wordpress.org/Widgets_API - http://codex.wordpress.org/Plugin_API The WordPress PHP Cross-Reference - http://xref.yoast.com The WordPress plugin repository: - http://wordpress.org/extend/plugins to download - http://wordpress.org/extend/plugins/about to add your own plugin PHP resources - W3Schools – http://www.w3schools.com/PHP/ - php.net – http://www.php.net/manual/en
  22. 22. Resources on your computer A good text editor, ideally one with - the ability to save over FTP - good search-and-replace - code highlighting - see http://lifehacker.com/385929/best-text-editors for suggestions A local development environment - AMP: Apache, MySQL, PHP - Windows: XAMPP; Mac: MAMP - Use SVN to get the most recent copies of WordPress
  23. 23. Words of advice The White Screen Of Death - Usually means you have a syntax error (check your punctuation!) - No duplicate function names, except for functions in pluggable.php - Use if ( function_exists( 'my_function' ) ) Get to know your data - Learn about globals: $wp, $post, $comment - var_dump( $wp ); - print &quot;<pre>&quot;; print_r( $wp ); print &quot;</pre>&quot;; - die(); Pick apart existing plugins Ask for help: http://wordpress.org/support
  24. 24. Booyah! Boone Gorges @boonebgorges [email_address] http://teleogistic.net
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×