How Not to Build a WordPress Plugin <ul><li>Will Norris < http://willnorris.com /> </li></ul>
<ul><li>Upgradability </li></ul><ul><li>Performance </li></ul><ul><li>Security </li></ul><ul><li>Extensibility </li></ul>
Unique Function Names
Function Name Prefix <ul><li>wcpdx_activate() </li></ul><ul><li>wcpdx_deactivate() </li></ul><ul><li>wcpdx_uninstall() </l...
Class <ul><li>class WordCampPDX { </li></ul><ul><li>function activate() </li></ul><ul><li>function deactivate() </li></ul>...
Never Assume File Location
Traditional Directory Layout <ul><li>example.com/ </li></ul><ul><li>wordpress/ </li></ul><ul><li>wp-config.php </li></ul><...
Non-Traditional Layout (since WP 2.6) <ul><li>example.com/ </li></ul><ul><li>wordpress/ </li></ul><ul><li>wp-config.php </...
Plugin URL <ul><li>ur doin it wrong: </li></ul><ul><li><img src=”<?php bloginfo(‘wpurl’) ?>/wp-content/plugins/wcpdx/logo....
Plugin URL <ul><li>plugins_url() </li></ul><ul><ul><ul><li>supports WPMU plugin directory </li></ul></ul></ul><ul><ul><ul>...
Friends of plugins_url() <ul><li>site_url() </li></ul><ul><li>admin_url() </li></ul><ul><li>includes_url() </li></ul><ul><...
Including Files <ul><li>ur doin it wrong: </li></ul><ul><li>include ‘../../wp-content/...’ </li></ul><ul><li>dats bedder: ...
Find the Right Hook <ul><li>Load as late as possible, but no later </li></ul>
Admin Hooks <ul><li>ur doin it wrong: </li></ul><ul><li>add_action(‘admin_init’, ‘wcpdx_admin_init’) </li></ul><ul><li>add...
Styles and Scripts <ul><li>ur doin it wrong: </li></ul><ul><li><script rel=”<?php echo plugins_url(‘wcpdx.js’, __FILE__) ?...
Styles and Scripts <ul><li>wp_register_* and wp_enqueue_* </li></ul><ul><ul><ul><li>support dependencies </li></ul></ul></...
Add your own hooks <ul><li>A strategically placed hook covers a multitude of sins. </li></ul>
Custom Hooks <ul><li>Can do everything core WP hooks do: </li></ul><ul><ul><ul><li>event notification (actions) </li></ul>...
Custom Hooks <ul><li>do_action(‘my-action’) </li></ul><ul><li>do_action(‘my-action’, $a, $b) </li></ul><ul><li>do_action_r...
Custom Tables
Designed for Flexibility <ul><li>WordPress database supports </li></ul><ul><ul><ul><li>custom options </li></ul></ul></ul>...
Custom Post Types <ul><li>Used by WordPress core for </li></ul><ul><ul><ul><li>posts </li></ul></ul></ul><ul><ul><ul><li>p...
If it walks like a duck... <ul><li>author </li></ul><ul><li>date and time </li></ul><ul><li>title </li></ul><ul><li>conten...
Admin Settings Pages
Admin Settings Pages <ul><li>Don’t waste time processing manually </li></ul><ul><li>register_setting( ‘wcpdx’, ‘my-option’...
Admin Settings Pages <ul><li>Do you really need a dedicated page? </li></ul><ul><li>Add options to any built-in settings p...
Direct Plugin Files
Direct Plugin File Calls <ul><li>Direct HTTP request to plugin file ajax.php: </li></ul><ul><li>echo ‘<script type=”text/j...
Direct Plugin File Calls <ul><li>If ajax.php includes anything similar to: </li></ul><ul><li>require_once(‘../../../wp-loa...
WordPress Requests <ul><li>Permalink URL: </li></ul><ul><li>http://example.com/2009/01/hello-world </li></ul><ul><li>becom...
Custom WP Request <ul><li>Instead of making an AJAX call to: </li></ul><ul><li>http://example.com/wp-content/plugins/wcpdx...
Custom WP Requests <ul><li>function wcpdx_parse_request($wp) { </li></ul><ul><li>// only process requests with &quot;wcpdx...
Upcoming SlideShare
Loading in...5
×

How Not to Build a WordPress Plugin

18,767

Published on

Published in: Technology, Business
2 Comments
13 Likes
Statistics
Notes
  • Thank you for sharing !

    http://www.e-rachat-credits.fr/
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • This is an awesome slideshow! I couldn't get calling my plugins directly to work under wpmu, and this showed me how to do it right... Thanks!
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
18,767
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
134
Comments
2
Likes
13
Embeds 0
No embeds

No notes for slide

How Not to Build a WordPress Plugin

  1. 1. How Not to Build a WordPress Plugin <ul><li>Will Norris < http://willnorris.com /> </li></ul>
  2. 2. <ul><li>Upgradability </li></ul><ul><li>Performance </li></ul><ul><li>Security </li></ul><ul><li>Extensibility </li></ul>
  3. 3. Unique Function Names
  4. 4. Function Name Prefix <ul><li>wcpdx_activate() </li></ul><ul><li>wcpdx_deactivate() </li></ul><ul><li>wcpdx_uninstall() </li></ul>
  5. 5. Class <ul><li>class WordCampPDX { </li></ul><ul><li>function activate() </li></ul><ul><li>function deactivate() </li></ul><ul><li>function uninstall() </li></ul><ul><li>} </li></ul>
  6. 6. Never Assume File Location
  7. 7. Traditional Directory Layout <ul><li>example.com/ </li></ul><ul><li>wordpress/ </li></ul><ul><li>wp-config.php </li></ul><ul><li>wp-content/ </li></ul><ul><li>plugins/ </li></ul><ul><li>themes/ </li></ul>
  8. 8. Non-Traditional Layout (since WP 2.6) <ul><li>example.com/ </li></ul><ul><li>wordpress/ </li></ul><ul><li>wp-config.php </li></ul><ul><li>wordpress-content/ </li></ul><ul><li>plugins/ </li></ul><ul><li>themes/ </li></ul>
  9. 9. Plugin URL <ul><li>ur doin it wrong: </li></ul><ul><li><img src=”<?php bloginfo(‘wpurl’) ?>/wp-content/plugins/wcpdx/logo.png” ?> </li></ul><ul><li>dats bedder: </li></ul><ul><li><img src=”<?php echo WP_PLUGIN_URL ?>/wcpdx/logo.png ?>”/> </li></ul><ul><li>you haz it: </li></ul><ul><li><img src=”<?php echo plugins_url(‘logo.png’, __FILE__) ?>” /> </li></ul>
  10. 10. Plugin URL <ul><li>plugins_url() </li></ul><ul><ul><ul><li>supports WPMU plugin directory </li></ul></ul></ul><ul><ul><ul><li>auto detects SSL </li></ul></ul></ul><ul><ul><ul><li>supports renamed plugin directory </li></ul></ul></ul><ul><ul><ul><li>calls ‘plugins_url’ filter </li></ul></ul></ul>
  11. 11. Friends of plugins_url() <ul><li>site_url() </li></ul><ul><li>admin_url() </li></ul><ul><li>includes_url() </li></ul><ul><li>content_url() </li></ul><ul><li>no home_url() (why not?) </li></ul>
  12. 12. Including Files <ul><li>ur doin it wrong: </li></ul><ul><li>include ‘../../wp-content/...’ </li></ul><ul><li>dats bedder: </li></ul><ul><li>include ABSPATH . ‘wp-content/...’ </li></ul><ul><li>you haz it: </li></ul><ul><li>include WP_CONTENT_DIR . ‘/...’ </li></ul>
  13. 13. Find the Right Hook <ul><li>Load as late as possible, but no later </li></ul>
  14. 14. Admin Hooks <ul><li>ur doin it wrong: </li></ul><ul><li>add_action(‘admin_init’, ‘wcpdx_admin_init’) </li></ul><ul><li>add_action(‘admin_head’, ‘wcpdx_admin_head’) </li></ul><ul><li>you haz it: </li></ul><ul><li>$hookname = add_options_page( ... ) </li></ul><ul><li>add_action(“admin_load-$hookname”, ‘wcpdx_admin_init’) </li></ul><ul><li>add_action(“admin_head-$hookname”, ‘wcpdx_admin_head’) </li></ul>
  15. 15. Styles and Scripts <ul><li>ur doin it wrong: </li></ul><ul><li><script rel=”<?php echo plugins_url(‘wcpdx.js’, __FILE__) ?>”></script> </li></ul><ul><li>you haz it: </li></ul><ul><li>wp_enqueue_script(‘wcpdx’, plugins_url(‘wcpdx.js’, __FILE__)) </li></ul><ul><li>wp_enqueue_style(‘wcpdx’, plugins_url(‘wcpdx.css’, __FILE__)) </li></ul>
  16. 16. Styles and Scripts <ul><li>wp_register_* and wp_enqueue_* </li></ul><ul><ul><ul><li>support dependencies </li></ul></ul></ul><ul><ul><ul><li>push scripts to footer </li></ul></ul></ul><ul><ul><ul><li>caching support based on version </li></ul></ul></ul><ul><ul><ul><li>(one day) server side concatenation </li></ul></ul></ul>
  17. 17. Add your own hooks <ul><li>A strategically placed hook covers a multitude of sins. </li></ul>
  18. 18. Custom Hooks <ul><li>Can do everything core WP hooks do: </li></ul><ul><ul><ul><li>event notification (actions) </li></ul></ul></ul><ul><ul><ul><li>massage data (the_content) </li></ul></ul></ul><ul><ul><ul><li>replace values (stylesheet) </li></ul></ul></ul><ul><ul><ul><li>extend functionality (http_api_curl) </li></ul></ul></ul><ul><ul><ul><li>replace functionality </li></ul></ul></ul>
  19. 19. Custom Hooks <ul><li>do_action(‘my-action’) </li></ul><ul><li>do_action(‘my-action’, $a, $b) </li></ul><ul><li>do_action_ref_array(‘my-action’, array($wcpdx)) </li></ul><ul><li>apply_filters(‘my-filter’, $wcpdx) </li></ul><ul><li>apply_filters(‘my-filter’, $wcpdx, $a, $b) </li></ul>
  20. 20. Custom Tables
  21. 21. Designed for Flexibility <ul><li>WordPress database supports </li></ul><ul><ul><ul><li>custom options </li></ul></ul></ul><ul><ul><ul><li>arbitrary metadata for posts, users, and comments (2.9) </li></ul></ul></ul><ul><ul><ul><li>custom taxonomies </li></ul></ul></ul><ul><ul><ul><li>custom post types </li></ul></ul></ul>
  22. 22. Custom Post Types <ul><li>Used by WordPress core for </li></ul><ul><ul><ul><li>posts </li></ul></ul></ul><ul><ul><ul><li>pages </li></ul></ul></ul><ul><ul><ul><li>revisions </li></ul></ul></ul><ul><ul><ul><li>attachments </li></ul></ul></ul>
  23. 23. If it walks like a duck... <ul><li>author </li></ul><ul><li>date and time </li></ul><ul><li>title </li></ul><ul><li>content </li></ul><ul><li>comments </li></ul><ul><li>categories and tags </li></ul><ul><li>permalink </li></ul><ul><li>order </li></ul><ul><li>hierarchy </li></ul><ul><li>(additional arbitrary metadata) </li></ul>
  24. 24. Admin Settings Pages
  25. 25. Admin Settings Pages <ul><li>Don’t waste time processing manually </li></ul><ul><li>register_setting( ‘wcpdx’, ‘my-option’ ) </li></ul><ul><li>http://codex.wordpress.org/Settings_API </li></ul><ul><li>http://codex.wordpress.org/Creating_Options_Pages# Register_Settings </li></ul>
  26. 26. Admin Settings Pages <ul><li>Do you really need a dedicated page? </li></ul><ul><li>Add options to any built-in settings page </li></ul><ul><li>add_settings_field( ... ) </li></ul>
  27. 27. Direct Plugin Files
  28. 28. Direct Plugin File Calls <ul><li>Direct HTTP request to plugin file ajax.php: </li></ul><ul><li>echo ‘<script type=”text/javascript”> </li></ul><ul><li>jQuery.get(“‘ . plugins_url(‘ajax.php’, __FILE__) . ‘”); </li></ul><ul><li>// do something with AJAX data </li></ul><ul><li></script>’; </li></ul>
  29. 29. Direct Plugin File Calls <ul><li>If ajax.php includes anything similar to: </li></ul><ul><li>require_once(‘../../../wp-load.php’); </li></ul><ul><li>ur doin it wrong </li></ul>
  30. 30. WordPress Requests <ul><li>Permalink URL: </li></ul><ul><li>http://example.com/2009/01/hello-world </li></ul><ul><li>becomes: </li></ul><ul><li>http://example.com/index.php? </li></ul><ul><li>year=2009& </li></ul><ul><li>monthnum=01& </li></ul><ul><li>name=hello-world </li></ul>
  31. 31. Custom WP Request <ul><li>Instead of making an AJAX call to: </li></ul><ul><li>http://example.com/wp-content/plugins/wcpdx/ajax.php </li></ul><ul><li>we want a URL like: </li></ul><ul><li>http://example.com/index.php?wcpdx=ajax-handler </li></ul>
  32. 32. Custom WP Requests <ul><li>function wcpdx_parse_request($wp) { </li></ul><ul><li>// only process requests with &quot;wcpdx=ajax-handler&quot; </li></ul><ul><li>if (array_key_exists('wcpdx', $wp->query_vars) </li></ul><ul><li>&& $wp->query_vars['wcpdx'] == 'ajax-handler') { </li></ul><ul><li>// process the request. </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>add_action('parse_request', 'wcpdx_parse_request'); </li></ul><ul><li>function wcpdx_query_vars($vars) { </li></ul><ul><li>$vars[] = 'wcpdx'; </li></ul><ul><li>return $vars; </li></ul><ul><li>} </li></ul><ul><li>add_filter('query_vars', 'wcpdx_query_vars'); </li></ul>
  1. A particular slide catching your eye?

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

×