How Not to Build a WordPress Plugin
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

How Not to Build a WordPress Plugin

on

  • 22,124 views

 

Statistics

Views

Total Views
22,124
Views on SlideShare
21,992
Embed Views
132

Actions

Likes
13
Downloads
129
Comments
2

5 Embeds 132

http://www.slideshare.net 61
http://www.nitinkatkam.com 52
http://www.linkedin.com 17
http://us-w1.rockmelt.com 1
https://si0.twimg.com 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • Thank you for sharing !

    http://www.e-rachat-credits.fr/
    Are you sure you want to
    Your message goes here
    Processing…
  • 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!
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

How Not to Build a WordPress Plugin Presentation Transcript

  • 1. How Not to Build a WordPress Plugin
    • Will Norris < http://willnorris.com />
  • 2.
    • Upgradability
    • Performance
    • Security
    • Extensibility
  • 3. Unique Function Names
  • 4. Function Name Prefix
    • wcpdx_activate()
    • wcpdx_deactivate()
    • wcpdx_uninstall()
  • 5. Class
    • class WordCampPDX {
    • function activate()
    • function deactivate()
    • function uninstall()
    • }
  • 6. Never Assume File Location
  • 7. Traditional Directory Layout
    • example.com/
    • wordpress/
    • wp-config.php
    • wp-content/
    • plugins/
    • themes/
  • 8. Non-Traditional Layout (since WP 2.6)
    • example.com/
    • wordpress/
    • wp-config.php
    • wordpress-content/
    • plugins/
    • themes/
  • 9. 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__) ?>” />
  • 10. Plugin URL
    • plugins_url()
        • supports WPMU plugin directory
        • auto detects SSL
        • supports renamed plugin directory
        • calls ‘plugins_url’ filter
  • 11. Friends of plugins_url()
    • site_url()
    • admin_url()
    • includes_url()
    • content_url()
    • no home_url() (why not?)
  • 12. Including Files
    • ur doin it wrong:
    • include ‘../../wp-content/...’
    • dats bedder:
    • include ABSPATH . ‘wp-content/...’
    • you haz it:
    • include WP_CONTENT_DIR . ‘/...’
  • 13. Find the Right Hook
    • Load as late as possible, but no later
  • 14. 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’)
  • 15. 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__))
  • 16. Styles and Scripts
    • wp_register_* and wp_enqueue_*
        • support dependencies
        • push scripts to footer
        • caching support based on version
        • (one day) server side concatenation
  • 17. Add your own hooks
    • A strategically placed hook covers a multitude of sins.
  • 18. 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
  • 19. 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)
  • 20. Custom Tables
  • 21. Designed for Flexibility
    • WordPress database supports
        • custom options
        • arbitrary metadata for posts, users, and comments (2.9)
        • custom taxonomies
        • custom post types
  • 22. Custom Post Types
    • Used by WordPress core for
        • posts
        • pages
        • revisions
        • attachments
  • 23. If it walks like a duck...
    • author
    • date and time
    • title
    • content
    • comments
    • categories and tags
    • permalink
    • order
    • hierarchy
    • (additional arbitrary metadata)
  • 24. Admin Settings Pages
  • 25. 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
  • 26. Admin Settings Pages
    • Do you really need a dedicated page?
    • Add options to any built-in settings page
    • add_settings_field( ... )
  • 27. Direct Plugin Files
  • 28. 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>’;
  • 29. Direct Plugin File Calls
    • If ajax.php includes anything similar to:
    • require_once(‘../../../wp-load.php’);
    • ur doin it wrong
  • 30. WordPress Requests
    • Permalink URL:
    • http://example.com/2009/01/hello-world
    • becomes:
    • http://example.com/index.php?
    • year=2009&
    • monthnum=01&
    • name=hello-world
  • 31. 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
  • 32. 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');