Extending WordPress with (Web)Hooks

Preview:

DESCRIPTION

Presented at the Boston WordPress meetup, September 28, 2009."In this presentation I will give a high level overview of how WordPress can be customized through custom plugins. I will walk through the creation of a very simple plugin in PHP, the default development language for WordPress. I will then present HookPress (http://mitcho.com/code/hookpress/) which is a plugin of mine which adds webhooks to WordPress, letting developers extend WordPress in languages other than PHP, such as ruby, python, perl, etc.""This will be an intermediate level session designed for users already comfortable with publishing with and customizing a wordpress.org installation and have maybe even installed a few plugins. Moreover, I will assume some familiarity with programming, though not necessarily the PHP language."

Citation preview

Extending WordPress with (Web)Hooks

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

September 28, 2009Boston WordPress Meetup

Today• Introduction• Modifying WordPress behavior• filters and actions

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

Today

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

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

Today• Introduction• Modifying WordPress behavior• filters and actions

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

Introduction

Introduction• Hi, I’m mitcho.

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

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

• At MIT. Previously at Mozilla.

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

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

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.

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

Introduction

Introduction

• Yet Another Related Posts Plugin

Introduction

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

Introduction

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

Introduction

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

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

Today• Introduction• Modifying WordPress behavior• filters and actions

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

WordPress

WordPress

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

WordPress

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

WordPress

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

WordPress

WordPress

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

WordPress

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

WordPress

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

• Highly extensible plugin architecture

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

Modifying behavior

Modifying behavior

• How does WordPress work?

Modifying behavior

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

Modifying behavior

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

parse URL

Modifying behavior

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

parse URL get content from db

Modifying behavior

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

parse URL get content from db display

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

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...

get content from db display

Modifying behavior

get content from db display

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

Modifying behavior

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

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

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

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

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

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

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:

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

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

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.

get content from db display

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

Modifying behavior

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.

Modifying behavior

Modifying behavior

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

Modifying behavior

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

add_filter(“the_content”, “my_function_name”);

Modifying behavior

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

add_filter(“the_content”, “my_function_name”);

hook name

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

Modifying behavior

Modifying behavior

• There’s another kind of hook:

Modifying behavior

• There’s another kind of hook:• actions

Modifying behavior

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

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.

Modifying behavior

Modifying behavior

• actions...

Modifying behavior

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

Modifying behavior

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

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

Modifying behavior

Modifying behavior

• filters...

Modifying behavior

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

Modifying behavior

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

• The return value matters.

Modifying behavior

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

• The return value matters.• actions...

Modifying behavior

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

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

Modifying behavior

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

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

Today• Introduction• Modifying WordPress behavior• filters and actions

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

Today

TodayNOTE:

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

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

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

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.

Writing a plugin

Task: display a word count for posts

Writing a plugin

Writing a plugin

Writing a plugin

• PHP script

Writing a plugin

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

Writing a plugin

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

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;

Writing a plugin

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

Writing a plugin

Task (continued): add a sponsorship message

Writing a plugin

Writing a plugin

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

4.Register it against the wp_footer action hook.

Note:

Note:

This is a filter...

Note:

This is a filter...

...so it takes an argument...

Note:

This is a filter...

...so it takes an argument...

...and returns it after modifying.

Note:

Note:

This is an action...

Note:

This is an action...

...so it returns nothing.

Writing a plugin

Writing a plugin

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

Writing a plugin

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

• Use the WordPress is_single() conditional tag.

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.

Resources

Resources

• PHP manual: php.net

Look into...

Look into...

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

Look into...

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

• Saving options: get_option(), etc.

Look into...

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

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

Look into...

Look into...

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

Look into...

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

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

Today• Introduction• Modifying WordPress behavior• filters and actions

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

Extending WordPress

Extending WordPress

PHP

Extending WordPress

• What if...

PHP

Extending WordPress

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

PHP

Extending WordPress

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

• you want to integrate WordPress with another system?

PHP

WordPress

+ HookPressWordPress

+ HookPressWordPress

+ HookPressWordPress

action!filter!

+ HookPressWordPress

action!filter!

+ HookPressWordPress

action!filter! your

script

local or remote

+ HookPressWordPress

action!filter! your

script

local or remote

+ HookPressWordPress

action!filter! POST your

script

local or remote

WordPress + HookPress

• create POST webhooks

WordPress + HookPress

• create POST webhooks• supports both actions...

WordPress + HookPress

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

WordPress + HookPress

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

• ...and filters

WordPress + HookPress

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

• ...and filters• extend WordPress without PHP

WordPress + HookPress

Get HookPress

• mitcho.com/code/hookpress• Screencast online

• Stay in touch!• @hookpress on twitter

Get HookPress

Resources

• Check the HookPress request:

Resources

• Check the HookPress request:• postbin.org

Resources

• Check the HookPress request:• postbin.org

• Run basic/proxy scripts:

Resources

• Check the HookPress request:• postbin.org

• Run basic/proxy scripts:• scriptlets.org

Resources

• Check the HookPress request:• postbin.org

• Run basic/proxy scripts:• scriptlets.org

• General WebHooks information:

Resources

• Check the HookPress request:• postbin.org

• Run basic/proxy scripts:• scriptlets.org

• General WebHooks information:• webhooks.org

Resources

Thank you!Questions?

Please rate the talk on SpeakerRate.

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