SlideShare ist ein Scribd-Unternehmen logo
1 von 23
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Was ist angesagt?

Writing Software not Code with Cucumber
Writing Software not Code with CucumberWriting Software not Code with Cucumber
Writing Software not Code with CucumberBen Mabey
 
Behaviour Driven Development con Behat & Drupal
Behaviour Driven Development con Behat & DrupalBehaviour Driven Development con Behat & Drupal
Behaviour Driven Development con Behat & Drupalsparkfabrik
 
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 perlDean Hamstead
 
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 Developmentmtoppa
 
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 BehatRyan 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.3Fabien Potencier
 
Plugin development wpmeetup010
Plugin development wpmeetup010Plugin development wpmeetup010
Plugin development wpmeetup010Barry 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 groovyJessie Evangelista
 
Introduction to WordPress Hooks 2016
Introduction to WordPress Hooks 2016Introduction to WordPress Hooks 2016
Introduction to WordPress Hooks 2016Ian Wilson
 
Django Introduction & Tutorial
Django Introduction & TutorialDjango Introduction & Tutorial
Django Introduction & Tutorial之宇 趙
 
Apache Ant
Apache AntApache Ant
Apache AntAli Bahu
 
Bending word press to your will
Bending word press to your willBending word press to your will
Bending word press to your willTom 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 Agarwalratneshsinghparihar
 
Plugin Development @ WordCamp Norway 2014
Plugin Development @ WordCamp Norway 2014Plugin Development @ WordCamp Norway 2014
Plugin Development @ WordCamp Norway 2014Barry Kooij
 
WordPress Plugin Basics
WordPress Plugin BasicsWordPress Plugin Basics
WordPress Plugin BasicsAmanda Giles
 
Test automation with Cucumber-JVM
Test automation with Cucumber-JVMTest automation with Cucumber-JVM
Test automation with Cucumber-JVMAlan Parkinson
 

Was ist angesagt? (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
 

Andere mochten auch

Plugin jQuery, Design Patterns
Plugin jQuery, Design PatternsPlugin jQuery, Design Patterns
Plugin jQuery, Design PatternsRobert Casanova
 
jQuery Plugin Creation
jQuery Plugin CreationjQuery Plugin Creation
jQuery Plugin Creationbenalman
 
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 developerskim.mens
 
Building GPE: What We Learned
Building GPE: What We LearnedBuilding GPE: What We Learned
Building GPE: What We Learnedrajeevdayal
 
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 PluginDaniel Spilker
 

Andere mochten auch (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
 

Ähnlich wie 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 PluginAndy 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 pluginJohn Tighe
 
Word press Plugins by WordPress Experts
Word press Plugins by WordPress ExpertsWord press Plugins by WordPress Experts
Word press Plugins by WordPress ExpertsYameen Khan
 
Write your first WordPress plugin
Write your first WordPress pluginWrite your first WordPress plugin
Write your first WordPress pluginAnthony Montalbano
 
Plug in development
Plug in developmentPlug in development
Plug in developmentLucky 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 PluginAndolasoft Inc
 
Wordpress plugin creation_overview
Wordpress plugin creation_overviewWordpress plugin creation_overview
Wordpress plugin creation_overviewDaniel Kline
 
Build Your Own Instagram Filters
Build Your Own Instagram FiltersBuild Your Own Instagram Filters
Build Your Own Instagram FiltersTJ Stalcup
 
Best practices in WordPress Development
Best practices in WordPress DevelopmentBest practices in WordPress Development
Best practices in WordPress DevelopmentMindfire Solutions
 
Plugin development demystified 2017
Plugin development demystified 2017Plugin development demystified 2017
Plugin development demystified 2017ylefebvre
 
CSI: WordPress -- Getting Into the Guts
CSI: WordPress -- Getting Into the GutsCSI: WordPress -- Getting Into the Guts
CSI: WordPress -- Getting Into the GutsDougal 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 pluginsStephanie 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.pptxRishiGandhi19
 
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 pluginMainak 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 2011David Carr
 

Ähnlich wie 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
 

Kürzlich hochgeladen

Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 

Kürzlich hochgeladen (20)

Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 

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/