SlideShare a Scribd company logo
1 of 28
Hooks
An Introduction to WordPress Development
5 Aug 2019
Edmund Chan
edmund@nerb.com.sg //
https://github.com/edmundcwm
Developer
Hooks
• What is a Hook?
– A place in the codebase for custom functions to be added or
change WP default behaviour
– Functions attached to the hook will run at specific juncture during
code processing
– Must-know concept in WP Development
– Very common way to interact with WP
Hooks
• Two kinds of hooks
– Action hook
– Filter hook
Hooks
• Action Hook
– Allows custom functions to be run at a specific point
– e.g, send email after publishing a post; add a notification bar
before the navigation section
– do_action()
– add_action()
– has_action()
– remove_action()
Hooks
• Filter Hook
– Allows data to be changed before it is used
– e.g, change WooCommerce ‘add to cart’ text; conditionally
append text to the end of the post content
– apply_filters()
– add_filter()
– has_filter()
– remove_filter()
Hooks
Key Differences
Actions Filters
Do something Change something
Execute functions Manipulate Data
Doesn’t need to return a value Must return a value
use do_action() and add_action() use apply_filters() and add_filter()
Hooks
Hooks
hook:
after_nav_menu
function:
nerb_new_action
Hooks
Hooks
• Why hooks?
– Recommended way to customize and extend WordPress
– Easier to debug
– Reduce reliability of external plugins
– Better understanding of how things work
– Allow for reusable custom functions
– Not mess with Core files
Hooks
Hooks
do_action( ‘woocommerce_thankyou’ , $order->get_id() );
Action hook
Tag/Hook name Args
Hooks
/woocommerce/templates/checkout/thankyou.php
Hooks
add_action( ‘woocommerce_thankyou’ , ‘nerb_thankyou_info’, 10, 1 );
Priority
Tag/Hook name Custom function Args
Hooks
add_action( ‘woocommerce_thankyou’ , ‘nerb_thankyou_info’, 10, 1 );
Hooks
add_action( ‘woocommerce_thankyou’ , ‘nerb_thankyou_info’, 10, 1 );
Hooks
/child-theme/functions.php or /plugin-name/plugin-name.php
Hooks
Hooks
apply_filters( ‘woocommerce_thankyou_order_received_text’ , ‘Thank you!’, $order );
Filter hook
Tag/Hook name Value
Args
Hooks
add_filter( ‘woocommerce_thankyou_order_received_text’ , ‘nerb_thankyou’, 10, 1 );
Priority
Tag/Hook name Custom function Args
Hooks
add_filter( ‘woocommerce_thankyou_order_received_text’ , ‘nerb_thankyou’ );
Hooks
/child-theme/functions.php or /plugin-name/plugin-name.php
Hooks
Hooks
Key Differences
Actions Filters
Do something Change something
Doesn’t need to return a value Must return a value
Execute functions Manipulate data
use do_action() and add_action() use apply_filters() and add_filter()
Hooks
Edmund Chan
edmund@nerb.com.sg //
https://github.com/edmundcwm
Developer

More Related Content

What's hot

CUST-10 Customizing the Upload File(s) dialog in Alfresco Share
CUST-10 Customizing the Upload File(s) dialog in Alfresco ShareCUST-10 Customizing the Upload File(s) dialog in Alfresco Share
CUST-10 Customizing the Upload File(s) dialog in Alfresco ShareAlfresco Software
 
Jose portillo dev con presentation 1138
Jose portillo   dev con presentation 1138Jose portillo   dev con presentation 1138
Jose portillo dev con presentation 1138Jose Portillo
 
EMC Documentum - xCP 2.x Troubleshooting
EMC Documentum - xCP 2.x TroubleshootingEMC Documentum - xCP 2.x Troubleshooting
EMC Documentum - xCP 2.x TroubleshootingHaytham Ghandour
 
Oak, the architecture of Apache Jackrabbit 3
Oak, the architecture of Apache Jackrabbit 3Oak, the architecture of Apache Jackrabbit 3
Oak, the architecture of Apache Jackrabbit 3Jukka Zitting
 
Flexbox and Grid Layout
Flexbox and Grid LayoutFlexbox and Grid Layout
Flexbox and Grid LayoutRachel Andrew
 
Alfresco DevCon 2019 - Alfresco Identity Services in Action
Alfresco DevCon 2019 - Alfresco Identity Services in ActionAlfresco DevCon 2019 - Alfresco Identity Services in Action
Alfresco DevCon 2019 - Alfresco Identity Services in ActionFrancesco Corti
 
Introduction to PowerShell
Introduction to PowerShellIntroduction to PowerShell
Introduction to PowerShellBoulos Dib
 
OAuth & OpenID Connect Deep Dive
OAuth & OpenID Connect Deep DiveOAuth & OpenID Connect Deep Dive
OAuth & OpenID Connect Deep DiveNordic APIs
 
How WordPress Themes Work
How WordPress Themes WorkHow WordPress Themes Work
How WordPress Themes WorkHandsOnWP.com
 
Introduction to HTML and CSS
Introduction to HTML and CSSIntroduction to HTML and CSS
Introduction to HTML and CSSdanpaquette
 
CSS3 notes
CSS3 notesCSS3 notes
CSS3 notesRex Wang
 
JCR, Sling or AEM? Which API should I use and when?
JCR, Sling or AEM? Which API should I use and when?JCR, Sling or AEM? Which API should I use and when?
JCR, Sling or AEM? Which API should I use and when?connectwebex
 
Introduction to JCR and Apache Jackrabbi
Introduction to JCR and Apache JackrabbiIntroduction to JCR and Apache Jackrabbi
Introduction to JCR and Apache JackrabbiJukka Zitting
 
Swagman - Converting Postman Collection to Swagger Build
Swagman - Converting Postman Collection to Swagger BuildSwagman - Converting Postman Collection to Swagger Build
Swagman - Converting Postman Collection to Swagger BuildAjinkya Dubey
 

What's hot (20)

CUST-10 Customizing the Upload File(s) dialog in Alfresco Share
CUST-10 Customizing the Upload File(s) dialog in Alfresco ShareCUST-10 Customizing the Upload File(s) dialog in Alfresco Share
CUST-10 Customizing the Upload File(s) dialog in Alfresco Share
 
Jose portillo dev con presentation 1138
Jose portillo   dev con presentation 1138Jose portillo   dev con presentation 1138
Jose portillo dev con presentation 1138
 
Sequelize
SequelizeSequelize
Sequelize
 
Git
GitGit
Git
 
EMC Documentum - xCP 2.x Troubleshooting
EMC Documentum - xCP 2.x TroubleshootingEMC Documentum - xCP 2.x Troubleshooting
EMC Documentum - xCP 2.x Troubleshooting
 
Upgrading to Alfresco 6
Upgrading to Alfresco 6Upgrading to Alfresco 6
Upgrading to Alfresco 6
 
Oak, the architecture of Apache Jackrabbit 3
Oak, the architecture of Apache Jackrabbit 3Oak, the architecture of Apache Jackrabbit 3
Oak, the architecture of Apache Jackrabbit 3
 
Flexbox and Grid Layout
Flexbox and Grid LayoutFlexbox and Grid Layout
Flexbox and Grid Layout
 
Alfresco DevCon 2019 - Alfresco Identity Services in Action
Alfresco DevCon 2019 - Alfresco Identity Services in ActionAlfresco DevCon 2019 - Alfresco Identity Services in Action
Alfresco DevCon 2019 - Alfresco Identity Services in Action
 
Introduction to PowerShell
Introduction to PowerShellIntroduction to PowerShell
Introduction to PowerShell
 
OAuth & OpenID Connect Deep Dive
OAuth & OpenID Connect Deep DiveOAuth & OpenID Connect Deep Dive
OAuth & OpenID Connect Deep Dive
 
Intro to Wordpress
Intro to WordpressIntro to Wordpress
Intro to Wordpress
 
Advanced Cascading Style Sheets
Advanced Cascading Style SheetsAdvanced Cascading Style Sheets
Advanced Cascading Style Sheets
 
How WordPress Themes Work
How WordPress Themes WorkHow WordPress Themes Work
How WordPress Themes Work
 
Introduction to HTML and CSS
Introduction to HTML and CSSIntroduction to HTML and CSS
Introduction to HTML and CSS
 
CSS3 notes
CSS3 notesCSS3 notes
CSS3 notes
 
JCR, Sling or AEM? Which API should I use and when?
JCR, Sling or AEM? Which API should I use and when?JCR, Sling or AEM? Which API should I use and when?
JCR, Sling or AEM? Which API should I use and when?
 
Introduction to JCR and Apache Jackrabbi
Introduction to JCR and Apache JackrabbiIntroduction to JCR and Apache Jackrabbi
Introduction to JCR and Apache Jackrabbi
 
Swagman - Converting Postman Collection to Swagger Build
Swagman - Converting Postman Collection to Swagger BuildSwagman - Converting Postman Collection to Swagger Build
Swagman - Converting Postman Collection to Swagger Build
 
Introducing CSS Grid
Introducing CSS GridIntroducing CSS Grid
Introducing CSS Grid
 

Similar to Introduction to WordPress Development - Hooks

Intro to WordPress Plugin Development
Intro to WordPress Plugin DevelopmentIntro to WordPress Plugin Development
Intro to WordPress Plugin DevelopmentBrad Williams
 
Creating Your First WordPress Plugin
Creating Your First WordPress PluginCreating Your First WordPress Plugin
Creating Your First WordPress PluginBrad Williams
 
Stop Hacking WordPress, Start Working with it - Charly Leetham - WordCamp Syd...
Stop Hacking WordPress, Start Working with it - Charly Leetham - WordCamp Syd...Stop Hacking WordPress, Start Working with it - Charly Leetham - WordCamp Syd...
Stop Hacking WordPress, Start Working with it - Charly Leetham - WordCamp Syd...WordCamp Sydney
 
WordPress Hooks Action & Filters
WordPress Hooks Action & FiltersWordPress Hooks Action & Filters
WordPress Hooks Action & FiltersNirav Mehta
 
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
 
Jumping Into WordPress Plugin Programming
Jumping Into WordPress Plugin ProgrammingJumping Into WordPress Plugin Programming
Jumping Into WordPress Plugin ProgrammingDougal Campbell
 
Hooking with WordPress
Hooking with WordPressHooking with WordPress
Hooking with WordPressEdward Caissie
 
Writing extensible Word press-plugins
Writing extensible Word press-pluginsWriting extensible Word press-plugins
Writing extensible Word press-pluginsAllenSnook
 
Hooked on WordPress: WordCamp Columbus
Hooked on WordPress: WordCamp ColumbusHooked on WordPress: WordCamp Columbus
Hooked on WordPress: WordCamp ColumbusShawn Hooper
 
How Not to Build a WordPress Plugin
How Not to Build a WordPress PluginHow Not to Build a WordPress Plugin
How Not to Build a WordPress PluginWill Norris
 
Hardcore URL Routing for WordPress - WordCamp Atlanta 2014 (PPT)
Hardcore URL Routing for WordPress - WordCamp Atlanta 2014 (PPT)Hardcore URL Routing for WordPress - WordCamp Atlanta 2014 (PPT)
Hardcore URL Routing for WordPress - WordCamp Atlanta 2014 (PPT)Mike Schinkel
 
WordPress hooks - WPLDN July 2013 Meetup
WordPress hooks - WPLDN July 2013 MeetupWordPress hooks - WPLDN July 2013 Meetup
WordPress hooks - WPLDN July 2013 Meetupl3rady
 
Simple Contact Us Plugin Development
Simple Contact Us Plugin DevelopmentSimple Contact Us Plugin Development
Simple Contact Us Plugin Developmentwpnepal
 
The Way to Theme Enlightenment
The Way to Theme EnlightenmentThe Way to Theme Enlightenment
The Way to Theme EnlightenmentAmanda Giles
 
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)arcware
 
Plug in development
Plug in developmentPlug in development
Plug in developmentLucky Ali
 
WordPress Plugin development
WordPress Plugin developmentWordPress Plugin development
WordPress Plugin developmentMostafa Soufi
 
Hooking with WordPress by Rahul Prajapati - COEP FOSSMeet March 2019
Hooking with WordPress by Rahul Prajapati - COEP FOSSMeet March 2019Hooking with WordPress by Rahul Prajapati - COEP FOSSMeet March 2019
Hooking with WordPress by Rahul Prajapati - COEP FOSSMeet March 2019rtCamp
 

Similar to Introduction to WordPress Development - Hooks (20)

Intro to WordPress Plugin Development
Intro to WordPress Plugin DevelopmentIntro to WordPress Plugin Development
Intro to WordPress Plugin Development
 
Rebrand WordPress Admin
Rebrand WordPress AdminRebrand WordPress Admin
Rebrand WordPress Admin
 
Creating Your First WordPress Plugin
Creating Your First WordPress PluginCreating Your First WordPress Plugin
Creating Your First WordPress Plugin
 
Stop Hacking WordPress, Start Working with it - Charly Leetham - WordCamp Syd...
Stop Hacking WordPress, Start Working with it - Charly Leetham - WordCamp Syd...Stop Hacking WordPress, Start Working with it - Charly Leetham - WordCamp Syd...
Stop Hacking WordPress, Start Working with it - Charly Leetham - WordCamp Syd...
 
WordPress Hooks Action & Filters
WordPress Hooks Action & FiltersWordPress Hooks Action & Filters
WordPress Hooks Action & Filters
 
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)
 
Jumping Into WordPress Plugin Programming
Jumping Into WordPress Plugin ProgrammingJumping Into WordPress Plugin Programming
Jumping Into WordPress Plugin Programming
 
Hooking with WordPress
Hooking with WordPressHooking with WordPress
Hooking with WordPress
 
Writing extensible Word press-plugins
Writing extensible Word press-pluginsWriting extensible Word press-plugins
Writing extensible Word press-plugins
 
Hooked on WordPress: WordCamp Columbus
Hooked on WordPress: WordCamp ColumbusHooked on WordPress: WordCamp Columbus
Hooked on WordPress: WordCamp Columbus
 
How Not to Build a WordPress Plugin
How Not to Build a WordPress PluginHow Not to Build a WordPress Plugin
How Not to Build a WordPress Plugin
 
Hardcore URL Routing for WordPress - WordCamp Atlanta 2014 (PPT)
Hardcore URL Routing for WordPress - WordCamp Atlanta 2014 (PPT)Hardcore URL Routing for WordPress - WordCamp Atlanta 2014 (PPT)
Hardcore URL Routing for WordPress - WordCamp Atlanta 2014 (PPT)
 
WordPress hooks - WPLDN July 2013 Meetup
WordPress hooks - WPLDN July 2013 MeetupWordPress hooks - WPLDN July 2013 Meetup
WordPress hooks - WPLDN July 2013 Meetup
 
Simple Contact Us Plugin Development
Simple Contact Us Plugin DevelopmentSimple Contact Us Plugin Development
Simple Contact Us Plugin Development
 
The Way to Theme Enlightenment
The Way to Theme EnlightenmentThe Way to Theme Enlightenment
The Way to Theme Enlightenment
 
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
 
Plug in development
Plug in developmentPlug in development
Plug in development
 
WordPress Plugin development
WordPress Plugin developmentWordPress Plugin development
WordPress Plugin development
 
Hooking with WordPress by Rahul Prajapati - COEP FOSSMeet March 2019
Hooking with WordPress by Rahul Prajapati - COEP FOSSMeet March 2019Hooking with WordPress by Rahul Prajapati - COEP FOSSMeet March 2019
Hooking with WordPress by Rahul Prajapati - COEP FOSSMeet March 2019
 

Recently uploaded

Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Clustering techniques data mining book ....
Clustering techniques data mining book ....Clustering techniques data mining book ....
Clustering techniques data mining book ....ShaimaaMohamedGalal
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 

Recently uploaded (20)

Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Clustering techniques data mining book ....
Clustering techniques data mining book ....Clustering techniques data mining book ....
Clustering techniques data mining book ....
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 

Introduction to WordPress Development - Hooks

Editor's Notes

  1. One of the most important concepts to understand when starting out as a WP Developer because it’s one of the most common ways to customise and extend WP, especially when you are working with theme or plugin development. You do not want to modify Core WP, themes or plugins files directly. It allows developers to add their own functions at specific juncture during code processing.
  2. When an action hook runs, all functions connected to it will be executed as well. For e.g., there is an action hook in WP codebase that is placed after the publishing of a post. With that, we can create our own functions and attached it to that hook so that we can perform actions after a post is published
  3. When using the filter hook, you are given access to the original data. So after you are done with the modifications, you have to return the data back or there will be an error. Depending on user roles, we could use a filter and conditionally append text to the end of the blog post content. For example, for non-logged in users, we can at the ‘sign up or login’ text and for logged-in users, we can show the ‘logout’ text.
  4. Imagine WordPress as a large factory that produces web pages. Not all the work is being done inside the factory; they hire contractors from outside for certain tasks. But these contractors do not have a permanent place in the factory and they are stuck outside until WordPress, the factory, invites them in. And this factory uses giant hooks to ferry contractors in (show pic). Now, this factory has two types of hooks - action and filter.
  5. Each hook is labeled with a name that corresponds to a specific process. So maybe during the building of the ‘navigation’ section of the site, WordPress sends out one hook called “after_nav_menu” right before the process completes and invites these contractors to come in and perform whatever they want. So if a contractor wants to be part of the process, he can ride that hook and get transported to the proper part of the factory/site. Two types of hooks - actions and filters The action hooks bring contractors to a specific part of the factory and they can do whatever they want after that. They don’t have to report to anyone. But for contractors riding the filter hooks, they will be part of the factory full-time workers’ process. They’re given some information to handle and after modifications, have to return it back to the full-time worker. For e.g., if you are given a title of a post, you have to return it back after your modifications or the whole process will break. In WP terms, all these external contractors are your custom functions from either plugins or inside your theme’s functions.php file. They rely on hooks to work.
  6. Other methods to customise WP include using overriding templates in your child theme but using hooks should be the first method to look into. Every hook is in-charge of a specific process. For e.g., if your post title or content is not displaying correctly, then there is probably a filter used to manipulate that data. Or if there is some weird image appearing after the footer, then an action hook is probably used to insert it there. So with that understanding, u will know where to start and what to look out for when debugging How many of you, when looking to make some changes to your site, the first thing you think of, or ask in a Facebook group is the question “is there a plugin that does this…?”. And then you will receive responses like, “… yes there is this premium WooCommerce add-on plugin that contains a suite of options for you to edit”. But maybe you are only looking to change the text of a button or add some shipping details below your product description? These can easily be achieved by using hooks and a few lines of code. So as developers, I think it’s a good habit to start asking “is there a hook for this…” instead of “is there a plugin…”. Which brings me to my next point. When figuring out how certain hooks work, you are bound to look up the documentations or even the source code. And by doing so, you will develop a strong understanding of how that part of the program works, why is the hook placed there, how is it being used by other functions. All these are valuable knowledge and will help you a lot when u are developing your own plugins or themes. When using hooks, u are likely going to put your custom functions inside a plugin or in your child theme’s functions.php. Either way, u will be able to easily use that plugin or code snippet in other sites. For e.g., I created a plugin that allows me to replace the WP logo in the wp-admin login screen. So all I need to do is to upload that plugin to all our projects and change the logo accordingly. All changes will be upgrade proof because u are not changing core files
  7. Most important part of the action hook is the tag/hook name. This is the name that your custom functions will get attached to (remember the analogy?) Some hooks provide arguments that you can use in your custom functions. It’s optional to use them in your custom functions so it depends on what u are trying to achieve
  8. What this code does is it checks if the order is successful. If it is, the order details table will be generated in the thank you page. If the order fails, then it will just show some error message. After this conditional check, it creates two hooks. Since the hooks are placed after the conditional statement, your custom function will be run below the error message or the order details table, depending on the status of the order. With this, if client wants u to add some additional information in this page, u will know which hook to use.
  9. To use the hook, we will utilise the add_action() function to attach our custom function to the hook. The first parameter is the tag name; it must correspond to the name of the hook you want to hook into second parameter is the name of your custom function third parameter is the priority. If multiple custom functions are attached to the same hook, the function with the lowest priority will be run first. So if i want to attach an image above the custom thank you message, i will use the same code, with a different function name and give it a priority lower than 10. Default value is 10 The last parameter is the number of arguments we want to use in our custom function. If the hook provides two arguments, and we want to use both, then we will set this number to 2. The default value is 1 So essentially what this code does is tell WP to attach our custom function named ‘nerb_thankyou_info’ to the ‘woocommerce_thankyou’ hook, give it a priority of 10 and utilise one argument that the woocommerce_thankyou hook provides.
  10. This is how our custom function will look like. What this does is it checks if the order id is 888, if it is it will display the congrats message. If not, it will display the better luck next time message
  11. The final snippet will look like this. and it can be placed either in your child-theme’s functions.php or u can make it into a plugin Since the default value for the $priority param is 10 and the $args is 1, we can leave them out in the add_action() if we are planning to change the priority and include more than 1 argument. So the add_action() function will only contain the name of the hook and the name of our custom function
  12. And this is how it will look like in the thank you page. Since the order number is not 888, the ‘better luck next time’ message is being displayed
  13. Most important part of the action hook is the tag/hook name. This is the name that your custom functions will get attached to (remember the analogy?) Like the action hook, the first parameter is the tag/hook name The ‘value’ is the data that the filter hook allows us to change So what this filter hook does is allow us to change the ‘Thank you’ message that appears in thank you page when the order is successfully made
  14. To use the filter, we will utilise the add_filter() function to attach our custom function to the hook. The first parameter is the tag name; it must correspond to the name of the hook you want to hook into second parameter is the name of your custom function third parameter is the priority. If multiple custom functions are attached to the same hook, the function with the lowest priority will be run first. So if i want to attach an image above the custom thank you message, i will use the same code, with a different function name and give it a priority lower than 10. Default value is 10 The last parameter is the number of arguments we want to use in our custom function. If the hook provides two arguments, and we want to use both, then we will set this number to 2. The default value is 1 So essentially what this code does is tell WP to attach our custom function named ‘nerb_thankyou’ to the ‘woocommerce_thankyou_order_received_text’ filter hook, give it a priority of 10 and utilise one argument that the woocommerce_thankyou hook provides.
  15. This is how our custom function will look like.
  16. Take note of the return statement. When using a filter, it is a must to return a value regardless whether it is modified or not.
  17. When using the filter hook, you are given access to the original data. So after you are done with the modifications, you have to return the data back or there will be an error. Depending on user roles, we could use a filter and conditionally append text to the end of the blog post content. For example, for non-logged in users, we can at the ‘sign up or login’ text and for logged-in users, we can show the ‘logout’ text.