jumping into wordpress plugin programming

23
Jumping into WordPress Plugin Programming WordCamp Birmingham 2009 Dougal Campbell

Upload: dougal-campbell

Post on 27-Jan-2015

109 views

Category:

Technology


0 download

DESCRIPTION

If you are new to WordPress, but already know how to program, the typical "Hello, World" examples aren't helpful. You need to know how to make the right API calls, and where to find documentation about the actions and filters that WordPress makes available to you.This presentation is a brief introduction skimming the surface of the API hook system in WordPress. It does not go into deep detail, but gives brief "real world" examples of how to use filters and actions, along with pointers on where to find the main documentation that will help you get started on your own plugins.

TRANSCRIPT

Page 1: Jumping Into WordPress Plugin Programming

Jumping into WordPress Plugin Programming

WordCamp Birmingham 2009Dougal Campbell

Page 2: Jumping Into WordPress Plugin Programming

Who am I?

Dougal CampbellBlog: geek ramblings http://dougal.gunters.orgTwitter: dougalFacebook: DougalCampbell

WordPress “Developer Emeritus”

Core features: Post custom fields (postmeta), Blogging API support, Conditional GET support for feeds, Mass re-enabling for plugins

Plugins: Text Filter Suite (pirate filter), EasyGravatars, Theme Preview, Fancybox Gallery

Created Ping-O-Matic and Twitual

Page 3: Jumping Into WordPress Plugin Programming

Who is this session for?

Use WordPress (duh!)

Know at least a little bit of PHP (functions, arrays)

Beyond “Hello, World”, but don’t grok the WP API yet

Keep hearing about “hooks”, “actions”, and “filters”

You’re just curious, darnit!

This session is for you if some combination of the following apply to you:

Page 4: Jumping Into WordPress Plugin Programming

What are plugins?

Extend the functionality of WordPress

Alter content before it is displayed

Interact with 3rd party services (data in/out)

Page 5: Jumping Into WordPress Plugin Programming

Terminology

Filters: Let you modify content before using it

Actions: Let you “do something” at certain points in the code flow

Hooks: Generic term for filter and action names

API: Application Programming Interface - fancy name for all of the functions available to you

Page 6: Jumping Into WordPress Plugin Programming

Ancient Chinese Secret

Underneath the hood, filters and actions are actually the same thing!

(more on that later)

Page 7: Jumping Into WordPress Plugin Programming

FiltersIn photography, a filter lets you change how an image looks, from its true reality into something different. Color filters, blur filters, etc. In programs like Photoshop, filters take this to a whole new level: posterization, color conversions, art styles, etc.

In WordPress, filters let you take an original piece of content (post title, content, option values, etc) and transform it. Original content goes in, new content comes out.

Page 8: Jumping Into WordPress Plugin Programming

Actions

Actions don't change anything, they just do stuff. For example, whenever you publish a new post, an action could update your status on Twitter. Or an action could see that a visitor to your site came from Digg, and display a special greeting to them. Or it could do something totally invisible, like collecting visitor stats in your database.

Actions are less about content, and more about events that happen as part of the process of processing a visit and displaying your content to the visitor, or as a result of actions you take when you configure your WordPress site.

Page 9: Jumping Into WordPress Plugin Programming

How do you use them?Fundamentally, the filter and action hooks are very easy to add to the WordPress execution flow:

Create a function

Tell WordPress to add your function as a filter or action

That's it. It's that simple.

Page 10: Jumping Into WordPress Plugin Programming

How do you use them?Fundamentally, the filter and action hooks are very easy to add to the WordPress execution flow:

Create a function

Tell WordPress to add your function as a filter or action

That's it. It's that simple.

Well, okay, not really. You need to know which actions and filters WordPress provides to you. Let's break it down to the next level with a simple real-world example...

Page 11: Jumping Into WordPress Plugin Programming

Filter function example

Filters are easy to start with, because they give you some visual feedback. Let’s make a function which adds a copyright notice to the end of some text:

/* Filter function to append a copyright notice. */function dc_add_copyright($text) { $message = "<p>Copyright &copy; 2009 by Dougal Campbell<p>"; return $text . $message;}

Page 12: Jumping Into WordPress Plugin Programming

Hooking it in

How do we tell WordPress about our filter function? With a filter hook.

Adding a filter hook looks like this:

add_filter(hook-name, function-name);

Where ‘hook-name’ is the name of a WordPress filter API hook, and ‘function-name’ is the name of the filter function you wish to pass content through.

Page 13: Jumping Into WordPress Plugin Programming

Hooking it in

The ‘function-name’ part is easy: we just wrote it. It’s our ‘dc_add_copyright’ function.

add_filter(hook-name, ‘dc_add_copyright’);

But what is this ‘hook-name’ you speak of?

Page 14: Jumping Into WordPress Plugin Programming

Hooking it inWordPress hook names are used to identify various 'events' that occur during the building of your pages. In our case, we are looking for the filter hook that is used to modify your post content. That hook is conveniently named 'the_content'.

add_filter(‘the_content’, ‘dc_add_copyright’);

Easy peasy! Except, how did I know what the name of the hook was? This is where some research might be necessary when you're first getting started...

Page 15: Jumping Into WordPress Plugin Programming

Codex API Resources

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

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

The Codex is the community-maintained online documentation site for WordPress. In particular, you might want to bookmark these pages:

These pages should list the names of every available filter and action hook.

Page 16: Jumping Into WordPress Plugin Programming

Action Hooks

Adding an action hook is extremely similar to adding a filter hook:

add_action(hook-name,function-name);

Where ‘hook-name’ is the name of a WordPress action API hook, and ‘function-name’ is the name of the function you wish to fire when that hook event occurs.

Page 17: Jumping Into WordPress Plugin Programming

Events?As the WordPress code executes, there are several key points at which action events can fire. Some occur automatically, some fire based on user actions, or certain conditions being true or false, or at certain points in the display of your theme.

A few examples:

init: fires after most of the main WP core is loaded

parse_request: fires when WP is determining what kind of request is being made (post, page, archives, category, etc.)

wp_head: fires in a theme inside the ‘header.php’ template

wp_footer: fires in a theme inside the ‘footer.php’ template

Page 18: Jumping Into WordPress Plugin Programming

Action ExampleInstead of appending our copyright notice to every post with a filter, let’s put it in the footer of our theme. Conveniently, we’ve just learned that there is a ‘wp_footer’ action hook. Now we just need to modify our filter function. Instead of returning a value, it will just print it out directly:

/* Action function to print a copyright notice. */function dc_print_copyright() { print "<p>Copyright &copy; 2009 by Dougal Campbell<p>";}

It can’t get much simpler than that.

Page 19: Jumping Into WordPress Plugin Programming

Hooking it in

This looks almost exactly like our filter example:

add_action(‘wp_footer’, ‘dc_print_copyright’);

Now when WordPress is outputting a page, and the theme calls do_action(‘wp_footer’), our function will be run, and it will print the copyright notice.

Page 20: Jumping Into WordPress Plugin Programming

Best Practices

Namespacing: Make sure that your functions and variables are unique. Give them a prefix, or encapsulate your code into a class.

Security: WordPress provides many functions to help you keep your code secure. Use them! esc_attr(), esc_url(), esc_sql(), esc_js(), wp_nonce_url(), wp_nonce_field(), etc.

If there is something you are trying to do that seems hard, WordPress has probably already solved the problem for you. Look for existing functions that do what you need. Don’t re-invent the wheel!

Page 21: Jumping Into WordPress Plugin Programming

What we didn’t coverThe standard plugin headerhttp://codex.wordpress.org/Writing_a_Plugin

Object methods require a special way to specify the filter or action function name: array(&$this, ‘function_name’);

Plugin option screens, saving and retrieving plugin settings

“Pluggable” functions (override core functions)http://codex.wordpress.org/Pluggable_Functions

Page 22: Jumping Into WordPress Plugin Programming

ResourcesWordPress PHPXrefhttp://phpxref.com/xref/wordpress/

Planet Ozhhttp://planetozh.com/blog/

Mark Jaquith on WordPresshttp://markjaquith.wordpress.com/

Planet WordPresshttp://planet.wordpress.org/