24
Writing Your First WordPress Plugin or, What I wish someone had told me because it would have prevented me from banging my head on the desk a lot Boone Gorges @boonebgorges [email protected] http://teleogistic.net

Wordpress Meetup 2 23 10

Embed Size (px)

DESCRIPTION

Writing your first WordPress plugin

Citation preview

Page 1: Wordpress Meetup 2 23 10

Writing Your First WordPress Pluginor, What I wish someone had told me because it would have prevented me from banging my head on the desk a lot

Boone Gorges@[email protected]://teleogistic.net

Page 2: Wordpress Meetup 2 23 10

Who am I? – early 2009o Instructional Technologist, Queens College, CUNY

o Philosopher

o Good HTML and CSS knowledge. Some scripting experience

o Moderately high awesomeness

Page 3: Wordpress Meetup 2 23 10

Who am I? – early 2010o Developer, CUNY Academic Commons http://commons.gc.cuny.edu

o A zillion plugins in WP repo http://wordpress.org/extend/plugins/profile/boonebgorges

o Extreme awesomeness

Page 4: Wordpress Meetup 2 23 10

1) The structure of WordPress

Page 5: Wordpress Meetup 2 23 10

WordPress is modular

Separable pieces: classes, functions, template files

Serial: Everything happens in a certain order

Page 6: Wordpress Meetup 2 23 10

WordPress is modular

Plugins:

New classes, functions, or template files that either replace or augment WP “core”

Page 7: Wordpress Meetup 2 23 10

2) Plugin basics

Page 8: Wordpress Meetup 2 23 10

Where do plugins live?

Ask yourself:

- Is my plugin specific to a theme?- wp-content/[theme-dir]/functions.php

- Should my plugin activate on all blogs? (MU only)- wp-content/mu-plugins/my-plugin.php

- Otherwise- wp-content/plugins/my-plugin/my-plugin.php

Page 9: Wordpress Meetup 2 23 10

Let WP know you exist

<?php/*Plugin Name: Boone’s PluginPlugin URI: http://teleogistic.netDescription: This plugin will blow your mindAuthor: boonebgorgesVersion: 1.0Author URI: http://teleogistic.net*/

wp-content/plugins/boones-plugin/boones-plugin.php

See http://codex.wordpress.org/Writing_a_Plugin#File_Headers

Page 10: Wordpress Meetup 2 23 10

3) Example: Shortcode

Page 11: Wordpress Meetup 2 23 10

Shortcodes fill in for frequently-typed stuff

Say you typed your address a lot:

Page 12: Wordpress Meetup 2 23 10

The WordPress Shortcode API

function address_shortcode() {return '<p>Boone Gorges<br />24 Gumdrop Lane<br />Apartment #32<br />Sugarlove, MD 20020</p>';

}add_shortcode( 'bgaddress', 'address_shortcode' );

wp-content/plugins/boones-plugin/boones-plugin.php

See http://codex.wordpress.org/Shortcode_API

Page 13: Wordpress Meetup 2 23 10

Soup that baby up

function address_shortcode($atts) {extract(shortcode_atts(array(

'location' => 'MD'), $atts));

if ( $atts['location'] == 'MD' ) return '<p>Boone Gorges<br /> 24 Gumdrop Lane<br /> Apartment #32<br /> Sugarlove, MD 20020</p>';

else return '<p>Boone Gorges<br /> 85 Lollipop Street<br /> Apartment #81<br /> Milwaukee, WI 54112</p>';

}add_shortcode( 'bgaddress', 'address_shortcode' );

wp-content/plugins/boones-plugin/boones-plugin.php

See http://codex.wordpress.org/Shortcode_API

Page 14: Wordpress Meetup 2 23 10

There's a Widgets API too

See http://codex.wordpress.org/Widgets_API

Page 15: Wordpress Meetup 2 23 10

4) Example: Hooks

Page 16: Wordpress Meetup 2 23 10

Actions are hooks

In the core:do_action( 'loop_start' );

In your plugin:add_action( 'loop_start', 'my_code' );

Page 17: Wordpress Meetup 2 23 10

Example: A hello message

function well_hello_there() {global $user_identity;

if ( is_user_logged_in() )echo "Welcome, " . $user_identity;

elseecho "Welcome, anonymous lurker!";

} add_action( 'loop_start', 'well_hello_there' );

wp-content/plugins/boones-plugin/boones-plugin.php

See http://codex.wordpress.org/Plugin_API/Action_Reference

Page 18: Wordpress Meetup 2 23 10

Filters are hooks…with benefits

In the core:$content = apply_filters('the_content', $content);

In your plugin:add_filter( 'the_content', 'my_code' );

Page 19: Wordpress Meetup 2 23 10

Example: Makin' it 1337

function make_it_leet( $content ) {$leet_content = str_replace( 'e', '3', $content );return $leet_content;

} add_filter( 'the_content', 'make_it_leet' );

wp-content/plugins/boones-plugin/boones-plugin.php

See http://codex.wordpress.org/Plugin_API/Filter_Reference

Page 20: Wordpress Meetup 2 23 10

5) The plugin dev's toolbox

Page 21: Wordpress Meetup 2 23 10

The WordPress Codex, especially:- http://codex.wordpress.org/Shortcode_API - http://codex.wordpress.org/Widgets_API- http://codex.wordpress.org/Plugin_API

The WordPress PHP Cross-Reference- http://xref.yoast.com

The WordPress plugin repository:- http://wordpress.org/extend/plugins to download- http://wordpress.org/extend/plugins/about to add your own plugin

PHP resources- W3Schools – http://www.w3schools.com/PHP/- php.net – http://www.php.net/manual/en

Resources on the web

Page 22: Wordpress Meetup 2 23 10

A good text editor, ideally one with- the ability to save over FTP- good search-and-replace- code highlighting- see http://lifehacker.com/385929/best-text-editors for suggestions

A local development environment- AMP: Apache, MySQL, PHP- Windows: XAMPP; Mac: MAMP- Use SVN to get the most recent copies of WordPress

Resources on your computer

Page 23: Wordpress Meetup 2 23 10

The White Screen Of Death- Usually means you have a syntax error (check your punctuation!)- No duplicate function names, except for functions in pluggable.php- Use if ( function_exists( 'my_function' ) )

Get to know your data- Learn about globals: $wp, $post, $comment- var_dump( $wp );- print "<pre>"; print_r( $wp ); print "</pre>";- die();

Pick apart existing plugins

Ask for help: http://wordpress.org/support

Words of advice

Page 24: Wordpress Meetup 2 23 10

Booyah!

Boone Gorges@[email protected]://teleogistic.net