Creating and Maintaining WordPress Plugins

Preview:

Citation preview

Creatingand

MaintainingWordPressPlugins

Mark Jaquith“JAKE-with”

@markjaquithmark@jaquith.memarkjaquith.com

Why?Pa!erns!

Repo…Sanity.

Why make plugins

functions.phpis not the newmy-hacks.php

Plugins separatefunctionality fromcore and themes

Plugins

your codecomparmentalize

Plugins

your code[comparmentalize]

Plugins

your code[compart][mentalize]

Pa!erns

<?php/*Plugin Name: NAME*/

Encapsulatewith CLASS

function your_initials_long_plugin_name_do_thing()

vs.

function do_thing()

<?php/*Plugin Name: NAME*/

class CWS_ATL_Plugin { static $instance;

function __construct() { self::$instance = $this; add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ) ); }

function plugins_loaded() { // Add actions and hooks here }}

new CWS_ATL_Plugin;

function plugins_loaded() { add_filter( 'the_title', array( $this, 'the_title' ), 10, 2 ); add_action( 'init', array( $this, 'init' ), 5 ); add_action( 'omg', array( $this, 'is_really_tedious' ), 11, 3 );}

if(!class_exists('CWS_Plugin_v2')){class CWS_Plugin_v2{function hook($h){$p=10;$m=$this->sanitize_method($h);$b=func_get_args();unset($b[0]);foreach((array)$b as $a){if(is_int($a))$p=$a;else $m=$a;}return add_action($h,array($this,$m),$p,999);}private function sanitize_method($m){return str_replace(array('.','-'),array('_DOT_','_DASH_'),$m);}}}

<?php/*Plugin Name: NAME*/

if(!class_exists('CWS_Plugin_v2')){class CWS_Plugin_v2{function hook($h){$p=10;$m=$this->sanitize_method($h);$b=func_get_args();unset($b[0]);foreach((array)$b as $a){if(is_int($a))$p=$a;else $m=$a;}return add_action($h,array($this,$m),$p,999);}private function sanitize_method($m){return str_replace(array('.','-'),array('_DOT_','_DASH_'),$m);}}}

class CWS_ATL_Plugin extends CWS_Plugin_v2 { static $instance;

function __construct() { self::$instance = $this; $this->hook( 'plugins_loaded' ); }

function plugins_loaded() { // Add actions and hooks here }}

new CWS_ATL_Plugin;

// BEFOREfunction plugins_loaded() { add_filter( 'the_title', array( $this, 'the_title' ), 10, 2 ); add_action( 'init', array( $this, 'init' ), 5 ); add_action( 'omg', array( $this, 'is_really_tedious' ), 11, 3 );}

// AFTERfunction plugins_loaded() { $this->hook( 'the_title' ); $this->hook( 'init', 5 ); $this->hook( 'omg', 'is_really_tedious', 3 );}

Defer actions

Options in an array

const OPTIONS = 'my_plugin_option_name';

function get_option( $key ) { $options = get_option( self::OPTIONS ); if ( isset( $options[$key] ) ) return $options[$key];}

function set_option( $key, $value ) { $options = get_option( self::OPTIONS ); $options[$key] = $value; update_option( self::OPTIONS, $options );}

Plugins Repo

CommunityKarma

ProfessionalCredibility

Easy reuse for you

SVN

svn checkout h!p://plugins.svn.wordpress.org/{plugin-name}

svn up

{hackity hack hack}

svn add {new-files}

svn commit -m '{Message about your changes}'{

Plug

in d

ev lo

op

/trunk/ for development

Tag your releases

/trunk/readme.txt

specifies the stable tag

Stable tag: 2.3

=== Your Plugin ===Contributors: markjaquith, nacinbotDonate link: h!p://example.com/paypalTags: foo, barRequires at least: 3.2Tested up to: 3.3.1Stable tag: 2.3

Here is a short description of the plugin. This should be no more than 150 characters. No markup here.

1. Tag in SVN

2. “Repoint” /trunk/readme.txt

assets/banner-772x250.(jpg|png)

Sanity

Slow downon the optionscowboyand/or-girl

Say hello to my li!le friend:

Say hello to my li!le friend:

“no”

Encourage

behaviorgood

Encourage

behaviorgood

Q & AMark Jaquith“JAKE-with”

@markjaquithmark@jaquith.memarkjaquith.com