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

What's hot

Writing Software not Code with Cucumber
Writing Software not Code with CucumberWriting Software not Code with Cucumber
Writing Software not Code with Cucumber
Ben Mabey
 
Behaviour Driven Development con Behat & Drupal
Behaviour Driven Development con Behat & DrupalBehaviour Driven Development con Behat & Drupal
Behaviour Driven Development con Behat & Drupal
sparkfabrik
 
Getting big without getting fat, in perl
Getting big without getting fat, in perlGetting big without getting fat, in perl
Getting big without getting fat, in perl
Dean Hamstead
 
Ant User Guide
Ant User GuideAnt User Guide
Ant User Guide
Muthuselvam RS
 
Actions filters
Actions filtersActions filters
Actions filters
John Dillick
 
Object Oriented Programming for WordPress Plugin Development
Object Oriented Programming for WordPress Plugin DevelopmentObject Oriented Programming for WordPress Plugin Development
Object Oriented Programming for WordPress Plugin Development
mtoppa
 
Twig: Friendly Curly Braces Invade Your Templates!
Twig: Friendly Curly Braces Invade Your Templates!Twig: Friendly Curly Braces Invade Your Templates!
Twig: Friendly Curly Braces Invade Your Templates!
Ryan Weaver
 
Grand Rapids PHP Meetup: Behavioral Driven Development with Behat
Grand Rapids PHP Meetup: Behavioral Driven Development with BehatGrand Rapids PHP Meetup: Behavioral Driven Development with Behat
Grand Rapids PHP Meetup: Behavioral Driven Development with Behat
Ryan Weaver
 
Design patterns revisited with PHP 5.3
Design patterns revisited with PHP 5.3Design patterns revisited with PHP 5.3
Design patterns revisited with PHP 5.3
Fabien Potencier
 
Plugin development wpmeetup010
Plugin development wpmeetup010Plugin development wpmeetup010
Plugin development wpmeetup010
Barry Kooij
 
greach 2014 marco vermeulen bdd using cucumber jvm and groovy
greach 2014 marco vermeulen bdd using cucumber jvm and groovygreach 2014 marco vermeulen bdd using cucumber jvm and groovy
greach 2014 marco vermeulen bdd using cucumber jvm and groovy
Jessie Evangelista
 
Introduction to WordPress Hooks 2016
Introduction to WordPress Hooks 2016Introduction to WordPress Hooks 2016
Introduction to WordPress Hooks 2016
Ian Wilson
 
Django Introduction & Tutorial
Django Introduction & TutorialDjango Introduction & Tutorial
Django Introduction & Tutorial
之宇 趙
 
Apache Ant
Apache AntApache Ant
Apache Ant
Ali Bahu
 
Bending word press to your will
Bending word press to your willBending word press to your will
Bending word press to your will
Tom Jenkins
 
Django app deployment in Azure By Saurabh Agarwal
Django app deployment in Azure By Saurabh AgarwalDjango app deployment in Azure By Saurabh Agarwal
Django app deployment in Azure By Saurabh Agarwal
ratneshsinghparihar
 
Plugin Development @ WordCamp Norway 2014
Plugin Development @ WordCamp Norway 2014Plugin Development @ WordCamp Norway 2014
Plugin Development @ WordCamp Norway 2014
Barry Kooij
 
Django Girls Tutorial
Django Girls TutorialDjango Girls Tutorial
Django Girls Tutorial
Kishimi Ibrahim Ishaq
 
WordPress Plugin Basics
WordPress Plugin BasicsWordPress Plugin Basics
WordPress Plugin Basics
Amanda Giles
 
Test automation with Cucumber-JVM
Test automation with Cucumber-JVMTest automation with Cucumber-JVM
Test automation with Cucumber-JVM
Alan Parkinson
 

What's hot (20)

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

Viewers also liked

Plugin jQuery, Design Patterns
Plugin jQuery, Design PatternsPlugin jQuery, Design Patterns
Plugin jQuery, Design Patterns
Robert Casanova
 
jQuery Plugin Creation
jQuery Plugin CreationjQuery Plugin Creation
jQuery Plugin Creation
benalman
 
Best Practices in Plugin Development (WordCamp Seattle)
Best Practices in Plugin Development (WordCamp Seattle)Best Practices in Plugin Development (WordCamp Seattle)
Best Practices in Plugin Development (WordCamp Seattle)
andrewnacin
 
Building an Eclipse plugin to recommend changes to developers
Building an Eclipse plugin to recommend changes to developersBuilding an Eclipse plugin to recommend changes to developers
Building an Eclipse plugin to recommend changes to developers
kim.mens
 
Building GPE: What We Learned
Building GPE: What We LearnedBuilding GPE: What We Learned
Building GPE: What We Learned
rajeevdayal
 
Configuration as Code: The Job DSL Plugin
Configuration as Code: The Job DSL PluginConfiguration as Code: The Job DSL Plugin
Configuration as Code: The Job DSL Plugin
Daniel Spilker
 

Viewers also liked (6)

Plugin jQuery, Design Patterns
Plugin jQuery, Design PatternsPlugin jQuery, Design Patterns
Plugin jQuery, Design Patterns
 
jQuery Plugin Creation
jQuery Plugin CreationjQuery Plugin Creation
jQuery Plugin Creation
 
Best Practices in Plugin Development (WordCamp Seattle)
Best Practices in Plugin Development (WordCamp Seattle)Best Practices in Plugin Development (WordCamp Seattle)
Best Practices in Plugin Development (WordCamp Seattle)
 
Building an Eclipse plugin to recommend changes to developers
Building an Eclipse plugin to recommend changes to developersBuilding an Eclipse plugin to recommend changes to developers
Building an Eclipse plugin to recommend changes to developers
 
Building GPE: What We Learned
Building GPE: What We LearnedBuilding GPE: What We Learned
Building GPE: What We Learned
 
Configuration as Code: The Job DSL Plugin
Configuration as Code: The Job DSL PluginConfiguration as Code: The Job DSL Plugin
Configuration as Code: The Job DSL Plugin
 

Similar to Jumping Into WordPress Plugin Programming

How To Write a WordPress Plugin
How To Write a WordPress PluginHow To Write a WordPress Plugin
How To Write a WordPress Plugin
Andy Stratton
 
How to create your own WordPress plugin
How to create your own WordPress pluginHow to create your own WordPress plugin
How to create your own WordPress plugin
John Tighe
 
Word press Plugins by WordPress Experts
Word press Plugins by WordPress ExpertsWord press Plugins by WordPress Experts
Word press Plugins by WordPress Experts
Yameen Khan
 
WordPress Plugins
WordPress PluginsWordPress Plugins
Write your first WordPress plugin
Write your first WordPress pluginWrite your first WordPress plugin
Write your first WordPress plugin
Anthony Montalbano
 
Plug in development
Plug in developmentPlug in development
Plug in development
Lucky Ali
 
How to Create a Custom WordPress Plugin
How to Create a Custom WordPress PluginHow to Create a Custom WordPress Plugin
How to Create a Custom WordPress Plugin
Andolasoft Inc
 
Wordpress plugin creation_overview
Wordpress plugin creation_overviewWordpress plugin creation_overview
Wordpress plugin creation_overview
Daniel Kline
 
Build Your Own Instagram Filters
Build Your Own Instagram FiltersBuild Your Own Instagram Filters
Build Your Own Instagram Filters
TJ Stalcup
 
Best practices in WordPress Development
Best practices in WordPress DevelopmentBest practices in WordPress Development
Best practices in WordPress Development
Mindfire Solutions
 
Plugin development demystified 2017
Plugin development demystified 2017Plugin development demystified 2017
Plugin development demystified 2017
ylefebvre
 
CSI: WordPress -- Getting Into the Guts
CSI: WordPress -- Getting Into the GutsCSI: WordPress -- Getting Into the Guts
CSI: WordPress -- Getting Into the Guts
Dougal Campbell
 
Writing your own WordPress themes and plugins
Writing your own WordPress themes and pluginsWriting your own WordPress themes and plugins
Writing your own WordPress themes and plugins
Stephanie Wells
 
WordPress Hooks (Actions & Filters)
WordPress Hooks (Actions & Filters)WordPress Hooks (Actions & Filters)
WordPress Hooks (Actions & Filters)
MuhammadKashif596
 
Build your-own-instagram-filters-with-javascript-202-335 (1)
Build your-own-instagram-filters-with-javascript-202-335 (1)Build your-own-instagram-filters-with-javascript-202-335 (1)
Build your-own-instagram-filters-with-javascript-202-335 (1)
Thinkful
 
-Kotlin_Camp_Unit2.pptx
-Kotlin_Camp_Unit2.pptx-Kotlin_Camp_Unit2.pptx
-Kotlin_Camp_Unit2.pptx
RishiGandhi19
 
-Kotlin Camp Unit2.pptx
-Kotlin Camp Unit2.pptx-Kotlin Camp Unit2.pptx
-Kotlin Camp Unit2.pptx
IshwariKulkarni6
 
Step by step guide for creating wordpress plugin
Step by step guide for creating wordpress pluginStep by step guide for creating wordpress plugin
Step by step guide for creating wordpress plugin
Mainak Goswami
 
Introduction to Plugin Programming, WordCamp Miami 2011
Introduction to Plugin Programming, WordCamp Miami 2011Introduction to Plugin Programming, WordCamp Miami 2011
Introduction to Plugin Programming, WordCamp Miami 2011
David Carr
 
Rebrand WordPress Admin
Rebrand WordPress AdminRebrand WordPress Admin
Rebrand WordPress Admin
Chandra Prakash Thapa
 

Similar to Jumping Into WordPress Plugin Programming (20)

How To Write a WordPress Plugin
How To Write a WordPress PluginHow To Write a WordPress Plugin
How To Write a WordPress Plugin
 
How to create your own WordPress plugin
How to create your own WordPress pluginHow to create your own WordPress plugin
How to create your own WordPress plugin
 
Word press Plugins by WordPress Experts
Word press Plugins by WordPress ExpertsWord press Plugins by WordPress Experts
Word press Plugins by WordPress Experts
 
WordPress Plugins
WordPress PluginsWordPress Plugins
WordPress Plugins
 
Write your first WordPress plugin
Write your first WordPress pluginWrite your first WordPress plugin
Write your first WordPress plugin
 
Plug in development
Plug in developmentPlug in development
Plug in development
 
How to Create a Custom WordPress Plugin
How to Create a Custom WordPress PluginHow to Create a Custom WordPress Plugin
How to Create a Custom WordPress Plugin
 
Wordpress plugin creation_overview
Wordpress plugin creation_overviewWordpress plugin creation_overview
Wordpress plugin creation_overview
 
Build Your Own Instagram Filters
Build Your Own Instagram FiltersBuild Your Own Instagram Filters
Build Your Own Instagram Filters
 
Best practices in WordPress Development
Best practices in WordPress DevelopmentBest practices in WordPress Development
Best practices in WordPress Development
 
Plugin development demystified 2017
Plugin development demystified 2017Plugin development demystified 2017
Plugin development demystified 2017
 
CSI: WordPress -- Getting Into the Guts
CSI: WordPress -- Getting Into the GutsCSI: WordPress -- Getting Into the Guts
CSI: WordPress -- Getting Into the Guts
 
Writing your own WordPress themes and plugins
Writing your own WordPress themes and pluginsWriting your own WordPress themes and plugins
Writing your own WordPress themes and plugins
 
WordPress Hooks (Actions & Filters)
WordPress Hooks (Actions & Filters)WordPress Hooks (Actions & Filters)
WordPress Hooks (Actions & Filters)
 
Build your-own-instagram-filters-with-javascript-202-335 (1)
Build your-own-instagram-filters-with-javascript-202-335 (1)Build your-own-instagram-filters-with-javascript-202-335 (1)
Build your-own-instagram-filters-with-javascript-202-335 (1)
 
-Kotlin_Camp_Unit2.pptx
-Kotlin_Camp_Unit2.pptx-Kotlin_Camp_Unit2.pptx
-Kotlin_Camp_Unit2.pptx
 
-Kotlin Camp Unit2.pptx
-Kotlin Camp Unit2.pptx-Kotlin Camp Unit2.pptx
-Kotlin Camp Unit2.pptx
 
Step by step guide for creating wordpress plugin
Step by step guide for creating wordpress pluginStep by step guide for creating wordpress plugin
Step by step guide for creating wordpress plugin
 
Introduction to Plugin Programming, WordCamp Miami 2011
Introduction to Plugin Programming, WordCamp Miami 2011Introduction to Plugin Programming, WordCamp Miami 2011
Introduction to Plugin Programming, WordCamp Miami 2011
 
Rebrand WordPress Admin
Rebrand WordPress AdminRebrand WordPress Admin
Rebrand WordPress Admin
 

Recently uploaded

Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
Zilliz
 
Must Know Postgres Extension for DBA and Developer during Migration
Must Know Postgres Extension for DBA and Developer during MigrationMust Know Postgres Extension for DBA and Developer during Migration
Must Know Postgres Extension for DBA and Developer during Migration
Mydbops
 
Essentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation ParametersEssentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation Parameters
Safe Software
 
"Scaling RAG Applications to serve millions of users", Kevin Goedecke
"Scaling RAG Applications to serve millions of users",  Kevin Goedecke"Scaling RAG Applications to serve millions of users",  Kevin Goedecke
"Scaling RAG Applications to serve millions of users", Kevin Goedecke
Fwdays
 
Dandelion Hashtable: beyond billion requests per second on a commodity server
Dandelion Hashtable: beyond billion requests per second on a commodity serverDandelion Hashtable: beyond billion requests per second on a commodity server
Dandelion Hashtable: beyond billion requests per second on a commodity server
Antonios Katsarakis
 
PRODUCT LISTING OPTIMIZATION PRESENTATION.pptx
PRODUCT LISTING OPTIMIZATION PRESENTATION.pptxPRODUCT LISTING OPTIMIZATION PRESENTATION.pptx
PRODUCT LISTING OPTIMIZATION PRESENTATION.pptx
christinelarrosa
 
Mutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented ChatbotsMutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented Chatbots
Pablo Gómez Abajo
 
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge GraphGraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
Neo4j
 
Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |
AstuteBusiness
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
Brandon Minnick, MBA
 
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-EfficiencyFreshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
ScyllaDB
 
"$10 thousand per minute of downtime: architecture, queues, streaming and fin...
"$10 thousand per minute of downtime: architecture, queues, streaming and fin..."$10 thousand per minute of downtime: architecture, queues, streaming and fin...
"$10 thousand per minute of downtime: architecture, queues, streaming and fin...
Fwdays
 
Biomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Biomedical Knowledge Graphs for Data Scientists and BioinformaticiansBiomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Biomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Neo4j
 
GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)
Javier Junquera
 
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectorsConnector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
DianaGray10
 
Session 1 - Intro to Robotic Process Automation.pdf
Session 1 - Intro to Robotic Process Automation.pdfSession 1 - Intro to Robotic Process Automation.pdf
Session 1 - Intro to Robotic Process Automation.pdf
UiPathCommunity
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
saastr
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
Chart Kalyan
 
A Deep Dive into ScyllaDB's Architecture
A Deep Dive into ScyllaDB's ArchitectureA Deep Dive into ScyllaDB's Architecture
A Deep Dive into ScyllaDB's Architecture
ScyllaDB
 
Christine's Supplier Sourcing Presentaion.pptx
Christine's Supplier Sourcing Presentaion.pptxChristine's Supplier Sourcing Presentaion.pptx
Christine's Supplier Sourcing Presentaion.pptx
christinelarrosa
 

Recently uploaded (20)

Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
 
Must Know Postgres Extension for DBA and Developer during Migration
Must Know Postgres Extension for DBA and Developer during MigrationMust Know Postgres Extension for DBA and Developer during Migration
Must Know Postgres Extension for DBA and Developer during Migration
 
Essentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation ParametersEssentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation Parameters
 
"Scaling RAG Applications to serve millions of users", Kevin Goedecke
"Scaling RAG Applications to serve millions of users",  Kevin Goedecke"Scaling RAG Applications to serve millions of users",  Kevin Goedecke
"Scaling RAG Applications to serve millions of users", Kevin Goedecke
 
Dandelion Hashtable: beyond billion requests per second on a commodity server
Dandelion Hashtable: beyond billion requests per second on a commodity serverDandelion Hashtable: beyond billion requests per second on a commodity server
Dandelion Hashtable: beyond billion requests per second on a commodity server
 
PRODUCT LISTING OPTIMIZATION PRESENTATION.pptx
PRODUCT LISTING OPTIMIZATION PRESENTATION.pptxPRODUCT LISTING OPTIMIZATION PRESENTATION.pptx
PRODUCT LISTING OPTIMIZATION PRESENTATION.pptx
 
Mutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented ChatbotsMutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented Chatbots
 
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge GraphGraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
GraphRAG for LifeSciences Hands-On with the Clinical Knowledge Graph
 
Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
 
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-EfficiencyFreshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
 
"$10 thousand per minute of downtime: architecture, queues, streaming and fin...
"$10 thousand per minute of downtime: architecture, queues, streaming and fin..."$10 thousand per minute of downtime: architecture, queues, streaming and fin...
"$10 thousand per minute of downtime: architecture, queues, streaming and fin...
 
Biomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Biomedical Knowledge Graphs for Data Scientists and BioinformaticiansBiomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Biomedical Knowledge Graphs for Data Scientists and Bioinformaticians
 
GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)
 
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectorsConnector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
 
Session 1 - Intro to Robotic Process Automation.pdf
Session 1 - Intro to Robotic Process Automation.pdfSession 1 - Intro to Robotic Process Automation.pdf
Session 1 - Intro to Robotic Process Automation.pdf
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
 
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
 
A Deep Dive into ScyllaDB's Architecture
A Deep Dive into ScyllaDB's ArchitectureA Deep Dive into ScyllaDB's Architecture
A Deep Dive into ScyllaDB's Architecture
 
Christine's Supplier Sourcing Presentaion.pptx
Christine's Supplier Sourcing Presentaion.pptxChristine's Supplier Sourcing Presentaion.pptx
Christine's Supplier Sourcing Presentaion.pptx
 

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/