How Not to Build a WordPress Plugin

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

1 comments

Comments 1 - 1 of 1 previous next Post a comment

  • + gueste726c81 Jason 1 month ago
    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!
Post a comment
Embed Video
Edit your comment Cancel

2 Favorites

How Not to Build a WordPress Plugin - Presentation Transcript

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

+ Will NorrisWill Norris, 2 months ago

custom

1429 views, 2 favs, 0 embeds more stats

More info about this document

© All Rights Reserved

Go to text version

  • Total Views 1429
    • 1429 on SlideShare
    • 0 from embeds
  • Comments 1
  • Favorites 2
  • Downloads 10
Most viewed embeds

more

All embeds

less

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

Cancel
File a copyright complaint
Having problems? Go to our helpdesk?

Categories