47
Plugging into Matt Harris <head> conference 26 th October 2008 1

Presentation: Plugging Into Wordpress

Embed Size (px)

DESCRIPTION

The presentation I gave during the Conference, 24-26 October 2008

Citation preview

Page 1: Presentation: Plugging Into Wordpress

Plugging intoMatt Harris

<head> conference26th October 2008

1

Page 2: Presentation: Plugging Into Wordpress

Type

s of

Wor

dpre

ss Wordpress.comHosted blogging solution

Wordpress.orgDownloadable edition for use on your own server

Wordpress MUMulti-user edition of Wordpress.org

2

Page 3: Presentation: Plugging Into Wordpress

Wor

dpre

ss.c

omHosted

Free

Maintained and backed up for you

Not hackable or pluggable by you

3Hosted: scalable, Automa1c deals with the spikes you may get

Page 4: Presentation: Plugging Into Wordpress

Wor

dpre

ss.o

rgCustomisable

Self-hosted

Complete code control

Maintenance, backup and reliability are your responsibility

4Customisable: hackable, expandable.  Add your own features

Page 5: Presentation: Plugging Into Wordpress

Wor

dpre

ss M

UAll of Wordpress.org pros and cons

Hosting of multiple blogs under one install

Bespoke permission settings per blog

Your own Wordpress.com

5Basically this is the meat behind Wordpress.comGreat for Uni’s, EducaEonal places, newspapers, blog networks etc

Page 6: Presentation: Plugging Into Wordpress

User Interface

6

Page 7: Presentation: Plugging Into Wordpress

User Interface

7

Page 8: Presentation: Plugging Into Wordpress

User Interface

8

Page 9: Presentation: Plugging Into Wordpress

User Interface

9

Page 10: Presentation: Plugging Into Wordpress

Admin Interface

10

Page 11: Presentation: Plugging Into Wordpress

Admin Interface

11

Page 12: Presentation: Plugging Into Wordpress

Admin Interface

12

Page 13: Presentation: Plugging Into Wordpress

Hooks and Filters

add_action($tag, $function, $priority, $accepted_args)

add_filter($tag, $function, $priority, $accepted_args)

http://www.flickr.com/photos/gaetanlee/2906941718/

add_shortcode($tag, $function)

13Don’t hack at the core code – makes it difficult to update the your installaEonUse hooks and filters to ‘inject’ an event into the page processing

Filters are the hooks that WordPress launches to modify text of various types before adding it to the database or sending it to the browser screen

Hooks aOach custom funcEons onto specific acEonshOp://codex.wordpress.org/Plugin_API/AcEon_ReferencehOp://codex.wordpress.org/Plugin_API/Filter_ReferencehOp://codex.wordpress.org/Shortcode_API

Page 14: Presentation: Plugging Into Wordpress

Where to put your code

PluginsInside it’s own file

Theme folder in the file

functions.php

14Plugins advantage – easily switch off and onableFuncEons – will always run – best if required as part of your theme (not covered today but example would be to change something a theme relies on – maybe the movement of a login buOon)

Page 15: Presentation: Plugging Into Wordpress

Simple Filter

<?php

function changeWord($content) { $search = “wordpress”; $replace = “my blog”; return str_ireplace($search, $replace, $content);}

add_filter(‘the_content’, ‘changeWord’);

?>

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

15‘the_content” is a built in Wordpress tagStr_ireplace is case insensiEve

To test this included it in funcEons.php

Page 16: Presentation: Plugging Into Wordpress

Simple Filter

<?php

function changeWord($content) { $search = “wordpress”; $replace = “my blog”; return str_ireplace($search, $replace, $content);}

add_filter(‘the_content’, ‘changeWord’);

?>

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

16‘the_content” is a built in Wordpress tagStr_ireplace is case insensiEve

To test this included it in funcEons.php

Page 17: Presentation: Plugging Into Wordpress

Simple Filter

Before After

17

Page 18: Presentation: Plugging Into Wordpress

Simple Hook

<?php

function addText() { echo “Can you see me?”;}

add_action(’wp_footer', ’addText’);

?>

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

18wp_footer Runs when the template calls the wp_footer funcEon, generally near the boOom of the blog page.

Page 19: Presentation: Plugging Into Wordpress

Simple Hook

<?php

function addText() { echo “Can you see me?”;}

add_action(’wp_footer', ’addText’);

?>

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

19wp_footer Runs when the template calls the wp_footer funcEon, generally near the boOom of the blog page.

Page 20: Presentation: Plugging Into Wordpress

Simple Hook<?php

function changeTextCol() { echo “ <style type=‘text/css’> body { color: #0ff00f; } </style> “;}

add_action(’wp_head', ’changeTextCol’);

?>

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

20wp_footer Runs when the template calls the wp_footer funcEon, generally near the boOom of the blog page.

Page 21: Presentation: Plugging Into Wordpress

Simple Hook

Before After

21

Page 22: Presentation: Plugging Into Wordpress

Simple Shortcode<?php

function myH1_shortcode($atts, $content = null) { extract($atts); return ‘ <div class=“heading”> <h1 style=“color:’ . $colour . ‘”>' . $content . '</h1> </div>’;}

add_shortcode(’heading', 'myH1_shortcode');

?>

http://codex.wordpress.org/Shortcode_API

22Shortcodes cannot be nested be default. Must add do_shortcode($content) into your handler to do this

Since WP2.5 (fixed 2.5.1. parsing order)

Note this isn’t really safe, $content should be saniEzed first using (stripslashes(wp_filter_post_kses($content) and colour should be protected with aOribute_escape($colour)

Page 23: Presentation: Plugging Into Wordpress

Simple Shortcode<?php

[heading color=“#f00”]This is my heading[/heading]

?>

Output

<div class="heading"> <h1 style="color:#f00">This is my heading</h1></div>

23Shortcodes cannot be nested be default. Must add do_shortcode($content) into your handler to do this

Page 24: Presentation: Plugging Into Wordpress

Namespacing

http://www.flickr.com/photos/thost/2244046981/24

Page 25: Presentation: Plugging Into Wordpress

Namespacing

<?php

function addText() { echo “Can you see me?”;}

add_action(’wp_footer', ’addText’);

?>

25

Page 26: Presentation: Plugging Into Wordpress

Namespacing

<?php

function tmh_addText() { echo “Can you see me?”;}

add_action(’wp_footer', ’tmh_addText’);

?>

26

Page 27: Presentation: Plugging Into Wordpress

Widgets

27

Page 28: Presentation: Plugging Into Wordpress

Plugin Code Structure<?php

/*Plugin Name: My WidgetPlugin URI: http://themattharris.comDescription: A widget that puts some text in the sidebarAuthor: Matt HarrisVersion: 1.0Author URI: http://themattharris.com*/

?>

28

Page 29: Presentation: Plugging Into Wordpress

Widget Code Structure<?php

function tmh_renderWidget($args) { extract($args); ?> <?php echo $before_widget; ?> <?php echo $before_title . wptexturize(’My Widget') . $after_title; ?> <p>My Widget Code</p> <?php echo $after_widget; ?> <?php}

?>

29

Page 30: Presentation: Plugging Into Wordpress

Widget Code Structure<?php

function tmh_renderWidget($args) { extract($args); ?> <?php echo $before_widget; ?> <?php echo $before_title . wptexturize(’My Widget') . $after_title; ?> <p>My Widget Code</p> <?php echo $after_widget; ?> <?php}

?>

30

Page 31: Presentation: Plugging Into Wordpress

Widget Code Structure<?php

function tmh_renderWidget($args) { extract($args); ?> <?php echo $before_widget; ?> <?php echo $before_title . wptexturize(’My Widget') . $after_title; ?> <p>My Widget Code</p> <?php echo $after_widget; ?> <?php}

?>

31

Page 32: Presentation: Plugging Into Wordpress

Widget Code Structure<?php

function tmh_renderWidget($args) { extract($args); ?> <?php echo $before_widget; ?> <?php echo $before_title . wptexturize(‘My Widget’) . $after_title; ?> <p>My Widget Code</p> <?php echo $after_widget; ?> <?php}

?>

32wp_specialchars ‐ Like htmlspecialchars except don't double‐encode HTML enEEes

Page 33: Presentation: Plugging Into Wordpress

<?php

function tmh_renderWidget($args) { extract($args); ?> <?php echo $before_widget; ?> <?php echo $before_title . wptexturize(’My Widget') . $after_title; ?> <p>My Widget Code</p> <?php echo $after_widget; ?> <?php}

?>

Widget Code Structure

33

Page 34: Presentation: Plugging Into Wordpress

Widget Registration Code<?php

function tmh_widgetInit() { if (function_exists('register_sidebar_widget’)) { register_sidebar_widget('My Widget', 'tmh_renderWidget'); }}

add_action( 'widgets_init', 'tmh_widgetInit');

?>

34

Page 35: Presentation: Plugging Into Wordpress

Putting it all together<?php/*Plugin Name: My WidgetPlugin URI: http://themattharris.comDescription: A widget that puts some text in the sidebarAuthor: Matt HarrisVersion: 1.0Author URI: http://themattharris.com*/

function tmh_renderWidget($args) { extract($args); ?> <?php echo $before_widget; ?> <?php echo $before_title . wptexturize('My Widget') . $after_title; ?>

<p>My Widget Code</p>

<?php echo $after_widget; ?> <?php}

function tmh_widgetInit() { if ( function_exists('register_sidebar_widget') ) { register_sidebar_widget('My Widget', 'tmh_renderWidget'); }}

add_action( 'widgets_init', 'tmh_widgetInit');

?>

35

Page 36: Presentation: Plugging Into Wordpress

Making it work

36

Page 37: Presentation: Plugging Into Wordpress

Making it work

37

Page 38: Presentation: Plugging Into Wordpress

Making it work

38

Page 39: Presentation: Plugging Into Wordpress

Making it work

39

Page 40: Presentation: Plugging Into Wordpress

40

Page 41: Presentation: Plugging Into Wordpress

41

Page 42: Presentation: Plugging Into Wordpress

Protecting your blog

check_admin_referer($action, [$query_arg]);

Use with

wp_create_nonce($action);

Use this on any forms you have.

42

Page 43: Presentation: Plugging Into Wordpress

Protecting your blog

attribute_escape($text); // also tag escape

wp_filter_post_kses($data); // adds slashes

wp_filter_nohtml_kses($data); // adds slashes

Use these when outputting data

43aOribute_escape (used for escaping for HTML aOributes)

Kses checks for allow html (or removes it in nohtml)

Page 44: Presentation: Plugging Into Wordpress

Protecting your blog

<?php current_user_can($capability) ?>

http://codex.wordpress.org/Roles_and_Capabilities

<?php if (current_user_can(‘unfiltered_html’)) { $data = Wp_filter_post_kses($data); } else { $data = wp_filter_nohtml_kses($data); }?>

Example

44aOribute_escape (used for escaping for HTML aOributes)

Kses checks for allow html (or removes it in nohtml)

Page 45: Presentation: Plugging Into Wordpress

Translation friendly

http://codex.wordpress.org/Translating_WordPress

<?php __($message, $domain); ?>

<?php _e($message, $domain); ?>

<?php _e(“Title:”,’tmh_pluginname’); ?>

Example

45WP uses GNU geOext localizaEon framework

Message level translaEon

_e echos

__ doesn’t

Too much info to go into here on how to have wordpress do the translaEon, but its worth building this in from the start

$message is output/returned if no translaEon is found so always worth building this in

Page 46: Presentation: Plugging Into Wordpress

Help and More InformationWordpress Mailing Lists

http://codex.wordpress.org/Mailing_Lists

Wordpress Codex (or Google term + “codex”)

http://codex.wordpress.org/

Wordpress Support Forum

http://wordpress.org/support/

Writing a Plugin

http://codex.wordpress.org/Writing_a_Plugin

46Wordpres support forum – if the FAQ can’t helpGoogle search – whole community out there that can help

Page 47: Presentation: Plugging Into Wordpress

Matt Harrishttp://[email protected]

All the links: http://ma.gnolia.com/people/themattharris/tags/plugging%20into%20wordpress

Image credits:“Hello … my name is”: http://www.flickr.com/photos/thost/2244046981/“Hooks for hand”: http://www.flickr.com/photos/gaetanlee/2906941718/“Wordpress Logo”: http://wordpress.org/about/logos/

47