Transcript
Page 1: Extending WordPress with (Web)Hooks

Extending WordPress with (Web)Hooks

mitcho (Michael 芳貴 Erlewine)http://mitcho.com

September 28, 2009Boston WordPress Meetup

Page 2: Extending WordPress with (Web)Hooks

Today• Introduction• Modifying WordPress behavior• filters and actions

• Writing our first plugin in PHP• Extending WordPress with webhooks via HookPress

Page 3: Extending WordPress with (Web)Hooks

Today

NOTE:• Will link to slides and code later on Meetup site

• Please rate later on SpeakerRate• again, link on Meetup.com

Page 4: Extending WordPress with (Web)Hooks

Today• Introduction• Modifying WordPress behavior• filters and actions

• Writing our first plugin in PHP• Extending WordPress with webhooks via HookPress

Page 5: Extending WordPress with (Web)Hooks

Introduction

Page 6: Extending WordPress with (Web)Hooks

Introduction• Hi, I’m mitcho.

Page 7: Extending WordPress with (Web)Hooks

Introduction• Hi, I’m mitcho.• Linguist, coder, teacher.

Page 8: Extending WordPress with (Web)Hooks

Introduction• Hi, I’m mitcho.• Linguist, coder, teacher.

• At MIT. Previously at Mozilla.

Page 9: Extending WordPress with (Web)Hooks

Introduction• Hi, I’m mitcho.• Linguist, coder, teacher.

• At MIT. Previously at Mozilla.• Programming PHP/MySQL since 2002?

Page 10: Extending WordPress with (Web)Hooks

Introduction• Hi, I’m mitcho.• Linguist, coder, teacher.

• At MIT. Previously at Mozilla.• Programming PHP/MySQL since 2002?• Blogging (off and on) at mitcho.com since 2007.

Page 11: Extending WordPress with (Web)Hooks

Introduction• Hi, I’m mitcho.• Linguist, coder, teacher.

• At MIT. Previously at Mozilla.• Programming PHP/MySQL since 2002?• Blogging (off and on) at mitcho.com since 2007.

• http://mitcho.com; @mitchoyoshitaka

Page 12: Extending WordPress with (Web)Hooks

Introduction

Page 13: Extending WordPress with (Web)Hooks

Introduction

• Yet Another Related Posts Plugin

Page 14: Extending WordPress with (Web)Hooks

Introduction

• Yet Another Related Posts Plugin• ...aka YARPP

Page 15: Extending WordPress with (Web)Hooks

Introduction

• Yet Another Related Posts Plugin• ...aka YARPP• As seen on Laughing Squid, ma.tt...

Page 16: Extending WordPress with (Web)Hooks

Introduction

• Yet Another Related Posts Plugin• ...aka YARPP• As seen on Laughing Squid, ma.tt...• Over 250k downloads

Page 17: Extending WordPress with (Web)Hooks

Introduction

• Yet Another Related Posts Plugin• ...aka YARPP• As seen on Laughing Squid, ma.tt...• Over 250k downloads

• http://mitcho.com/code/yarpp or search for “YARPP”; @yarpp

Page 18: Extending WordPress with (Web)Hooks

Today• Introduction• Modifying WordPress behavior• filters and actions

• Writing our first plugin in PHP• Extending WordPress with webhooks via HookPress

Page 19: Extending WordPress with (Web)Hooks

WordPress

Page 20: Extending WordPress with (Web)Hooks

WordPress

• Open-source, http://wordpress.org

Page 21: Extending WordPress with (Web)Hooks

WordPress

• Open-source, http://wordpress.org• PHP/MySQL

Page 22: Extending WordPress with (Web)Hooks

WordPress

• Open-source, http://wordpress.org• PHP/MySQL• Today: not wordpress.com

Page 23: Extending WordPress with (Web)Hooks

WordPress

Page 24: Extending WordPress with (Web)Hooks

WordPress

• Self-hosted, so in theory you could modify it it directly

Page 25: Extending WordPress with (Web)Hooks

WordPress

• Self-hosted, so in theory you could modify it it directly• Not recommended!

Page 26: Extending WordPress with (Web)Hooks

WordPress

• Self-hosted, so in theory you could modify it it directly• Not recommended!

• Highly extensible plugin architecture

Page 27: Extending WordPress with (Web)Hooks

WordPress

• Self-hosted, so in theory you could modify it it directly• Not recommended!

• Highly extensible plugin architecture• WordPress itself uses it to modify its own behavior

Page 28: Extending WordPress with (Web)Hooks
Page 29: Extending WordPress with (Web)Hooks

Modifying behavior

Page 30: Extending WordPress with (Web)Hooks

Modifying behavior

• How does WordPress work?

Page 31: Extending WordPress with (Web)Hooks

Modifying behavior

• How does WordPress work?• Example: displaying a post’s content

Page 32: Extending WordPress with (Web)Hooks

Modifying behavior

• How does WordPress work?• Example: displaying a post’s content

parse URL

Page 33: Extending WordPress with (Web)Hooks

Modifying behavior

• How does WordPress work?• Example: displaying a post’s content

parse URL get content from db

Page 34: Extending WordPress with (Web)Hooks

Modifying behavior

• How does WordPress work?• Example: displaying a post’s content

parse URL get content from db display

Page 35: Extending WordPress with (Web)Hooks

Modifying behavior

• How does WordPress work?• Example: displaying a post’s content

parse URL get content from db display

• but it’s not actually that simple

Page 36: Extending WordPress with (Web)Hooks

Modifying behavior

• How does WordPress work?• Example: displaying a post’s content

parse URL get content from db display

• but it’s not actually that simple

smart quotes, paragraph splitting, HTML formatting, smilies...

Page 37: Extending WordPress with (Web)Hooks

get content from db display

Modifying behavior

Page 38: Extending WordPress with (Web)Hooks

get content from db display

I have the content and am about to display it. Who wants to modify it first?

Modifying behavior

Page 39: Extending WordPress with (Web)Hooks

get content from db display

I have the content and am about to display it. Who wants to modify it first?

wp_texturize

convert_smilies

wpautop

...

Modifying behavior

Page 40: Extending WordPress with (Web)Hooks

get content from db display

I have the content and am about to display it. Who wants to modify it first?

wp_texturize

convert_smilies

wpautop

...

Modifying behavior

Page 41: Extending WordPress with (Web)Hooks

get content from db display

I have the content and am about to display it. Who wants to modify it first?

wp_texturize

convert_smilies

wpautop

...

Modifying behavior

Page 42: Extending WordPress with (Web)Hooks

get content from db display

I have the content and am about to display it. Who wants to modify it first?

wp_texturize

convert_smilies

wpautop

...

Modifying behavior

Page 43: Extending WordPress with (Web)Hooks

get content from db display

I have the content and am about to display it. Who wants to modify it first?

wp_texturize

convert_smilies

wpautop

...

Modifying behavior

Page 44: Extending WordPress with (Web)Hooks

get content from db display

I have the content and am about to display it. Who wants to modify it first?

wp_texturize

convert_smilies

wpautop

...

Modifying behavior

Page 45: Extending WordPress with (Web)Hooks

get content from db display

I have the content and am about to display it. Who wants to modify it first?

wp_texturize

convert_smilies

wpautop

...

Modifying behavior

Your customfunctions here:

Page 46: Extending WordPress with (Web)Hooks

get content from db display

I have the content and am about to display it. Who wants to modify it first?

wp_texturize

convert_smilies

wpautop

...

Modifying behavior

Page 47: Extending WordPress with (Web)Hooks

get content from db display

I have the content and am about to display it. Who wants to modify it first?

wp_texturize

convert_smilies

wpautop

...

Modifying behavior

Page 48: Extending WordPress with (Web)Hooks

get content from db display

I have the content and am about to display it. Who wants to modify it first?

wp_texturize

convert_smilies

wpautop

...

Modifying behavior

This is a filter pattern:multiple functions act asfilters on the same text/target.

Page 49: Extending WordPress with (Web)Hooks

get content from db display

I have the content and am about to display it. Who wants to modify it first?

Modifying behavior

Page 50: Extending WordPress with (Web)Hooks

get content from db display

I have the content and am about to display it. Who wants to modify it first?

Modifying behavior

Each such filter hook (= “pathway,” “process”) has a name. This one is called the_content.

Page 51: Extending WordPress with (Web)Hooks

Modifying behavior

Page 52: Extending WordPress with (Web)Hooks

Modifying behavior

• Every filter function must be registered against that filter hook ahead of time:

Page 53: Extending WordPress with (Web)Hooks

Modifying behavior

• Every filter function must be registered against that filter hook ahead of time:

add_filter(“the_content”, “my_function_name”);

Page 54: Extending WordPress with (Web)Hooks

Modifying behavior

• Every filter function must be registered against that filter hook ahead of time:

add_filter(“the_content”, “my_function_name”);

hook name

Page 55: Extending WordPress with (Web)Hooks

Modifying behavior

• Every filter function must be registered against that filter hook ahead of time:

add_filter(“the_content”, “my_function_name”);

hook name function name

Page 56: Extending WordPress with (Web)Hooks

Modifying behavior

Page 57: Extending WordPress with (Web)Hooks

Modifying behavior

• There’s another kind of hook:

Page 58: Extending WordPress with (Web)Hooks

Modifying behavior

• There’s another kind of hook:• actions

Page 59: Extending WordPress with (Web)Hooks

Modifying behavior

• There’s another kind of hook:• actions• target certain “events” that occur.

Page 60: Extending WordPress with (Web)Hooks

Modifying behavior

• There’s another kind of hook:• actions• target certain “events” that occur.• Examples: a post is published, a comment is made, someone accesses wp-admin, etc.

Page 61: Extending WordPress with (Web)Hooks

Modifying behavior

Page 62: Extending WordPress with (Web)Hooks

Modifying behavior

• actions...

Page 63: Extending WordPress with (Web)Hooks

Modifying behavior

• actions...• don’t necessarily modify anything but can trigger other processes.

Page 64: Extending WordPress with (Web)Hooks

Modifying behavior

• actions...• don’t necessarily modify anything but can trigger other processes.

• can also produce output, modify the database, etc.

Page 65: Extending WordPress with (Web)Hooks

Modifying behavior

Page 66: Extending WordPress with (Web)Hooks

Modifying behavior

• filters...

Page 67: Extending WordPress with (Web)Hooks

Modifying behavior

• filters...• modify (filter) something and return it.

Page 68: Extending WordPress with (Web)Hooks

Modifying behavior

• filters...• modify (filter) something and return it.

• The return value matters.

Page 69: Extending WordPress with (Web)Hooks

Modifying behavior

• filters...• modify (filter) something and return it.

• The return value matters.• actions...

Page 70: Extending WordPress with (Web)Hooks

Modifying behavior

• filters...• modify (filter) something and return it.

• The return value matters.• actions...• correspond to an “event.”

Page 71: Extending WordPress with (Web)Hooks

Modifying behavior

• filters...• modify (filter) something and return it.

• The return value matters.• actions...• correspond to an “event.”• Return values don’t matter.

Page 72: Extending WordPress with (Web)Hooks

Today• Introduction• Modifying WordPress behavior• filters and actions

• Writing our first plugin in PHP• Extending WordPress with webhooks via HookPress

Page 73: Extending WordPress with (Web)Hooks

Today

Page 74: Extending WordPress with (Web)Hooks

TodayNOTE:

Page 75: Extending WordPress with (Web)Hooks

TodayNOTE:• I’m using MAMP: Mac, Apache, MySQL, PHP.

Page 76: Extending WordPress with (Web)Hooks

TodayNOTE:• I’m using MAMP: Mac, Apache, MySQL, PHP.• Similar to XAMPP on Windows.

Page 77: Extending WordPress with (Web)Hooks

TodayNOTE:• I’m using MAMP: Mac, Apache, MySQL, PHP.• Similar to XAMPP on Windows.• Working on a server directly is okay too...

Page 78: Extending WordPress with (Web)Hooks

TodayNOTE:• I’m using MAMP: Mac, Apache, MySQL, PHP.• Similar to XAMPP on Windows.• Working on a server directly is okay too...• but avoid working on live sites.

Page 79: Extending WordPress with (Web)Hooks

Writing a plugin

Page 80: Extending WordPress with (Web)Hooks

Task: display a word count for posts

Writing a plugin

Page 81: Extending WordPress with (Web)Hooks

Writing a plugin

Page 82: Extending WordPress with (Web)Hooks

Writing a plugin

• PHP script

Page 83: Extending WordPress with (Web)Hooks

Writing a plugin

• PHP script• Place in wp-content/plugins/

Page 84: Extending WordPress with (Web)Hooks

Writing a plugin

• PHP script• Place in wp-content/plugins/• Requires a certain header

Page 86: Extending WordPress with (Web)Hooks

Writing a plugin

The plan:1.Write a filter function which...a. takes the content as its input;b. counts the words;c. adds, for ex., <p>(43 words)</p> to the end of the content;

d. returns the modified content;

Page 87: Extending WordPress with (Web)Hooks

Writing a plugin

The plan:1.Write a filter function which...2.Register that function as a filter. Use the the_content filter hook.

Page 88: Extending WordPress with (Web)Hooks

Writing a plugin

Page 89: Extending WordPress with (Web)Hooks

Task (continued): add a sponsorship message

Writing a plugin

Page 90: Extending WordPress with (Web)Hooks

Writing a plugin

The plan (continued):3.Write an action function which prints the message.

4.Register it against the wp_footer action hook.

Page 91: Extending WordPress with (Web)Hooks

Note:

Page 92: Extending WordPress with (Web)Hooks

Note:

This is a filter...

Page 93: Extending WordPress with (Web)Hooks

Note:

This is a filter...

...so it takes an argument...

Page 94: Extending WordPress with (Web)Hooks

Note:

This is a filter...

...so it takes an argument...

...and returns it after modifying.

Page 95: Extending WordPress with (Web)Hooks

Note:

Page 96: Extending WordPress with (Web)Hooks

Note:

This is an action...

Page 97: Extending WordPress with (Web)Hooks

Note:

This is an action...

...so it returns nothing.

Page 98: Extending WordPress with (Web)Hooks

Writing a plugin

Page 99: Extending WordPress with (Web)Hooks

Writing a plugin

• Right now the counting happens on the archive and front pages as well.

Page 100: Extending WordPress with (Web)Hooks

Writing a plugin

• Right now the counting happens on the archive and front pages as well.

• Use the WordPress is_single() conditional tag.

Page 101: Extending WordPress with (Web)Hooks

Writing a plugin

• Right now the counting happens on the archive and front pages as well.

• Use the WordPress is_single() conditional tag.• You may have seen such conditional tags in theme development.

Page 102: Extending WordPress with (Web)Hooks

Resources

Page 103: Extending WordPress with (Web)Hooks

Resources

• PHP manual: php.net

Page 107: Extending WordPress with (Web)Hooks
Page 108: Extending WordPress with (Web)Hooks

Look into...

Page 109: Extending WordPress with (Web)Hooks

Look into...

• Namespacing: use function_exists(), classes, unique names.

Page 110: Extending WordPress with (Web)Hooks

Look into...

• Namespacing: use function_exists(), classes, unique names.

• Saving options: get_option(), etc.

Page 111: Extending WordPress with (Web)Hooks

Look into...

• Namespacing: use function_exists(), classes, unique names.

• Saving options: get_option(), etc.• Adding an options screen: codex.wordpress.org/Adding_Administration_Menus

Page 112: Extending WordPress with (Web)Hooks

Look into...

Page 113: Extending WordPress with (Web)Hooks

Look into...

• Security: use data validation for HTML, SQL queries, etc.

Page 114: Extending WordPress with (Web)Hooks

Look into...

• Security: use data validation for HTML, SQL queries, etc.• WordPress helps you: codex.wordpress.org/Data_Validation

Page 115: Extending WordPress with (Web)Hooks

Look into...

• Security: use data validation for HTML, SQL queries, etc.• WordPress helps you: codex.wordpress.org/Data_Validation

• Share! Consider uploading to wordpress.org/extend

Page 116: Extending WordPress with (Web)Hooks

Today• Introduction• Modifying WordPress behavior• filters and actions

• Writing our first plugin in PHP• Extending WordPress with webhooks via HookPress

Page 117: Extending WordPress with (Web)Hooks

Extending WordPress

Page 118: Extending WordPress with (Web)Hooks

Extending WordPress

PHP

Page 119: Extending WordPress with (Web)Hooks

Extending WordPress

• What if...

PHP

Page 120: Extending WordPress with (Web)Hooks

Extending WordPress

• What if...• you’re more comfortable in another language?

PHP

Page 121: Extending WordPress with (Web)Hooks

Extending WordPress

• What if...• you’re more comfortable in another language?

• you want to integrate WordPress with another system?

PHP

Page 122: Extending WordPress with (Web)Hooks

WordPress

Page 123: Extending WordPress with (Web)Hooks

+ HookPressWordPress

Page 124: Extending WordPress with (Web)Hooks

+ HookPressWordPress

Page 125: Extending WordPress with (Web)Hooks

+ HookPressWordPress

action!filter!

Page 126: Extending WordPress with (Web)Hooks

+ HookPressWordPress

action!filter!

Page 127: Extending WordPress with (Web)Hooks

+ HookPressWordPress

action!filter! your

script

local or remote

Page 128: Extending WordPress with (Web)Hooks

+ HookPressWordPress

action!filter! your

script

local or remote

Page 129: Extending WordPress with (Web)Hooks

+ HookPressWordPress

action!filter! POST your

script

local or remote

Page 130: Extending WordPress with (Web)Hooks
Page 131: Extending WordPress with (Web)Hooks

WordPress + HookPress

Page 132: Extending WordPress with (Web)Hooks

• create POST webhooks

WordPress + HookPress

Page 133: Extending WordPress with (Web)Hooks

• create POST webhooks• supports both actions...

WordPress + HookPress

Page 134: Extending WordPress with (Web)Hooks

• create POST webhooks• supports both actions...• create push notifications

WordPress + HookPress

Page 135: Extending WordPress with (Web)Hooks

• create POST webhooks• supports both actions...• create push notifications

• ...and filters

WordPress + HookPress

Page 136: Extending WordPress with (Web)Hooks

• create POST webhooks• supports both actions...• create push notifications

• ...and filters• extend WordPress without PHP

WordPress + HookPress

Page 137: Extending WordPress with (Web)Hooks

Get HookPress

Page 138: Extending WordPress with (Web)Hooks

• mitcho.com/code/hookpress• Screencast online

• Stay in touch!• @hookpress on twitter

Get HookPress

Page 139: Extending WordPress with (Web)Hooks

Resources

Page 140: Extending WordPress with (Web)Hooks

• Check the HookPress request:

Resources

Page 141: Extending WordPress with (Web)Hooks

• Check the HookPress request:• postbin.org

Resources

Page 142: Extending WordPress with (Web)Hooks

• Check the HookPress request:• postbin.org

• Run basic/proxy scripts:

Resources

Page 143: Extending WordPress with (Web)Hooks

• Check the HookPress request:• postbin.org

• Run basic/proxy scripts:• scriptlets.org

Resources

Page 144: Extending WordPress with (Web)Hooks

• Check the HookPress request:• postbin.org

• Run basic/proxy scripts:• scriptlets.org

• General WebHooks information:

Resources

Page 145: Extending WordPress with (Web)Hooks

• Check the HookPress request:• postbin.org

• Run basic/proxy scripts:• scriptlets.org

• General WebHooks information:• webhooks.org

Resources

Page 146: Extending WordPress with (Web)Hooks

Thank you!Questions?

Please rate the talk on SpeakerRate.

mitcho (Michael 芳貴 Erlewine)mitcho.com; @mitchoyoshitaka