Jumping into WordPress Plugin Programming WordCamp Birmingham 2009 Dougal Campbell
Jan 27, 2015
Jumping into WordPress Plugin Programming
WordCamp Birmingham 2009Dougal Campbell
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
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:
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)
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.
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 © 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 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...
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.
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 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 © 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 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
ResourcesWordPress PHPXrefhttp://phpxref.com/xref/wordpress/
Planet Ozhhttp://planetozh.com/blog/
Mark Jaquith on WordPresshttp://markjaquith.wordpress.com/
Planet WordPresshttp://planet.wordpress.org/
Thanks!Dougal Campbellhttp://[email protected]://twitter.com/dougal