SlideShare une entreprise Scribd logo
1  sur  19
Télécharger pour lire hors ligne
WordPress Hooks
Scott Cariss • @l3rady • Big Fish Ltd
What I'm going to cover
The WordPress page lifecycle.
What are hooks and how do they fit into the page lifecycle?
Why use hooks?
What are actions
What are filters
Examples
The WP page lifecycle
"A page life cycle is nothing more than a combination of the
events that take place from when a browser requests a
page to when the server returns the page to the browser."
Lets look at loading a single page in WP
- Look at the requested page ID
- Query the database
- Get associated data (Categories, tags, images, etc)
- Return all of the data to the browser
The WP page lifecycle
The template files and the calls to the API
functions are then responsible for rendering,
styling and position the data on the screen
Sounds simple but this is just a simple page
load. Think of more complex sites WP sites you
have been on.
You can now understand how intensive this
particular process can be.
The WP page lifecycle
"While WordPress is running its series of queries and
preparing to render data back to the browser, it’s looking at
all of the custom hooks – that is, the actions and filters –
that have been written and is passing data through those
filters before returning data to the browser."
At this point it's important to define what hooks
are and look at the difference between action
and filter hooks and how they play into this
whole lifecycle
What are hooks and why use them?
They are the backbone of WordPress.
They enable developers to "hook" into the WP
lifecycle to change how it works without
modifying the core code.
Developers hook code is separate from WP
core so that WP updates don't remove or
destroy the developers code.
Different functions for hooks
Action Hooks
- do_action()
- add_action()
- remove_action()
- has_action()
- do_action_ref_array()
- did_action()
- remove_all_actions()
Filter Hooks
- apply_filters()
- add_filters()
- remove_filters()
- has_filter()
- current_filter()
- merge_filters()
- remove_all_filters()
Using action hooks
do_action( 'hook_name', $arg1, $arg2, ... );
add_action( 'hook_name',
'your_function_name', [priority],
[number_args_accepted] );
remove_action( 'hook_name',
'function_to_remove', [priority],
[number_args_accepted] );
Using filter hooks
$value = apply_filters( 'hook_name', $value,
$value2, ... );
add_filter( 'hook_name',
'your_filter_function', [priority],
[number_args_accepted] );
remove_filter( 'hook_name',
'filter_function_to_remove', [priority],
[number_args_accepted] );
Examples
Changing the excerpt length on the home page:
function wpldn_excerpt( $length ) {
if ( is_home() ) {
return 20;
}
return $length;
}
add_filter( 'excerpt_length', 'wpldn_excerpt', 999 );
Examples
Adding Google Maps JS to contact us page
function wpldn_maps_js() {
if ( !is_page( 'contact-us' ) ) {
return;
}
wp_enqueue_scripts( 'google_maps_api',
'[PATH_TO_GOOGLE_MAP_API]', array(), false, true
);
wp_enqueue_scripts( 'my_map_js', '[PATH_TO_YOUR_JS]',
array( 'google_maps_api' ), false, true );
}
add_action( 'wp_enqueue_scripts', 'wpldn_maps_js' );
Examples
Load a different theme template file at will
function wpldn_any_file() {
if( !is_page( "about-us" ) ) {
return;
}
include( '/path/to/any/file.extension' );
exit();
}
add_action( 'template_redirect', 'wpldn_any_file' );
Examples
Using pre_get_posts to change the main query
function wpldn_change_query( $query ) {
if( $query->is_home() && $query->is_main_query() ) {
$query->set( 'posts_per_page', 20 );
}
}
add_action( 'pre_get_posts', 'wpldn_change_query' );
Examples
Remove that pesky capital_P_dangit function
remove_filter( 'the_title', 'capital_P_dangit', 11 );
remove_filter( 'the_content', 'capital_P_dangit', 11 );
remove_filter( 'comment_text', 'capital_P_dangit', 31 );
* Check /wp-includes/default-filters.php to see what functions WP
is applying to its own filters
Resources
WordPress Codex - Action Reference
Resources
Debug Bar plugin & Action Hooks add on
Don't be scared!
Rummage around in core code to find filters
and hooks that are not documented.
A good knowledge of PHP will help you to find
and understand WordPress hooks.
IDEs can help in navigating through WP core
code.
Questions?

Contenu connexe

Tendances

An Overview of Node.js
An Overview of Node.jsAn Overview of Node.js
An Overview of Node.jsAyush Mishra
 
Grails Plugins(Console, DB Migration, Asset Pipeline and Remote pagination)
Grails Plugins(Console, DB Migration, Asset Pipeline and Remote pagination)Grails Plugins(Console, DB Migration, Asset Pipeline and Remote pagination)
Grails Plugins(Console, DB Migration, Asset Pipeline and Remote pagination)NexThoughts Technologies
 
Ufo Ship for AWS ECS
Ufo Ship for AWS ECSUfo Ship for AWS ECS
Ufo Ship for AWS ECSTung Nguyen
 
Web Deployment With Visual Studio 2010
Web Deployment With Visual Studio 2010Web Deployment With Visual Studio 2010
Web Deployment With Visual Studio 2010Rishu Mehra
 
Hooking with WordPress
Hooking with WordPressHooking with WordPress
Hooking with WordPressEdward Caissie
 
Apache spark with akka couchbase code by bhawani
Apache spark with akka couchbase code by bhawaniApache spark with akka couchbase code by bhawani
Apache spark with akka couchbase code by bhawaniBhawani N Prasad
 
Introducing to node.js
Introducing to node.jsIntroducing to node.js
Introducing to node.jsJeongHun Byeon
 
Intro to BackboneJS + Intermediate Javascript
Intro to BackboneJS + Intermediate JavascriptIntro to BackboneJS + Intermediate Javascript
Intro to BackboneJS + Intermediate JavascriptAndrew Lovett-Barron
 
Service Worker - Reliability bits
Service Worker - Reliability bitsService Worker - Reliability bits
Service Worker - Reliability bitsjungkees
 
N:1 Replication meets MHA
N:1 Replication meets MHAN:1 Replication meets MHA
N:1 Replication meets MHAdo_aki
 
Web Worker, Service Worker and Worklets
Web Worker, Service Worker and WorkletsWeb Worker, Service Worker and Worklets
Web Worker, Service Worker and WorkletsKeshav Gupta
 
BackboneJS Training - Giving Backbone to your applications
BackboneJS Training - Giving Backbone to your applicationsBackboneJS Training - Giving Backbone to your applications
BackboneJS Training - Giving Backbone to your applicationsJoseph Khan
 
Backbone/Marionette recap [2015]
Backbone/Marionette recap [2015]Backbone/Marionette recap [2015]
Backbone/Marionette recap [2015]Andrii Lundiak
 
Timothy N. Tsvetkov, Rails 3.1
Timothy N. Tsvetkov, Rails 3.1Timothy N. Tsvetkov, Rails 3.1
Timothy N. Tsvetkov, Rails 3.1Evil Martians
 
Python Database Connection | Edureka
Python Database Connection | EdurekaPython Database Connection | Edureka
Python Database Connection | EdurekaEdureka!
 
Backbone js in action
Backbone js in actionBackbone js in action
Backbone js in actionUsha Guduri
 

Tendances (20)

An Overview of Node.js
An Overview of Node.jsAn Overview of Node.js
An Overview of Node.js
 
Grails Plugins(Console, DB Migration, Asset Pipeline and Remote pagination)
Grails Plugins(Console, DB Migration, Asset Pipeline and Remote pagination)Grails Plugins(Console, DB Migration, Asset Pipeline and Remote pagination)
Grails Plugins(Console, DB Migration, Asset Pipeline and Remote pagination)
 
Ufo Ship for AWS ECS
Ufo Ship for AWS ECSUfo Ship for AWS ECS
Ufo Ship for AWS ECS
 
Web Deployment With Visual Studio 2010
Web Deployment With Visual Studio 2010Web Deployment With Visual Studio 2010
Web Deployment With Visual Studio 2010
 
Hooking with WordPress
Hooking with WordPressHooking with WordPress
Hooking with WordPress
 
Apache spark with akka couchbase code by bhawani
Apache spark with akka couchbase code by bhawaniApache spark with akka couchbase code by bhawani
Apache spark with akka couchbase code by bhawani
 
Introducing to node.js
Introducing to node.jsIntroducing to node.js
Introducing to node.js
 
Intro to BackboneJS + Intermediate Javascript
Intro to BackboneJS + Intermediate JavascriptIntro to BackboneJS + Intermediate Javascript
Intro to BackboneJS + Intermediate Javascript
 
Spine.js
Spine.jsSpine.js
Spine.js
 
NodeJS @ ACS
NodeJS @ ACSNodeJS @ ACS
NodeJS @ ACS
 
Service Worker - Reliability bits
Service Worker - Reliability bitsService Worker - Reliability bits
Service Worker - Reliability bits
 
N:1 Replication meets MHA
N:1 Replication meets MHAN:1 Replication meets MHA
N:1 Replication meets MHA
 
Web Worker, Service Worker and Worklets
Web Worker, Service Worker and WorkletsWeb Worker, Service Worker and Worklets
Web Worker, Service Worker and Worklets
 
Meet VueJs
Meet VueJsMeet VueJs
Meet VueJs
 
BackboneJS Training - Giving Backbone to your applications
BackboneJS Training - Giving Backbone to your applicationsBackboneJS Training - Giving Backbone to your applications
BackboneJS Training - Giving Backbone to your applications
 
Backbone/Marionette recap [2015]
Backbone/Marionette recap [2015]Backbone/Marionette recap [2015]
Backbone/Marionette recap [2015]
 
Timothy N. Tsvetkov, Rails 3.1
Timothy N. Tsvetkov, Rails 3.1Timothy N. Tsvetkov, Rails 3.1
Timothy N. Tsvetkov, Rails 3.1
 
Python Database Connection | Edureka
Python Database Connection | EdurekaPython Database Connection | Edureka
Python Database Connection | Edureka
 
BackboneJs
BackboneJsBackboneJs
BackboneJs
 
Backbone js in action
Backbone js in actionBackbone js in action
Backbone js in action
 

Similaire à WordPress hooks - WPLDN July 2013 Meetup

AngularJS training - Day 1 - Basics: Why, What and basic features of AngularJS
AngularJS training - Day 1 - Basics: Why, What and basic features of AngularJSAngularJS training - Day 1 - Basics: Why, What and basic features of AngularJS
AngularJS training - Day 1 - Basics: Why, What and basic features of AngularJSmurtazahaveliwala
 
Angular presentation
Angular presentationAngular presentation
Angular presentationMatus Szabo
 
WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...Fabio Franzini
 
Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQueryAlek Davis
 
Application Security from the Inside - OWASP
Application Security from the Inside - OWASPApplication Security from the Inside - OWASP
Application Security from the Inside - OWASPSqreen
 
Code igniter - A brief introduction
Code igniter - A brief introductionCode igniter - A brief introduction
Code igniter - A brief introductionCommit University
 
gDayX 2013 - Advanced AngularJS - Nicolas Embleton
gDayX 2013 - Advanced AngularJS - Nicolas EmbletongDayX 2013 - Advanced AngularJS - Nicolas Embleton
gDayX 2013 - Advanced AngularJS - Nicolas EmbletonGeorge Nguyen
 
important struts interview questions
important struts interview questionsimportant struts interview questions
important struts interview questionssurendray
 
Raybiztech Guide To Backbone Javascript Library
Raybiztech Guide To Backbone Javascript LibraryRaybiztech Guide To Backbone Javascript Library
Raybiztech Guide To Backbone Javascript Libraryray biztech
 
Quick Start: ActiveScaffold
Quick Start: ActiveScaffoldQuick Start: ActiveScaffold
Quick Start: ActiveScaffoldDavid Keener
 
AngularJs Workshop SDP December 28th 2014
AngularJs Workshop SDP December 28th 2014AngularJs Workshop SDP December 28th 2014
AngularJs Workshop SDP December 28th 2014Ran Wahle
 
Understanding angular js
Understanding angular jsUnderstanding angular js
Understanding angular jsAayush Shrestha
 
Staying Sane with Drupal NEPHP
Staying Sane with Drupal NEPHPStaying Sane with Drupal NEPHP
Staying Sane with Drupal NEPHPOscar Merida
 
Approaches to mobile site development
Approaches to mobile site developmentApproaches to mobile site development
Approaches to mobile site developmentErik Mitchell
 
MVC & SQL_In_1_Hour
MVC & SQL_In_1_HourMVC & SQL_In_1_Hour
MVC & SQL_In_1_HourDilip Patel
 
How to disassemble one monster app into an ecosystem of 30
How to disassemble one monster app into an ecosystem of 30How to disassemble one monster app into an ecosystem of 30
How to disassemble one monster app into an ecosystem of 30fiyuer
 
Little Opinions, Big Possibilities: The Tools and Patterns for Building Larg...
 Little Opinions, Big Possibilities: The Tools and Patterns for Building Larg... Little Opinions, Big Possibilities: The Tools and Patterns for Building Larg...
Little Opinions, Big Possibilities: The Tools and Patterns for Building Larg...Brian Mann
 

Similaire à WordPress hooks - WPLDN July 2013 Meetup (20)

AngularJS training - Day 1 - Basics: Why, What and basic features of AngularJS
AngularJS training - Day 1 - Basics: Why, What and basic features of AngularJSAngularJS training - Day 1 - Basics: Why, What and basic features of AngularJS
AngularJS training - Day 1 - Basics: Why, What and basic features of AngularJS
 
Angular presentation
Angular presentationAngular presentation
Angular presentation
 
WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...
 
Basics of AngularJS
Basics of AngularJSBasics of AngularJS
Basics of AngularJS
 
Introduction to jQuery
Introduction to jQueryIntroduction to jQuery
Introduction to jQuery
 
Application Security from the Inside - OWASP
Application Security from the Inside - OWASPApplication Security from the Inside - OWASP
Application Security from the Inside - OWASP
 
Google app engine by example
Google app engine by exampleGoogle app engine by example
Google app engine by example
 
Code igniter - A brief introduction
Code igniter - A brief introductionCode igniter - A brief introduction
Code igniter - A brief introduction
 
gDayX 2013 - Advanced AngularJS - Nicolas Embleton
gDayX 2013 - Advanced AngularJS - Nicolas EmbletongDayX 2013 - Advanced AngularJS - Nicolas Embleton
gDayX 2013 - Advanced AngularJS - Nicolas Embleton
 
important struts interview questions
important struts interview questionsimportant struts interview questions
important struts interview questions
 
Raybiztech Guide To Backbone Javascript Library
Raybiztech Guide To Backbone Javascript LibraryRaybiztech Guide To Backbone Javascript Library
Raybiztech Guide To Backbone Javascript Library
 
Quick Start: ActiveScaffold
Quick Start: ActiveScaffoldQuick Start: ActiveScaffold
Quick Start: ActiveScaffold
 
AngularJs Workshop SDP December 28th 2014
AngularJs Workshop SDP December 28th 2014AngularJs Workshop SDP December 28th 2014
AngularJs Workshop SDP December 28th 2014
 
Understanding angular js
Understanding angular jsUnderstanding angular js
Understanding angular js
 
Staying Sane with Drupal NEPHP
Staying Sane with Drupal NEPHPStaying Sane with Drupal NEPHP
Staying Sane with Drupal NEPHP
 
Approaches to mobile site development
Approaches to mobile site developmentApproaches to mobile site development
Approaches to mobile site development
 
MVC & SQL_In_1_Hour
MVC & SQL_In_1_HourMVC & SQL_In_1_Hour
MVC & SQL_In_1_Hour
 
Knolx session
Knolx sessionKnolx session
Knolx session
 
How to disassemble one monster app into an ecosystem of 30
How to disassemble one monster app into an ecosystem of 30How to disassemble one monster app into an ecosystem of 30
How to disassemble one monster app into an ecosystem of 30
 
Little Opinions, Big Possibilities: The Tools and Patterns for Building Larg...
 Little Opinions, Big Possibilities: The Tools and Patterns for Building Larg... Little Opinions, Big Possibilities: The Tools and Patterns for Building Larg...
Little Opinions, Big Possibilities: The Tools and Patterns for Building Larg...
 

Dernier

Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Bhuvaneswari Subramani
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Angeliki Cooney
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...apidays
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Zilliz
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 

Dernier (20)

Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 

WordPress hooks - WPLDN July 2013 Meetup

  • 1. WordPress Hooks Scott Cariss • @l3rady • Big Fish Ltd
  • 2. What I'm going to cover The WordPress page lifecycle. What are hooks and how do they fit into the page lifecycle? Why use hooks? What are actions What are filters Examples
  • 3. The WP page lifecycle "A page life cycle is nothing more than a combination of the events that take place from when a browser requests a page to when the server returns the page to the browser." Lets look at loading a single page in WP - Look at the requested page ID - Query the database - Get associated data (Categories, tags, images, etc) - Return all of the data to the browser
  • 4. The WP page lifecycle The template files and the calls to the API functions are then responsible for rendering, styling and position the data on the screen Sounds simple but this is just a simple page load. Think of more complex sites WP sites you have been on. You can now understand how intensive this particular process can be.
  • 5.
  • 6. The WP page lifecycle "While WordPress is running its series of queries and preparing to render data back to the browser, it’s looking at all of the custom hooks – that is, the actions and filters – that have been written and is passing data through those filters before returning data to the browser." At this point it's important to define what hooks are and look at the difference between action and filter hooks and how they play into this whole lifecycle
  • 7. What are hooks and why use them? They are the backbone of WordPress. They enable developers to "hook" into the WP lifecycle to change how it works without modifying the core code. Developers hook code is separate from WP core so that WP updates don't remove or destroy the developers code.
  • 8. Different functions for hooks Action Hooks - do_action() - add_action() - remove_action() - has_action() - do_action_ref_array() - did_action() - remove_all_actions() Filter Hooks - apply_filters() - add_filters() - remove_filters() - has_filter() - current_filter() - merge_filters() - remove_all_filters()
  • 9. Using action hooks do_action( 'hook_name', $arg1, $arg2, ... ); add_action( 'hook_name', 'your_function_name', [priority], [number_args_accepted] ); remove_action( 'hook_name', 'function_to_remove', [priority], [number_args_accepted] );
  • 10. Using filter hooks $value = apply_filters( 'hook_name', $value, $value2, ... ); add_filter( 'hook_name', 'your_filter_function', [priority], [number_args_accepted] ); remove_filter( 'hook_name', 'filter_function_to_remove', [priority], [number_args_accepted] );
  • 11. Examples Changing the excerpt length on the home page: function wpldn_excerpt( $length ) { if ( is_home() ) { return 20; } return $length; } add_filter( 'excerpt_length', 'wpldn_excerpt', 999 );
  • 12. Examples Adding Google Maps JS to contact us page function wpldn_maps_js() { if ( !is_page( 'contact-us' ) ) { return; } wp_enqueue_scripts( 'google_maps_api', '[PATH_TO_GOOGLE_MAP_API]', array(), false, true ); wp_enqueue_scripts( 'my_map_js', '[PATH_TO_YOUR_JS]', array( 'google_maps_api' ), false, true ); } add_action( 'wp_enqueue_scripts', 'wpldn_maps_js' );
  • 13. Examples Load a different theme template file at will function wpldn_any_file() { if( !is_page( "about-us" ) ) { return; } include( '/path/to/any/file.extension' ); exit(); } add_action( 'template_redirect', 'wpldn_any_file' );
  • 14. Examples Using pre_get_posts to change the main query function wpldn_change_query( $query ) { if( $query->is_home() && $query->is_main_query() ) { $query->set( 'posts_per_page', 20 ); } } add_action( 'pre_get_posts', 'wpldn_change_query' );
  • 15. Examples Remove that pesky capital_P_dangit function remove_filter( 'the_title', 'capital_P_dangit', 11 ); remove_filter( 'the_content', 'capital_P_dangit', 11 ); remove_filter( 'comment_text', 'capital_P_dangit', 31 ); * Check /wp-includes/default-filters.php to see what functions WP is applying to its own filters
  • 16. Resources WordPress Codex - Action Reference
  • 17. Resources Debug Bar plugin & Action Hooks add on
  • 18. Don't be scared! Rummage around in core code to find filters and hooks that are not documented. A good knowledge of PHP will help you to find and understand WordPress hooks. IDEs can help in navigating through WP core code.