• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Jumping Into WordPress Plugin Programming
 

Jumping Into WordPress Plugin Programming

on

  • 16,033 views

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

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.

Statistics

Views

Total Views
16,033
Views on SlideShare
14,612
Embed Views
1,421

Actions

Likes
8
Downloads
164
Comments
0

15 Embeds 1,421

http://dougal.gunters.org 642
http://paulstamatiou.com 355
http://www.authorityhost.com 273
http://joomlademo.authorityhost.com 103
http://www.slideshare.net 13
http://authorityhost.com 12
http://michaelwparks.com 12
http://feeds.feedburner.com 2
http://translate.googleusercontent.com 2
http://65.55.177.205 2
http://www.google.com 1
http://rss2.com 1
http://twitter.com 1
http://static.slidesharecdn.com 1
http://www.linkedin.com 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Jumping Into WordPress Plugin Programming Jumping Into WordPress Plugin Programming Presentation Transcript

    • Jumping into WordPress Plugin Programming WordCamp Birmingham 2009 Dougal Campbell
    • Who am I? Dougal Campbell Blog: geek ramblings http://dougal.gunters.org Twitter: dougal Facebook: 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
    • Who is this session for? This session is for you if some combination of the following apply to you: 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!
    • What are plugins? Extend the functionality of WordPress Alter content before it is displayed Interact with 3rd party services (data in/out)
    • 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
    • Ancient Chinese Secret Underneath the hood, filters and actions are actually the same thing! (more on that later)
    • Filters In 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.
    • 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.
    • 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.
    • 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...
    • 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; }
    • 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.
    • 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?
    • Hooking it in WordPress 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...
    • Codex API Resources The Codex is the community-maintained online documentation site for WordPress. In particular, you might want to bookmark these pages: http://codex.wordpress.org/Plugin_API/Filter_Reference http://codex.wordpress.org/Plugin_API/Action_Reference These pages should list the names of every available filter and action hook.
    • 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.
    • 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
    • Action Example Instead 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.
    • 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.
    • 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!
    • What we didn’t cover The standard plugin header http://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
    • Resources WordPress PHPXref http://phpxref.com/xref/wordpress/ Planet Ozh http://planetozh.com/blog/ Mark Jaquith on WordPress http://markjaquith.wordpress.com/ Planet WordPress http://planet.wordpress.org/
    • Thanks! Dougal Campbell http://dougal.gunters.org dougal@gunters.org http://twitter.com/dougal