SlideShare a Scribd company logo
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

More Related Content

PDF
An easy guide to Plugin Development
KEY
Writing your Third Plugin
PDF
So, you want to be a plugin developer?
PPTX
Creating a Plug-In Architecture
PPTX
The Open-source Eclipse Plugin for Force.com Development, Summer ‘14
PDF
A Simple Plugin Architecture for Wicket
ODP
The Future Of WordPress Presentation
PPTX
Eclipse Overview
An easy guide to Plugin Development
Writing your Third Plugin
So, you want to be a plugin developer?
Creating a Plug-In Architecture
The Open-source Eclipse Plugin for Force.com Development, Summer ‘14
A Simple Plugin Architecture for Wicket
The Future Of WordPress Presentation
Eclipse Overview

What's hot (20)

PDF
Writing Software not Code with Cucumber
PDF
Behaviour Driven Development con Behat & Drupal
PDF
Getting big without getting fat, in perl
ODP
Ant User Guide
KEY
Actions filters
PDF
Object Oriented Programming for WordPress Plugin Development
PDF
Twig: Friendly Curly Braces Invade Your Templates!
PDF
Grand Rapids PHP Meetup: Behavioral Driven Development with Behat
PDF
Design patterns revisited with PHP 5.3
PPTX
Plugin development wpmeetup010
PDF
greach 2014 marco vermeulen bdd using cucumber jvm and groovy
PDF
Introduction to WordPress Hooks 2016
PDF
Django Introduction & Tutorial
PPTX
Apache Ant
PDF
Bending word press to your will
PPTX
Django app deployment in Azure By Saurabh Agarwal
PDF
Plugin Development @ WordCamp Norway 2014
PPTX
Django Girls Tutorial
PPT
WordPress Plugin Basics
PPTX
Test automation with Cucumber-JVM
Writing Software not Code with Cucumber
Behaviour Driven Development con Behat & Drupal
Getting big without getting fat, in perl
Ant User Guide
Actions filters
Object Oriented Programming for WordPress Plugin Development
Twig: Friendly Curly Braces Invade Your Templates!
Grand Rapids PHP Meetup: Behavioral Driven Development with Behat
Design patterns revisited with PHP 5.3
Plugin development wpmeetup010
greach 2014 marco vermeulen bdd using cucumber jvm and groovy
Introduction to WordPress Hooks 2016
Django Introduction & Tutorial
Apache Ant
Bending word press to your will
Django app deployment in Azure By Saurabh Agarwal
Plugin Development @ WordCamp Norway 2014
Django Girls Tutorial
WordPress Plugin Basics
Test automation with Cucumber-JVM
Ad

Viewers also liked (6)

PDF
Plugin jQuery, Design Patterns
KEY
jQuery Plugin Creation
PDF
Best Practices in Plugin Development (WordCamp Seattle)
PDF
Building an Eclipse plugin to recommend changes to developers
PDF
Building GPE: What We Learned
PPTX
Configuration as Code: The Job DSL Plugin
Plugin jQuery, Design Patterns
jQuery Plugin Creation
Best Practices in Plugin Development (WordCamp Seattle)
Building an Eclipse plugin to recommend changes to developers
Building GPE: What We Learned
Configuration as Code: The Job DSL Plugin
Ad

Similar to Jumping Into WordPress Plugin Programming (20)

PPTX
2016 WordCamp Pittsburgh - Let's Write a Plugin
PPTX
Let’s write a plugin
PDF
Demystifying Hooks, Actions & Filters - WordCamp Belfast 2018
KEY
How To Write a WordPress Plugin
PDF
Stop Hacking WordPress, Start Working with it - Charly Leetham - WordCamp Syd...
ODP
Beginning WordPress Plugin Development
KEY
CSI: WordPress -- Getting Into the Guts
PDF
So, You Wanna Dev? Join the Team! - WordCamp Raleigh 2017
PPT
PDF
Creating Your First WordPress Plugin
PPT
Word press Plugins by WordPress Experts
PDF
Intro to WordPress Plugin Development
PDF
WordCamp Greenville 2018 - Beware the Dark Side, or an Intro to Development
ODP
Best practices in WordPress Development
PDF
Write Your First WordPress Plugin
KEY
doing_it_right() with WordPress
PPTX
WordPress Hooks Action & Filters
KEY
WordPress Bootcamp Part 2 - Extending WordPress
PDF
Plugin development demystified 2017
PDF
Write your first WordPress plugin
2016 WordCamp Pittsburgh - Let's Write a Plugin
Let’s write a plugin
Demystifying Hooks, Actions & Filters - WordCamp Belfast 2018
How To Write a WordPress Plugin
Stop Hacking WordPress, Start Working with it - Charly Leetham - WordCamp Syd...
Beginning WordPress Plugin Development
CSI: WordPress -- Getting Into the Guts
So, You Wanna Dev? Join the Team! - WordCamp Raleigh 2017
Creating Your First WordPress Plugin
Word press Plugins by WordPress Experts
Intro to WordPress Plugin Development
WordCamp Greenville 2018 - Beware the Dark Side, or an Intro to Development
Best practices in WordPress Development
Write Your First WordPress Plugin
doing_it_right() with WordPress
WordPress Hooks Action & Filters
WordPress Bootcamp Part 2 - Extending WordPress
Plugin development demystified 2017
Write your first WordPress plugin

Recently uploaded (20)

PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Heart disease approach using modified random forest and particle swarm optimi...
PPTX
OMC Textile Division Presentation 2021.pptx
PPTX
Chapter 5: Probability Theory and Statistics
PDF
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
PDF
Approach and Philosophy of On baking technology
PDF
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
PDF
Mushroom cultivation and it's methods.pdf
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Getting Started with Data Integration: FME Form 101
PPTX
A Presentation on Touch Screen Technology
PPTX
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
PPTX
1. Introduction to Computer Programming.pptx
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Zenith AI: Advanced Artificial Intelligence
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Unlocking AI with Model Context Protocol (MCP)
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Heart disease approach using modified random forest and particle swarm optimi...
OMC Textile Division Presentation 2021.pptx
Chapter 5: Probability Theory and Statistics
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
Approach and Philosophy of On baking technology
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
Mushroom cultivation and it's methods.pdf
Univ-Connecticut-ChatGPT-Presentaion.pdf
MIND Revenue Release Quarter 2 2025 Press Release
Getting Started with Data Integration: FME Form 101
A Presentation on Touch Screen Technology
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
1. Introduction to Computer Programming.pptx
Programs and apps: productivity, graphics, security and other tools
Zenith AI: Advanced Artificial Intelligence
Building Integrated photovoltaic BIPV_UPV.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf

Jumping Into WordPress Plugin Programming

  • 1. Jumping into WordPress Plugin Programming WordCamp Birmingham 2009 Dougal Campbell
  • 2. 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
  • 3. 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!
  • 4. What are plugins? Extend the functionality of WordPress Alter content before it is displayed Interact with 3rd party services (data in/out)
  • 5. 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
  • 6. Ancient Chinese Secret Underneath the hood, filters and actions are actually the same thing! (more on that later)
  • 7. 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.
  • 8. 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.
  • 9. 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.
  • 10. 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...
  • 11. 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; }
  • 12. 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.
  • 13. 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?
  • 14. 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...
  • 15. 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.
  • 16. 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.
  • 17. 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
  • 18. 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.
  • 19. 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.
  • 20. 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!
  • 21. 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
  • 22. 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/