SlideShare a Scribd company logo
1 of 40
Download to read offline
Hooked on WordPress
An Introduction to Actions & Filters
@shawnhooper - shawnhooper.ca
WordCamp Columbus
July 17, 2015
@shawnhooper - shawnhooper.ca
Hi, I’m Shawn
• Developing for the web since the mid-90s
!
• Using WordPress since ~ version 2.8 (2009)
!
• Chief Technology Officer at Actionable Books
!
• Lives in Ottawa, Canada
What is a Hook ?
@shawnhooper - shawnhooper.ca
What is a Hook ?
@shawnhooper - shawnhooper.ca
What is a Hook ?
@shawnhooper - shawnhooper.ca
What is a Hook ?
@shawnhooper - shawnhooper.ca
What is a Hook ?
@shawnhooper - shawnhooper.ca
What is a Hook ?
@shawnhooper - shawnhooper.ca
What is a Hook ?
@shawnhooper - shawnhooper.ca
Never Modify Core
What is a Hook ?
@shawnhooper - shawnhooper.ca
Hooks allow you to change the core functionality
of WordPress.
!
They are implemented through 

themes and plugins.
What is a Hook ?
@shawnhooper - shawnhooper.ca
WordPress Plugin API
What is a Hook ?
@shawnhooper - shawnhooper.ca
There are two kinds of hooks in WordPress:
!
Actions and Filters
Action Hooks
@shawnhooper - shawnhooper.ca
Flickr: ifindkarma
Action Hooks
@shawnhooper - shawnhooper.ca
An action is triggered when a
specific event takes place in WordPress.
Action Hooks
@shawnhooper - shawnhooper.ca
What can you do in an action?!
!
Modify data in the database
Send an e-mail
Interact with APIs
Modify code being sent to the browser
Action Hooks
@shawnhooper - shawnhooper.ca
do_action( $tag );
!
do_action ($tag, $arg_a, $arg_b, $arg_c );
Action Hooks
@shawnhooper - shawnhooper.ca
do_action ( ‘wp_head’ );!
!
wp-includesgeneral-template.php
do_action ( ‘save_post’, $post->ID, $post, $update );!
!
wp-includespost.php
Action Hooks
@shawnhooper - shawnhooper.ca
add_action ( $hook, $function_to_add );



add_action( $hook, $function_to_add, $priority, $accepted_args );
Action Hooks
@shawnhooper - shawnhooper.ca
The optional priority parameter allows you to
specify the order in which the hooked actions
will run.
!
The default value is 10.
Action Hooks
@shawnhooper - shawnhooper.ca
add_action( ‘wp_head’, ‘smh_add_metadesc’);
function smh_add_metadesc() {
echo ‘<meta name=“description” value=“Hello!” />’;
}
Filter Hooks
@shawnhooper - shawnhooper.ca
Filter Hooks
@shawnhooper - shawnhooper.ca
Filters are functions that WordPress passes data
through, at certain points in execution, just before
taking some action with the data.
!
Source: WordPress Codex
Filter Hooks
@shawnhooper - shawnhooper.ca
$filter = apply_filters( $tag, $value );
!
$value = apply_filters ($tag, $value, $var1, $var2, … );
Filter Hooks
@shawnhooper - shawnhooper.ca
$content = apply_filters ( ‘the_content’, $content );!
!
wp-includespost-template.php
$atts = apply_filters( 'wp_mail', compact( 'to', 'subject',
'message', 'headers', 'attachments' ) );



wp-includespluggable.php
Filter Hooks
@shawnhooper - shawnhooper.ca
add_filter ( $tag, $function_to_add );



add_filter( $tag, $function_to_add, $priority, $accepted_args );
Filter Hooks
@shawnhooper - shawnhooper.ca
The optional priority parameter allows you to
specify the order in which the hooked filters
will run.
!
The default value is 10.
Filter Hooks
@shawnhooper - shawnhooper.ca
function smh_old_post_notice($content) {
!
$days = floor( ( time() - get_the_date( 'U' ) ) / ( 60 * 60 * 24 ) );
!
if ( $days > 365 ) {
$content = '<div class="old_post">This is an older post. Beware of
possible out-of-date advice.</div>' . $content;
}
!
return $content;
}
!
add_filter(‘the_content’, ‘smh_old_post_notice’);
Hooks in Themes
@shawnhooper - shawnhooper.ca
99% of the time, you’ll use hooks and filters
in plugins.
!
But there are some times where it’s useful to put these
functions into your theme’s functions.php file.
Hooks in Themes
@shawnhooper - shawnhooper.ca
Examples:
!
Theme Activation Hook
Modifying the “Read More” Text
Filtering Menu Content
Setting up the Customizer
!
These are all things that are isolated to the active theme. If you
change themes, your site won’t break.
That’s the basics….
@shawnhooper - shawnhooper.ca
Extending Plugins
@shawnhooper - shawnhooper.ca
WordPress Core isn’t the only thing that can be extended
with hooks.
!
A well written plugin can also expose Actions and Filters
that will allow you to add, remove, or modify
its functionality.
Variable Hooks
@shawnhooper - shawnhooper.ca
Some hooks include variables in their names. These are very
powerful hooks that allow you to interact with only specific objects
in WordPress.
!
save_post_{$post_type}

ex: add_action ( ‘save_post_page’, ‘my_function’);
!
would only trigger when a page is being saved, not a post.
Pluggable Functions
@shawnhooper - shawnhooper.ca
A set of core WordPress functions that can be overridden in your
plugins.
!
They can all be found in wp-includespluggable.php
!
!
NOTE: No new pluggable functions are being added. They are
being replaced with filters, a more flexible solution.
Pluggable Functions
@shawnhooper - shawnhooper.ca
Only one plugin can make use of these functions. For safety, wrap
the function with the function_exists() check:
if ( ! function_exists( ‘wp_mail’ ) ) {

function wp_mail($to, $subject, $message, $headers = ‘’) {
// do stuff
}

}
Removing Hooks
@shawnhooper - shawnhooper.ca
You can also remove hooks that have already been put into place.
!
!
remove_action( $tag, $function_name, $priority );
!
remove_filter ($tag, $function_to_remove, $priority );
Removing Hooks
@shawnhooper - shawnhooper.ca
You can also remove ALL hooks that have already been put into
place.
!
!
remove_all_actions( $tag, $priority );
!
remove_all_filters ($tag, $priority );
Naming Conventions
@shawnhooper - shawnhooper.ca
Function names must be unique
!
(or you risk the White Screen of Death!)
Naming Conventions
@shawnhooper - shawnhooper.ca
PRO TIP: Put your plugin in a class.!
!
add_action( ‘save_post’, array( $this, ‘send_email’) );



add_filter( ‘the_content’, array( $this, ‘old_post_warning’) );
@shawnhooper - shawnhooper.ca
Thank You!
@shawnhooper - shawnhooper.ca
Questions ?
Twitter: @shawnhooper



WordPress Slack: @shooper



Slides & Notes Posted on:

www.shawnhooper.ca



E-Mail:
shawn@actionablebooks.com

More Related Content

What's hot

An Introduction to WordPress Hooks
An Introduction to WordPress HooksAn Introduction to WordPress Hooks
An Introduction to WordPress HooksAndrew Marks
 
Story Driven Development With Cucumber
Story Driven Development With CucumberStory Driven Development With Cucumber
Story Driven Development With CucumberSean Cribbs
 
Introduction to VueJS & The WordPress REST API
Introduction to VueJS & The WordPress REST APIIntroduction to VueJS & The WordPress REST API
Introduction to VueJS & The WordPress REST APICaldera Labs
 
PowerShell with SharePoint 2013 and Office 365 - EPC Group
PowerShell with SharePoint 2013 and Office 365 - EPC GroupPowerShell with SharePoint 2013 and Office 365 - EPC Group
PowerShell with SharePoint 2013 and Office 365 - EPC GroupEPC Group
 
Rails for Beginners - Le Wagon
Rails for Beginners - Le WagonRails for Beginners - Le Wagon
Rails for Beginners - Le WagonAlex Benoit
 
Simplifying Code: Monster to Elegant in 5 Steps
Simplifying Code: Monster to Elegant in 5 StepsSimplifying Code: Monster to Elegant in 5 Steps
Simplifying Code: Monster to Elegant in 5 Stepstutec
 
Rails Antipatterns | Open Session with Chad Pytel
Rails Antipatterns | Open Session with Chad Pytel Rails Antipatterns | Open Session with Chad Pytel
Rails Antipatterns | Open Session with Chad Pytel Engine Yard
 
You're Doing It Wrong
You're Doing It WrongYou're Doing It Wrong
You're Doing It Wrongbostonrb
 
jQuery - Doing it right
jQuery - Doing it rightjQuery - Doing it right
jQuery - Doing it rightgirish82
 
WordCamp San Francisco 2011: Transients, Caching, and the Complexities of Mul...
WordCamp San Francisco 2011: Transients, Caching, and the Complexities of Mul...WordCamp San Francisco 2011: Transients, Caching, and the Complexities of Mul...
WordCamp San Francisco 2011: Transients, Caching, and the Complexities of Mul...andrewnacin
 
Java spring mysql without hibernate(2) (1)
Java spring mysql without hibernate(2) (1)Java spring mysql without hibernate(2) (1)
Java spring mysql without hibernate(2) (1)AmedJacobReza
 
Concurrent PHP in the Etsy API
Concurrent PHP in the Etsy APIConcurrent PHP in the Etsy API
Concurrent PHP in the Etsy APIMatt Graham
 
You Don't Know Query - WordCamp Portland 2011
You Don't Know Query - WordCamp Portland 2011You Don't Know Query - WordCamp Portland 2011
You Don't Know Query - WordCamp Portland 2011andrewnacin
 
My first WordPress Plugin
My first WordPress PluginMy first WordPress Plugin
My first WordPress PluginAbbas Siddiqi
 
Writing Software not Code with Cucumber
Writing Software not Code with CucumberWriting Software not Code with Cucumber
Writing Software not Code with CucumberBen Mabey
 
AngularJS for Legacy Apps
AngularJS for Legacy AppsAngularJS for Legacy Apps
AngularJS for Legacy AppsPeter Drinnan
 
Entry-level PHP for WordPress
Entry-level PHP for WordPressEntry-level PHP for WordPress
Entry-level PHP for WordPresssprclldr
 

What's hot (20)

An Introduction to WordPress Hooks
An Introduction to WordPress HooksAn Introduction to WordPress Hooks
An Introduction to WordPress Hooks
 
Story Driven Development With Cucumber
Story Driven Development With CucumberStory Driven Development With Cucumber
Story Driven Development With Cucumber
 
Introduction to VueJS & The WordPress REST API
Introduction to VueJS & The WordPress REST APIIntroduction to VueJS & The WordPress REST API
Introduction to VueJS & The WordPress REST API
 
PowerShell with SharePoint 2013 and Office 365 - EPC Group
PowerShell with SharePoint 2013 and Office 365 - EPC GroupPowerShell with SharePoint 2013 and Office 365 - EPC Group
PowerShell with SharePoint 2013 and Office 365 - EPC Group
 
Rails for Beginners - Le Wagon
Rails for Beginners - Le WagonRails for Beginners - Le Wagon
Rails for Beginners - Le Wagon
 
Simplifying Code: Monster to Elegant in 5 Steps
Simplifying Code: Monster to Elegant in 5 StepsSimplifying Code: Monster to Elegant in 5 Steps
Simplifying Code: Monster to Elegant in 5 Steps
 
Rails Antipatterns | Open Session with Chad Pytel
Rails Antipatterns | Open Session with Chad Pytel Rails Antipatterns | Open Session with Chad Pytel
Rails Antipatterns | Open Session with Chad Pytel
 
You're Doing It Wrong
You're Doing It WrongYou're Doing It Wrong
You're Doing It Wrong
 
jQuery - Doing it right
jQuery - Doing it rightjQuery - Doing it right
jQuery - Doing it right
 
WordCamp San Francisco 2011: Transients, Caching, and the Complexities of Mul...
WordCamp San Francisco 2011: Transients, Caching, and the Complexities of Mul...WordCamp San Francisco 2011: Transients, Caching, and the Complexities of Mul...
WordCamp San Francisco 2011: Transients, Caching, and the Complexities of Mul...
 
Java spring mysql without hibernate(2) (1)
Java spring mysql without hibernate(2) (1)Java spring mysql without hibernate(2) (1)
Java spring mysql without hibernate(2) (1)
 
Concurrent PHP in the Etsy API
Concurrent PHP in the Etsy APIConcurrent PHP in the Etsy API
Concurrent PHP in the Etsy API
 
You Don't Know Query - WordCamp Portland 2011
You Don't Know Query - WordCamp Portland 2011You Don't Know Query - WordCamp Portland 2011
You Don't Know Query - WordCamp Portland 2011
 
My first WordPress Plugin
My first WordPress PluginMy first WordPress Plugin
My first WordPress Plugin
 
Rails OO views
Rails OO viewsRails OO views
Rails OO views
 
Routes
RoutesRoutes
Routes
 
Jquery tutorial
Jquery tutorialJquery tutorial
Jquery tutorial
 
Writing Software not Code with Cucumber
Writing Software not Code with CucumberWriting Software not Code with Cucumber
Writing Software not Code with Cucumber
 
AngularJS for Legacy Apps
AngularJS for Legacy AppsAngularJS for Legacy Apps
AngularJS for Legacy Apps
 
Entry-level PHP for WordPress
Entry-level PHP for WordPressEntry-level PHP for WordPress
Entry-level PHP for WordPress
 

Similar to Hooked on WordPress: WordCamp Columbus

How To Write a WordPress Plugin
How To Write a WordPress PluginHow To Write a WordPress Plugin
How To Write a WordPress PluginAndy Stratton
 
WordPress Hooks (Actions & Filters)
WordPress Hooks (Actions & Filters)WordPress Hooks (Actions & Filters)
WordPress Hooks (Actions & Filters)MuhammadKashif596
 
Introduction to WordPress Development - Hooks
Introduction to WordPress Development - HooksIntroduction to WordPress Development - Hooks
Introduction to WordPress Development - HooksEdmund Chan
 
The Way to Theme Enlightenment
The Way to Theme EnlightenmentThe Way to Theme Enlightenment
The Way to Theme EnlightenmentAmanda Giles
 
Creating Your First WordPress Plugin
Creating Your First WordPress PluginCreating Your First WordPress Plugin
Creating Your First WordPress PluginBrad Williams
 
Jumping Into WordPress Plugin Programming
Jumping Into WordPress Plugin ProgrammingJumping Into WordPress Plugin Programming
Jumping Into WordPress Plugin ProgrammingDougal Campbell
 
Introduction to WordPress Hooks 2016
Introduction to WordPress Hooks 2016Introduction to WordPress Hooks 2016
Introduction to WordPress Hooks 2016Ian Wilson
 
[Bristol WordPress] Supercharging WordPress Development
[Bristol WordPress] Supercharging WordPress Development[Bristol WordPress] Supercharging WordPress Development
[Bristol WordPress] Supercharging WordPress DevelopmentAdam Tomat
 
Extending & Scaling | Dallas PHP
Extending & Scaling | Dallas PHPExtending & Scaling | Dallas PHP
Extending & Scaling | Dallas PHPrandyhoyt
 
The Way to Theme Enlightenment 2017
The Way to Theme Enlightenment 2017The Way to Theme Enlightenment 2017
The Way to Theme Enlightenment 2017Amanda Giles
 
Intro to WordPress Plugin Development
Intro to WordPress Plugin DevelopmentIntro to WordPress Plugin Development
Intro to WordPress Plugin DevelopmentBrad Williams
 
<Head> Presentation: Plugging Into Wordpress
<Head> Presentation: Plugging Into Wordpress<Head> Presentation: Plugging Into Wordpress
<Head> Presentation: Plugging Into WordpressMatt Harris
 
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
 
Introduction to WordPress Security
Introduction to WordPress SecurityIntroduction to WordPress Security
Introduction to WordPress SecurityShawn Hooper
 
Plug in development
Plug in developmentPlug in development
Plug in developmentLucky Ali
 
Bending word press to your will
Bending word press to your willBending word press to your will
Bending word press to your willTom Jenkins
 
WooCommerce: Filter Hooks
WooCommerce: Filter HooksWooCommerce: Filter Hooks
WooCommerce: Filter HooksRodolfo Melogli
 

Similar to Hooked on WordPress: WordCamp Columbus (20)

How To Write a WordPress Plugin
How To Write a WordPress PluginHow To Write a WordPress Plugin
How To Write a WordPress Plugin
 
WordPress Hooks (Actions & Filters)
WordPress Hooks (Actions & Filters)WordPress Hooks (Actions & Filters)
WordPress Hooks (Actions & Filters)
 
Introduction to WordPress Development - Hooks
Introduction to WordPress Development - HooksIntroduction to WordPress Development - Hooks
Introduction to WordPress Development - Hooks
 
The Way to Theme Enlightenment
The Way to Theme EnlightenmentThe Way to Theme Enlightenment
The Way to Theme Enlightenment
 
Creating Your First WordPress Plugin
Creating Your First WordPress PluginCreating Your First WordPress Plugin
Creating Your First WordPress Plugin
 
Rebrand WordPress Admin
Rebrand WordPress AdminRebrand WordPress Admin
Rebrand WordPress Admin
 
Jumping Into WordPress Plugin Programming
Jumping Into WordPress Plugin ProgrammingJumping Into WordPress Plugin Programming
Jumping Into WordPress Plugin Programming
 
Introduction to WordPress Hooks 2016
Introduction to WordPress Hooks 2016Introduction to WordPress Hooks 2016
Introduction to WordPress Hooks 2016
 
[Bristol WordPress] Supercharging WordPress Development
[Bristol WordPress] Supercharging WordPress Development[Bristol WordPress] Supercharging WordPress Development
[Bristol WordPress] Supercharging WordPress Development
 
Extending & Scaling | Dallas PHP
Extending & Scaling | Dallas PHPExtending & Scaling | Dallas PHP
Extending & Scaling | Dallas PHP
 
The Way to Theme Enlightenment 2017
The Way to Theme Enlightenment 2017The Way to Theme Enlightenment 2017
The Way to Theme Enlightenment 2017
 
5 W's of Hookin'
5 W's of Hookin'5 W's of Hookin'
5 W's of Hookin'
 
Intro to WordPress Plugin Development
Intro to WordPress Plugin DevelopmentIntro to WordPress Plugin Development
Intro to WordPress Plugin Development
 
WooCommerce filters
WooCommerce filtersWooCommerce filters
WooCommerce filters
 
<Head> Presentation: Plugging Into Wordpress
<Head> Presentation: Plugging Into Wordpress<Head> Presentation: Plugging Into Wordpress
<Head> Presentation: Plugging Into Wordpress
 
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
 
Introduction to WordPress Security
Introduction to WordPress SecurityIntroduction to WordPress Security
Introduction to WordPress Security
 
Plug in development
Plug in developmentPlug in development
Plug in development
 
Bending word press to your will
Bending word press to your willBending word press to your will
Bending word press to your will
 
WooCommerce: Filter Hooks
WooCommerce: Filter HooksWooCommerce: Filter Hooks
WooCommerce: Filter Hooks
 

More from Shawn Hooper

WP REST API: Actionable.co
WP REST API: Actionable.coWP REST API: Actionable.co
WP REST API: Actionable.coShawn Hooper
 
Database Considerations for SaaS Products
Database Considerations for SaaS ProductsDatabase Considerations for SaaS Products
Database Considerations for SaaS ProductsShawn Hooper
 
Payments Made Easy with Stripe
Payments Made Easy with StripePayments Made Easy with Stripe
Payments Made Easy with StripeShawn Hooper
 
WordPress Coding Standards & Best Practices
WordPress Coding Standards & Best PracticesWordPress Coding Standards & Best Practices
WordPress Coding Standards & Best PracticesShawn Hooper
 
Save Time By Manging WordPress from the Command Line
Save Time By Manging WordPress from the Command LineSave Time By Manging WordPress from the Command Line
Save Time By Manging WordPress from the Command LineShawn Hooper
 
Writing Clean, Standards Compliant, Testable Code for WordPress
Writing Clean, Standards Compliant, Testable Code for WordPressWriting Clean, Standards Compliant, Testable Code for WordPress
Writing Clean, Standards Compliant, Testable Code for WordPressShawn Hooper
 
Creating Multilingual WordPress Websites
Creating Multilingual WordPress WebsitesCreating Multilingual WordPress Websites
Creating Multilingual WordPress WebsitesShawn Hooper
 
Creating Multilingual WordPress Websites
Creating Multilingual WordPress WebsitesCreating Multilingual WordPress Websites
Creating Multilingual WordPress WebsitesShawn Hooper
 
WP-CLI Presentation from WordCamp NYC 2015
WP-CLI Presentation from WordCamp NYC 2015WP-CLI Presentation from WordCamp NYC 2015
WP-CLI Presentation from WordCamp NYC 2015Shawn Hooper
 
Securing WordPress
Securing WordPressSecuring WordPress
Securing WordPressShawn Hooper
 
Writing Secure Code for WordPress
Writing Secure Code for WordPressWriting Secure Code for WordPress
Writing Secure Code for WordPressShawn Hooper
 
Manage WordPress From the Command Line with WP-CLI
Manage WordPress From the Command Line with WP-CLIManage WordPress From the Command Line with WP-CLI
Manage WordPress From the Command Line with WP-CLIShawn Hooper
 
WP-CLI Talk from WordCamp Montreal
WP-CLI Talk from WordCamp MontrealWP-CLI Talk from WordCamp Montreal
WP-CLI Talk from WordCamp MontrealShawn Hooper
 
WP-CLI - WordCamp Miami 2015
WP-CLI - WordCamp Miami 2015WP-CLI - WordCamp Miami 2015
WP-CLI - WordCamp Miami 2015Shawn Hooper
 
Save Time by Managing WordPress from the Command Line
Save Time by Managing WordPress from the Command LineSave Time by Managing WordPress from the Command Line
Save Time by Managing WordPress from the Command LineShawn Hooper
 
Time Code: Automating Tasks in WordPress with WP-Cron
Time Code: Automating Tasks in WordPress with WP-CronTime Code: Automating Tasks in WordPress with WP-Cron
Time Code: Automating Tasks in WordPress with WP-CronShawn Hooper
 

More from Shawn Hooper (16)

WP REST API: Actionable.co
WP REST API: Actionable.coWP REST API: Actionable.co
WP REST API: Actionable.co
 
Database Considerations for SaaS Products
Database Considerations for SaaS ProductsDatabase Considerations for SaaS Products
Database Considerations for SaaS Products
 
Payments Made Easy with Stripe
Payments Made Easy with StripePayments Made Easy with Stripe
Payments Made Easy with Stripe
 
WordPress Coding Standards & Best Practices
WordPress Coding Standards & Best PracticesWordPress Coding Standards & Best Practices
WordPress Coding Standards & Best Practices
 
Save Time By Manging WordPress from the Command Line
Save Time By Manging WordPress from the Command LineSave Time By Manging WordPress from the Command Line
Save Time By Manging WordPress from the Command Line
 
Writing Clean, Standards Compliant, Testable Code for WordPress
Writing Clean, Standards Compliant, Testable Code for WordPressWriting Clean, Standards Compliant, Testable Code for WordPress
Writing Clean, Standards Compliant, Testable Code for WordPress
 
Creating Multilingual WordPress Websites
Creating Multilingual WordPress WebsitesCreating Multilingual WordPress Websites
Creating Multilingual WordPress Websites
 
Creating Multilingual WordPress Websites
Creating Multilingual WordPress WebsitesCreating Multilingual WordPress Websites
Creating Multilingual WordPress Websites
 
WP-CLI Presentation from WordCamp NYC 2015
WP-CLI Presentation from WordCamp NYC 2015WP-CLI Presentation from WordCamp NYC 2015
WP-CLI Presentation from WordCamp NYC 2015
 
Securing WordPress
Securing WordPressSecuring WordPress
Securing WordPress
 
Writing Secure Code for WordPress
Writing Secure Code for WordPressWriting Secure Code for WordPress
Writing Secure Code for WordPress
 
Manage WordPress From the Command Line with WP-CLI
Manage WordPress From the Command Line with WP-CLIManage WordPress From the Command Line with WP-CLI
Manage WordPress From the Command Line with WP-CLI
 
WP-CLI Talk from WordCamp Montreal
WP-CLI Talk from WordCamp MontrealWP-CLI Talk from WordCamp Montreal
WP-CLI Talk from WordCamp Montreal
 
WP-CLI - WordCamp Miami 2015
WP-CLI - WordCamp Miami 2015WP-CLI - WordCamp Miami 2015
WP-CLI - WordCamp Miami 2015
 
Save Time by Managing WordPress from the Command Line
Save Time by Managing WordPress from the Command LineSave Time by Managing WordPress from the Command Line
Save Time by Managing WordPress from the Command Line
 
Time Code: Automating Tasks in WordPress with WP-Cron
Time Code: Automating Tasks in WordPress with WP-CronTime Code: Automating Tasks in WordPress with WP-Cron
Time Code: Automating Tasks in WordPress with WP-Cron
 

Recently uploaded

Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
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
 
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
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGSujit Pal
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
[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
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
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
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 

Recently uploaded (20)

Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
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...
 
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
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAG
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
[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
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
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
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 

Hooked on WordPress: WordCamp Columbus

  • 1. Hooked on WordPress An Introduction to Actions & Filters @shawnhooper - shawnhooper.ca WordCamp Columbus July 17, 2015
  • 2. @shawnhooper - shawnhooper.ca Hi, I’m Shawn • Developing for the web since the mid-90s ! • Using WordPress since ~ version 2.8 (2009) ! • Chief Technology Officer at Actionable Books ! • Lives in Ottawa, Canada
  • 3. What is a Hook ? @shawnhooper - shawnhooper.ca
  • 4. What is a Hook ? @shawnhooper - shawnhooper.ca
  • 5. What is a Hook ? @shawnhooper - shawnhooper.ca
  • 6. What is a Hook ? @shawnhooper - shawnhooper.ca
  • 7. What is a Hook ? @shawnhooper - shawnhooper.ca
  • 8. What is a Hook ? @shawnhooper - shawnhooper.ca
  • 9. What is a Hook ? @shawnhooper - shawnhooper.ca Never Modify Core
  • 10. What is a Hook ? @shawnhooper - shawnhooper.ca Hooks allow you to change the core functionality of WordPress. ! They are implemented through 
 themes and plugins.
  • 11. What is a Hook ? @shawnhooper - shawnhooper.ca WordPress Plugin API
  • 12. What is a Hook ? @shawnhooper - shawnhooper.ca There are two kinds of hooks in WordPress: ! Actions and Filters
  • 13. Action Hooks @shawnhooper - shawnhooper.ca Flickr: ifindkarma
  • 14. Action Hooks @shawnhooper - shawnhooper.ca An action is triggered when a specific event takes place in WordPress.
  • 15. Action Hooks @shawnhooper - shawnhooper.ca What can you do in an action?! ! Modify data in the database Send an e-mail Interact with APIs Modify code being sent to the browser
  • 16. Action Hooks @shawnhooper - shawnhooper.ca do_action( $tag ); ! do_action ($tag, $arg_a, $arg_b, $arg_c );
  • 17. Action Hooks @shawnhooper - shawnhooper.ca do_action ( ‘wp_head’ );! ! wp-includesgeneral-template.php do_action ( ‘save_post’, $post->ID, $post, $update );! ! wp-includespost.php
  • 18. Action Hooks @shawnhooper - shawnhooper.ca add_action ( $hook, $function_to_add );
 
 add_action( $hook, $function_to_add, $priority, $accepted_args );
  • 19. Action Hooks @shawnhooper - shawnhooper.ca The optional priority parameter allows you to specify the order in which the hooked actions will run. ! The default value is 10.
  • 20. Action Hooks @shawnhooper - shawnhooper.ca add_action( ‘wp_head’, ‘smh_add_metadesc’); function smh_add_metadesc() { echo ‘<meta name=“description” value=“Hello!” />’; }
  • 21. Filter Hooks @shawnhooper - shawnhooper.ca
  • 22. Filter Hooks @shawnhooper - shawnhooper.ca Filters are functions that WordPress passes data through, at certain points in execution, just before taking some action with the data. ! Source: WordPress Codex
  • 23. Filter Hooks @shawnhooper - shawnhooper.ca $filter = apply_filters( $tag, $value ); ! $value = apply_filters ($tag, $value, $var1, $var2, … );
  • 24. Filter Hooks @shawnhooper - shawnhooper.ca $content = apply_filters ( ‘the_content’, $content );! ! wp-includespost-template.php $atts = apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers', 'attachments' ) );
 
 wp-includespluggable.php
  • 25. Filter Hooks @shawnhooper - shawnhooper.ca add_filter ( $tag, $function_to_add );
 
 add_filter( $tag, $function_to_add, $priority, $accepted_args );
  • 26. Filter Hooks @shawnhooper - shawnhooper.ca The optional priority parameter allows you to specify the order in which the hooked filters will run. ! The default value is 10.
  • 27. Filter Hooks @shawnhooper - shawnhooper.ca function smh_old_post_notice($content) { ! $days = floor( ( time() - get_the_date( 'U' ) ) / ( 60 * 60 * 24 ) ); ! if ( $days > 365 ) { $content = '<div class="old_post">This is an older post. Beware of possible out-of-date advice.</div>' . $content; } ! return $content; } ! add_filter(‘the_content’, ‘smh_old_post_notice’);
  • 28. Hooks in Themes @shawnhooper - shawnhooper.ca 99% of the time, you’ll use hooks and filters in plugins. ! But there are some times where it’s useful to put these functions into your theme’s functions.php file.
  • 29. Hooks in Themes @shawnhooper - shawnhooper.ca Examples: ! Theme Activation Hook Modifying the “Read More” Text Filtering Menu Content Setting up the Customizer ! These are all things that are isolated to the active theme. If you change themes, your site won’t break.
  • 31. Extending Plugins @shawnhooper - shawnhooper.ca WordPress Core isn’t the only thing that can be extended with hooks. ! A well written plugin can also expose Actions and Filters that will allow you to add, remove, or modify its functionality.
  • 32. Variable Hooks @shawnhooper - shawnhooper.ca Some hooks include variables in their names. These are very powerful hooks that allow you to interact with only specific objects in WordPress. ! save_post_{$post_type}
 ex: add_action ( ‘save_post_page’, ‘my_function’); ! would only trigger when a page is being saved, not a post.
  • 33. Pluggable Functions @shawnhooper - shawnhooper.ca A set of core WordPress functions that can be overridden in your plugins. ! They can all be found in wp-includespluggable.php ! ! NOTE: No new pluggable functions are being added. They are being replaced with filters, a more flexible solution.
  • 34. Pluggable Functions @shawnhooper - shawnhooper.ca Only one plugin can make use of these functions. For safety, wrap the function with the function_exists() check: if ( ! function_exists( ‘wp_mail’ ) ) {
 function wp_mail($to, $subject, $message, $headers = ‘’) { // do stuff }
 }
  • 35. Removing Hooks @shawnhooper - shawnhooper.ca You can also remove hooks that have already been put into place. ! ! remove_action( $tag, $function_name, $priority ); ! remove_filter ($tag, $function_to_remove, $priority );
  • 36. Removing Hooks @shawnhooper - shawnhooper.ca You can also remove ALL hooks that have already been put into place. ! ! remove_all_actions( $tag, $priority ); ! remove_all_filters ($tag, $priority );
  • 37. Naming Conventions @shawnhooper - shawnhooper.ca Function names must be unique ! (or you risk the White Screen of Death!)
  • 38. Naming Conventions @shawnhooper - shawnhooper.ca PRO TIP: Put your plugin in a class.! ! add_action( ‘save_post’, array( $this, ‘send_email’) );
 
 add_filter( ‘the_content’, array( $this, ‘old_post_warning’) );
  • 40. @shawnhooper - shawnhooper.ca Questions ? Twitter: @shawnhooper
 
 WordPress Slack: @shooper
 
 Slides & Notes Posted on:
 www.shawnhooper.ca
 
 E-Mail: shawn@actionablebooks.com