how not to build a wordpress plugin

32
How Not to Build a WordPress Plugin Will Norris <http://willnorris.com />

Upload: will-norris

Post on 27-Jan-2015

119 views

Category:

Technology


4 download

DESCRIPTION

 

TRANSCRIPT

Page 1: How Not to Build a WordPress Plugin

How Not to Build a WordPress Plugin

Will Norris <http://willnorris.com/>

Page 2: How Not to Build a WordPress Plugin

•Upgradability

•Performance

•Security

•Extensibility

Page 3: How Not to Build a WordPress Plugin

Unique Function Names

Page 4: How Not to Build a WordPress Plugin

Function Name Prefix

wcpdx_activate()

wcpdx_deactivate()

wcpdx_uninstall()

Page 5: How Not to Build a WordPress Plugin

Class

class WordCampPDX {

function activate()

function deactivate()

function uninstall()

}

Page 6: How Not to Build a WordPress Plugin

Never Assume File Location

Page 7: How Not to Build a WordPress Plugin

Traditional Directory Layout

example.com/

wordpress/

wp-config.php

wp-content/

plugins/

themes/

Page 8: How Not to Build a WordPress Plugin

Non-Traditional Layout (since WP

2.6)example.com/

wordpress/

wp-config.php

wordpress-content/

plugins/

themes/

Page 9: How Not to Build a WordPress Plugin

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__) ?>” />

Page 10: How Not to Build a WordPress Plugin

Plugin URL

plugins_url()

• supports WPMU plugin directory

• auto detects SSL

• supports renamed plugin directory

• calls ‘plugins_url’ filter

Page 11: How Not to Build a WordPress Plugin

Friends of plugins_url()

site_url()

admin_url()

includes_url()

content_url()

no home_url() (why not?)

Page 12: How Not to Build a WordPress Plugin

Including Files

ur doin it wrong:include ‘../../wp-content/...’

dats bedder:include ABSPATH . ‘wp-content/...’

you haz it:include WP_CONTENT_DIR . ‘/...’

Page 13: How Not to Build a WordPress Plugin

Find the Right Hook

Load as late as possible, but no later

Page 14: How Not to Build a WordPress Plugin

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’)

Page 15: How Not to Build a WordPress Plugin

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__))

Page 16: How Not to Build a WordPress Plugin

Styles and Scriptswp_register_* and wp_enqueue_*

• support dependencies

• push scripts to footer

• caching support based on version

• (one day) server side concatenation

Page 17: How Not to Build a WordPress Plugin

Add your own hooks

A strategically placed hook covers a multitude of sins.

Page 18: How Not to Build a WordPress Plugin

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

Page 19: How Not to Build a WordPress Plugin

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)

Page 20: How Not to Build a WordPress Plugin

Custom Tables

Page 21: How Not to Build a WordPress Plugin

Designed for Flexibility

WordPress database supports

• custom options

• arbitrary metadata for posts, users, and comments (2.9)

• custom taxonomies

• custom post types

Page 22: How Not to Build a WordPress Plugin

Custom Post Types

Used by WordPress core for

• posts

• pages

• revisions

• attachments

Page 23: How Not to Build a WordPress Plugin

If it walks like a duck...

• author

• date and time

• title

• content

• comments

• categories and tags

• permalink

• order

• hierarchy

• (additional arbitrary metadata)

Page 24: How Not to Build a WordPress Plugin

Admin Settings Pages

Page 25: How Not to Build a WordPress Plugin

Admin Settings Pages

Don’t waste time processing manuallyregister_setting( ‘wcpdx’, ‘my-option’ )

http://codex.wordpress.org/Settings_API

http://codex.wordpress.org/Creating_Options_Pages#Register_Settings

Page 26: How Not to Build a WordPress Plugin

Admin Settings Pages

Do you really need a dedicated page?

Add options to any built-in settings pageadd_settings_field( ... )

Page 27: How Not to Build a WordPress Plugin

Direct Plugin Files

Page 28: How Not to Build a WordPress Plugin

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>’;

Page 29: How Not to Build a WordPress Plugin

Direct Plugin File Calls

If ajax.php includes anything similar to:require_once(‘../../../wp-load.php’);

ur doin it wrong

Page 30: How Not to Build a WordPress Plugin

WordPress Requests

Permalink URL:http://example.com/2009/01/hello-world

becomes:http://example.com/index.php?

year=2009&

monthnum=01&

name=hello-world

Page 31: How Not to Build a WordPress Plugin

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

Page 32: How Not to Build a WordPress Plugin

Custom WP Requests

function wcpdx_parse_request($wp) {

// only process requests with "wcpdx=ajax-handler"

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');