SlideShare a Scribd company logo
1 of 34
Hooking with WordPress

  WordPress Plugins 103
WordPress Hooks



WordPress Hooks
• Actions
• Filters
WordPress Actions



ACTIONS ARE THE HOOKS THAT THE
WORDPRESS CORE LAUNCHES AT SPECIFIC
POINTS DURING EXECUTION, OR WHEN
SPECIFIC EVENTS OCCUR. YOUR PLUGIN CAN
SPECIFY THAT ONE OR MORE OF ITS PHP
FUNCTIONS ARE EXECUTED AT THESE POINTS,
        ACTION API.
USING THE
~~ WORDPRESS CODEX
WordPress Actions



WordPress Hooks
Actions
• http://codex.wordpress.org/Plugin_API/Action_Reference
WordPress Actions



Basic Functions
• do_action()
  – http://codex.wordpress.org/Function_Reference/do_action

• add_action()
  – http://codex.wordpress.org/Function_Reference/add_action

• remove_action()
  – http://codex.wordpress.org/Function_Reference/remove_action
WordPress Actions



Basic Functions
• do_action()
  – http://codex.wordpress.org/Function_Reference/do_action


This is where the action fires in the code,
and where your hook will attach itself.

Example: do_action( „my_action‟ );
WordPress Actions



Basic Functions
• add_action()
   – http://codex.wordpress.org/Function_Reference/add_action


This is what you use to add your action (or
functionality) to the do_action() sequence.

Example:
add_action( „my_action‟, „my_functionality‟, 10 );
WordPress Actions



Basic Functions
• remove_action()
  – http://codex.wordpress.org/Function_Reference/remove_action


This is what you use to remove a specific
action that has been added … and it
requires a call to an action, too?!
WordPress Actions



Basic Functions
• remove_action()


Example:
function remove_my_action(){
  remove_action( „my_action‟, „my_functionality‟, 10 );
}
add_action( „init‟, „remove_my_action‟ );

Note: the priority being used; and, adding to
the init action.
WordPress Filters



FILTERS ARE THE HOOKS THAT WORDPRESS
LAUNCHES TO MODIFY TEXT OF VARIOUS TYPES
BEFORE ADDING IT TO THE DATABASE OR
SENDING IT TO THE BROWSER SCREEN.   YOUR
PLUGIN CAN SPECIFY THAT ONE OR MORE OF
ITSPHP FUNCTIONS IS EXECUTED TO MODIFY
SPECIFIC TYPES OF TEXT AT THESE TIMES,
USING THE FILTER API.
~~ WORDPRESS CODEX
WordPress Filters



WordPress Hooks
Filters
• http://codex.wordpress.org/Plugin_API/Filter_Reference
WordPress Filters



Basic Functions
• apply_filters
  – http://codex.wordpress.org/Function_Reference/apply_filters

• add_filter
  – http://codex.wordpress.org/Function_Reference/add_filter

• remove_filter
  – http://codex.wordpress.org/Function_Reference/remove_filter
WordPress Filters



Basic Functions
• apply_filters
   – http://codex.wordpress.org/Function_Reference/apply_filters


This is where the filter fires in the code, and
where your hook will attach itself.

Example:
apply_filters( „my_filter‟, „my_original_value‟ );
WordPress Filters



Basic Functions
• add_filter
   – http://codex.wordpress.org/Function_Reference/add_filter


This is what you use to add your filter (or
new value) to the apply_filters() item.

Example:
add_filter( „my_filter‟, „my_new_value‟ );
WordPress Filters



Basic Functions
• remove_filter
  – http://codex.wordpress.org/Function_Reference/remove_filter


This is what you use to remove a specific
filter that has been used … and it is best to
follow the remove_action() example as the
safest method to implement.
WordPress Filters



Basic functions
• remove_filter()
Example:
function remove_my_filter(){
  remove_filter( „my_filter‟, „my_new_value‟, 20 );
}
add_action( „init‟, „remove_my_filter‟ );
Note: the priority being used; and, adding to
the init action.
Hooking with WordPress



1. Open your local test environment
2. Make sure the BNS Bio plugin is installed
   and activated
3. Add the [bns_bio] shortcode to a post
4. Open your text editor
5. Navigate to the BNS Bio folder
6. Ready?
BNS Bio Plugin



The BNS Bio plugin adds a simple
shortcode to your WordPress installation
that outputs the author details of the post it
is added to.
The plugin uses a different approach to
options by utilizing “extension” plugins
versus an options panel. This is primarily for
the purposes of using BNS Bio as example
code.
BNS Bio Structures



• Uses the Class PHP structure
  – Uses a simple constuctor method
    • __construct()
  – Enqueues its own stylesheet with an optional
    call to a custom stylesheet
    • Scripts_and_Styles()
  – Collect the author details which it passes back
    to the shortcode `bns_bio`
    • author_block()
BNS Bio __construct()



The constructor method of the BNS Bio
class calls an action to add the
Scripts_and_Styles functionality …

/** Add Scripts and Styles */
add_action( 'wp_enqueue_scripts', array( $this, 'Scripts_and_Styles' ) );


… note the difference from the earlier
example.
WordPress Actions



Basic Functions
• add_action()
   – http://codex.wordpress.org/Function_Reference/add_action


Example:
add_action( „my_action‟, „my_functionality‟, 10 );
Using in a Class example:
add_action( „my_action‟, array( $this, „my_functionality‟ ) );
BNS Bio author_block()



• This is the main code of the plugin.
• There are many instances of hooks
  (actions and filters) available to work with.

Examples:
do_action( 'bns_bio_before_all' );
apply_filters( 'bns_bio_author_url_text', …
apply_filters( 'bns_bio_author_desc', …
BNS Bio Box



This is an “extension plugin” for BNS Bio.

It adds a rounded corner border, or box,
around the BNS Bio output by hooking an
additional CSS container into the
„bns_bio_before_all‟ and „bns_bio_after_all‟
actions; then adding the appropriate style
properties to create the look.
BNS Bio Box Code



The essentials:
• Define the functions to be used
• Hook them into the appropriate action
  – add_action( 'bns_bio_before_all', 'bns_bio_open_box' );
  – add_action( 'bns_bio_after_all', 'bns_bio_close_box' );

• Add the stylesheet (using the common
  enqueue method; and another add_action
  call)
BNS Bio List



This is another “extension plugin” for BNS
Bio that changes the output to an unordered
list by hooking into the available actions and
adding the appropriate HTML („ul‟ and „li‟)
elements.

It also includes its own enqueued stylesheet
and calls an optional custom stylesheet if
available.
BNS Bio List Code



The essentials:
•   Add the „ul‟ container:
     –   add_action( 'bns_bio_before_all', function(){ echo '<ul class="bns-bio-list">'; }, 20 );
     –   add_action( 'bns_bio_after_all', function(){ echo '</ul><!-- .bns-bio-list -->'; }, 9 );
•   Add the „li‟ containers:
     – add_action( 'bns_bio_before_author_name', 'bns_bio_list_item' );
     – add_action( 'bns_bio_before_author_url', 'bns_bio_list_item' );
     – add_action( 'bns_bio_before_author_email', 'bns_bio_list_item' );
     – add_action( 'bns_bio_before_author_desc', 'bns_bio_list_item' );


NB: I used two different methods for adding the
functionality: a call to a predefined function
(„bns_bio_list_item‟) and anonymous functions1
BNS Bio List Notes



1. The anonymous functions were used for
example purposes and may not work with
versions of PHP earlier than 5.3.0 where
they were introduced.
BNS Bio Hide



This is another “extension plugin” for BNS
Bio that changes the output by hiding the
author email details.
There are actually three items that need to
be addressed to hide the email line in the
output:
• What displays before the email
• What displays as the author (pre) email text
• What displays as the author email
WordPress Plugins 103



The essentials:
•   remove_action( 'bns_bio_before_author_email', 'bns_bio_list_item' );
     – (as applied to the init hook – see original „remove_action‟ example)
•   add_filter( 'bns_bio_author_email_text', '__return_null', 100 );
•   add_filter( 'bns_bio_author_email', '__return_null', 100 );


Notes:
•   The remove_action priority is left as the default as the „bns_bio_list_item‟
    function was added at the default priority (these priorities must match).
•   Filters are essentially a “last one standing” mechanism; and in this case, the
    WordPress „__return_null‟ function was called at a very late priority to help
    insure it is last to run and therefore the final output.
WordPress Plugins 103



    Hooking with WordPress

     Questions and Answers
Credits



     Presented by Edward Caissie
for the WordPress GTA Meetup group
        on November 19, 2012.

            Contact Details:
     Email: edward.caissie@gmail.com
      URL: http://edwardcaissie.com
Quick Resources



Codex References - Actions:
http://codex.wordpress.org/Plugin_API/Action_Reference
http://codex.wordpress.org/Function_Reference/do_action
http://codex.wordpress.org/Function_Reference/add_action
http://codex.wordpress.org/Function_Reference/remove_filter
Codex References – Filters:
http://codex.wordpress.org/Plugin_API/Filter_Reference
http://codex.wordpress.org/Function_Reference/apply_filters
http://codex.wordpress.org/Function_Reference/add_filter
http://codex.wordpress.org/Function_Reference/remove_filter
WordPress Power Point Template Credits




m62 visualcommunications:
http://www.m62.net/powerpoint-templates/technology-templates/wordpress-powerpoint-template/
WordPress Plugins 103: Hooking with WordPress




            Thank You.

More Related Content

What's hot

Getting Started with WP-CLI, a tool to automate your life
Getting Started with WP-CLI, a tool to automate your lifeGetting Started with WP-CLI, a tool to automate your life
Getting Started with WP-CLI, a tool to automate your lifeAJ Morris
 
Django REST Framework
Django REST FrameworkDjango REST Framework
Django REST FrameworkLoad Impact
 
Django for mobile applications
Django for mobile applicationsDjango for mobile applications
Django for mobile applicationsHassan Abid
 
Transforming WordPress Search and Query Performance with Elasticsearch
Transforming WordPress Search and Query Performance with Elasticsearch Transforming WordPress Search and Query Performance with Elasticsearch
Transforming WordPress Search and Query Performance with Elasticsearch Taylor Lovett
 
Assetic (Symfony Live Paris)
Assetic (Symfony Live Paris)Assetic (Symfony Live Paris)
Assetic (Symfony Live Paris)Kris Wallsmith
 
The Best (and Worst) of Django
The Best (and Worst) of DjangoThe Best (and Worst) of Django
The Best (and Worst) of DjangoJacob Kaplan-Moss
 
The Coolest Symfony Components you’ve never heard of - DrupalCon 2017
The Coolest Symfony Components you’ve never heard of - DrupalCon 2017The Coolest Symfony Components you’ve never heard of - DrupalCon 2017
The Coolest Symfony Components you’ve never heard of - DrupalCon 2017Ryan Weaver
 
PesterSec: Using Pester & ScriptAnalyzer to Detect Obfuscated PowerShell
PesterSec: Using Pester & ScriptAnalyzer to Detect Obfuscated PowerShellPesterSec: Using Pester & ScriptAnalyzer to Detect Obfuscated PowerShell
PesterSec: Using Pester & ScriptAnalyzer to Detect Obfuscated PowerShellDaniel Bohannon
 
Drupal 8 + Elasticsearch + Docker
Drupal 8 + Elasticsearch + DockerDrupal 8 + Elasticsearch + Docker
Drupal 8 + Elasticsearch + DockerRoald Umandal
 
Developing iOS REST Applications
Developing iOS REST ApplicationsDeveloping iOS REST Applications
Developing iOS REST Applicationslmrei
 
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
 
ACL in CodeIgniter
ACL in CodeIgniterACL in CodeIgniter
ACL in CodeIgnitermirahman
 
WordPress hooks - WPLDN July 2013 Meetup
WordPress hooks - WPLDN July 2013 MeetupWordPress hooks - WPLDN July 2013 Meetup
WordPress hooks - WPLDN July 2013 Meetupl3rady
 
Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 Minutes
Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 MinutesDjangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 Minutes
Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 MinutesNina Zakharenko
 
Modernizing WordPress Search with Elasticsearch
Modernizing WordPress Search with ElasticsearchModernizing WordPress Search with Elasticsearch
Modernizing WordPress Search with ElasticsearchTaylor Lovett
 
Ako prepojiť aplikáciu s Elasticsearch
Ako prepojiť aplikáciu s ElasticsearchAko prepojiť aplikáciu s Elasticsearch
Ako prepojiť aplikáciu s Elasticsearchbart-sk
 
Introduction to ElasticSearch
Introduction to ElasticSearchIntroduction to ElasticSearch
Introduction to ElasticSearchSimobo
 
(DEV305) Building Apps with the AWS SDK for PHP | AWS re:Invent 2014
(DEV305) Building Apps with the AWS SDK for PHP | AWS re:Invent 2014(DEV305) Building Apps with the AWS SDK for PHP | AWS re:Invent 2014
(DEV305) Building Apps with the AWS SDK for PHP | AWS re:Invent 2014Amazon Web Services
 
Getting to The Loop - London Wordpress Meetup July 28th
Getting to The Loop - London Wordpress Meetup  July 28thGetting to The Loop - London Wordpress Meetup  July 28th
Getting to The Loop - London Wordpress Meetup July 28thChris Adams
 

What's hot (20)

Getting Started with WP-CLI, a tool to automate your life
Getting Started with WP-CLI, a tool to automate your lifeGetting Started with WP-CLI, a tool to automate your life
Getting Started with WP-CLI, a tool to automate your life
 
Django REST Framework
Django REST FrameworkDjango REST Framework
Django REST Framework
 
Django for mobile applications
Django for mobile applicationsDjango for mobile applications
Django for mobile applications
 
Transforming WordPress Search and Query Performance with Elasticsearch
Transforming WordPress Search and Query Performance with Elasticsearch Transforming WordPress Search and Query Performance with Elasticsearch
Transforming WordPress Search and Query Performance with Elasticsearch
 
Assetic (Symfony Live Paris)
Assetic (Symfony Live Paris)Assetic (Symfony Live Paris)
Assetic (Symfony Live Paris)
 
The Best (and Worst) of Django
The Best (and Worst) of DjangoThe Best (and Worst) of Django
The Best (and Worst) of Django
 
The Coolest Symfony Components you’ve never heard of - DrupalCon 2017
The Coolest Symfony Components you’ve never heard of - DrupalCon 2017The Coolest Symfony Components you’ve never heard of - DrupalCon 2017
The Coolest Symfony Components you’ve never heard of - DrupalCon 2017
 
PesterSec: Using Pester & ScriptAnalyzer to Detect Obfuscated PowerShell
PesterSec: Using Pester & ScriptAnalyzer to Detect Obfuscated PowerShellPesterSec: Using Pester & ScriptAnalyzer to Detect Obfuscated PowerShell
PesterSec: Using Pester & ScriptAnalyzer to Detect Obfuscated PowerShell
 
Drupal 8 + Elasticsearch + Docker
Drupal 8 + Elasticsearch + DockerDrupal 8 + Elasticsearch + Docker
Drupal 8 + Elasticsearch + Docker
 
Developing iOS REST Applications
Developing iOS REST ApplicationsDeveloping iOS REST Applications
Developing iOS REST Applications
 
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
 
Django
DjangoDjango
Django
 
ACL in CodeIgniter
ACL in CodeIgniterACL in CodeIgniter
ACL in CodeIgniter
 
WordPress hooks - WPLDN July 2013 Meetup
WordPress hooks - WPLDN July 2013 MeetupWordPress hooks - WPLDN July 2013 Meetup
WordPress hooks - WPLDN July 2013 Meetup
 
Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 Minutes
Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 MinutesDjangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 Minutes
Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 Minutes
 
Modernizing WordPress Search with Elasticsearch
Modernizing WordPress Search with ElasticsearchModernizing WordPress Search with Elasticsearch
Modernizing WordPress Search with Elasticsearch
 
Ako prepojiť aplikáciu s Elasticsearch
Ako prepojiť aplikáciu s ElasticsearchAko prepojiť aplikáciu s Elasticsearch
Ako prepojiť aplikáciu s Elasticsearch
 
Introduction to ElasticSearch
Introduction to ElasticSearchIntroduction to ElasticSearch
Introduction to ElasticSearch
 
(DEV305) Building Apps with the AWS SDK for PHP | AWS re:Invent 2014
(DEV305) Building Apps with the AWS SDK for PHP | AWS re:Invent 2014(DEV305) Building Apps with the AWS SDK for PHP | AWS re:Invent 2014
(DEV305) Building Apps with the AWS SDK for PHP | AWS re:Invent 2014
 
Getting to The Loop - London Wordpress Meetup July 28th
Getting to The Loop - London Wordpress Meetup  July 28thGetting to The Loop - London Wordpress Meetup  July 28th
Getting to The Loop - London Wordpress Meetup July 28th
 

Similar to Hooking with WordPress

Creating Your First WordPress Plugin
Creating Your First WordPress PluginCreating Your First WordPress Plugin
Creating Your First WordPress PluginBrad Williams
 
Intro to WordPress Plugin Development
Intro to WordPress Plugin DevelopmentIntro to WordPress Plugin Development
Intro to WordPress Plugin DevelopmentBrad Williams
 
Write your first WordPress plugin
Write your first WordPress pluginWrite your first WordPress plugin
Write your first WordPress pluginAnthony Montalbano
 
Getting started with WordPress development
Getting started with WordPress developmentGetting started with WordPress development
Getting started with WordPress developmentSteve Mortiboy
 
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 Plugins
WordPress PluginsWordPress Plugins
WordPress Pluginsrandyhoyt
 
Plug in development
Plug in developmentPlug in development
Plug in developmentLucky Ali
 
WordPress Plugin Development For Beginners
WordPress Plugin Development For BeginnersWordPress Plugin Development For Beginners
WordPress Plugin Development For Beginnersjohnpbloch
 
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
 
Writing your own WordPress themes and plugins
Writing your own WordPress themes and pluginsWriting your own WordPress themes and plugins
Writing your own WordPress themes and pluginsStephanie Wells
 
Word press Plugins by WordPress Experts
Word press Plugins by WordPress ExpertsWord press Plugins by WordPress Experts
Word press Plugins by WordPress ExpertsYameen Khan
 
Introduction to WordPress Development - Hooks
Introduction to WordPress Development - HooksIntroduction to WordPress Development - Hooks
Introduction to WordPress Development - HooksEdmund Chan
 
WordPress Developers Israel Meetup #1
WordPress Developers Israel Meetup #1WordPress Developers Israel Meetup #1
WordPress Developers Israel Meetup #1Yoav Farhi
 
The Way to Theme Enlightenment
The Way to Theme EnlightenmentThe Way to Theme Enlightenment
The Way to Theme EnlightenmentAmanda Giles
 
Plugin development demystified 2017
Plugin development demystified 2017Plugin development demystified 2017
Plugin development demystified 2017ylefebvre
 
Bending word press to your will
Bending word press to your willBending word press to your will
Bending word press to your willTom Jenkins
 
Step by step guide for creating wordpress plugin
Step by step guide for creating wordpress pluginStep by step guide for creating wordpress plugin
Step by step guide for creating wordpress pluginMainak Goswami
 

Similar to Hooking with WordPress (20)

Creating Your First WordPress Plugin
Creating Your First WordPress PluginCreating Your First WordPress Plugin
Creating Your First WordPress Plugin
 
Intro to WordPress Plugin Development
Intro to WordPress Plugin DevelopmentIntro to WordPress Plugin Development
Intro to WordPress Plugin Development
 
Write your first WordPress plugin
Write your first WordPress pluginWrite your first WordPress plugin
Write your first WordPress plugin
 
Getting started with WordPress development
Getting started with WordPress developmentGetting started with WordPress development
Getting started with WordPress development
 
How To Write a WordPress Plugin
How To Write a WordPress PluginHow To Write a WordPress Plugin
How To Write a WordPress Plugin
 
Extending WordPress
Extending WordPressExtending WordPress
Extending WordPress
 
WordPress Plugins
WordPress PluginsWordPress Plugins
WordPress Plugins
 
Plug in development
Plug in developmentPlug in development
Plug in development
 
WordPress Plugin Development For Beginners
WordPress Plugin Development For BeginnersWordPress Plugin Development For Beginners
WordPress Plugin Development For Beginners
 
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
 
Writing your own WordPress themes and plugins
Writing your own WordPress themes and pluginsWriting your own WordPress themes and plugins
Writing your own WordPress themes and plugins
 
Word press Plugins by WordPress Experts
Word press Plugins by WordPress ExpertsWord press Plugins by WordPress Experts
Word press Plugins by WordPress Experts
 
WordPress Plugins
WordPress PluginsWordPress Plugins
WordPress Plugins
 
Introduction to WordPress Development - Hooks
Introduction to WordPress Development - HooksIntroduction to WordPress Development - Hooks
Introduction to WordPress Development - Hooks
 
WordPress Developers Israel Meetup #1
WordPress Developers Israel Meetup #1WordPress Developers Israel Meetup #1
WordPress Developers Israel Meetup #1
 
The Way to Theme Enlightenment
The Way to Theme EnlightenmentThe Way to Theme Enlightenment
The Way to Theme Enlightenment
 
Plugin development demystified 2017
Plugin development demystified 2017Plugin development demystified 2017
Plugin development demystified 2017
 
Bending word press to your will
Bending word press to your willBending word press to your will
Bending word press to your will
 
Step by step guide for creating wordpress plugin
Step by step guide for creating wordpress pluginStep by step guide for creating wordpress plugin
Step by step guide for creating wordpress plugin
 
Rebrand WordPress Admin
Rebrand WordPress AdminRebrand WordPress Admin
Rebrand WordPress Admin
 

Recently uploaded

My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
[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
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
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
 
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
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
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
 
The Evolution of Money: Digital Transformation and CBDCs in Central Banking
The Evolution of Money: Digital Transformation and CBDCs in Central BankingThe Evolution of Money: Digital Transformation and CBDCs in Central Banking
The Evolution of Money: Digital Transformation and CBDCs in Central BankingSelcen Ozturkcan
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
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
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
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
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
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
 

Recently uploaded (20)

My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
[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
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
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...
 
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
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
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
 
The Evolution of Money: Digital Transformation and CBDCs in Central Banking
The Evolution of Money: Digital Transformation and CBDCs in Central BankingThe Evolution of Money: Digital Transformation and CBDCs in Central Banking
The Evolution of Money: Digital Transformation and CBDCs in Central Banking
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
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 ...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 

Hooking with WordPress

  • 1. Hooking with WordPress WordPress Plugins 103
  • 3. WordPress Actions ACTIONS ARE THE HOOKS THAT THE WORDPRESS CORE LAUNCHES AT SPECIFIC POINTS DURING EXECUTION, OR WHEN SPECIFIC EVENTS OCCUR. YOUR PLUGIN CAN SPECIFY THAT ONE OR MORE OF ITS PHP FUNCTIONS ARE EXECUTED AT THESE POINTS, ACTION API. USING THE ~~ WORDPRESS CODEX
  • 4. WordPress Actions WordPress Hooks Actions • http://codex.wordpress.org/Plugin_API/Action_Reference
  • 5. WordPress Actions Basic Functions • do_action() – http://codex.wordpress.org/Function_Reference/do_action • add_action() – http://codex.wordpress.org/Function_Reference/add_action • remove_action() – http://codex.wordpress.org/Function_Reference/remove_action
  • 6. WordPress Actions Basic Functions • do_action() – http://codex.wordpress.org/Function_Reference/do_action This is where the action fires in the code, and where your hook will attach itself. Example: do_action( „my_action‟ );
  • 7. WordPress Actions Basic Functions • add_action() – http://codex.wordpress.org/Function_Reference/add_action This is what you use to add your action (or functionality) to the do_action() sequence. Example: add_action( „my_action‟, „my_functionality‟, 10 );
  • 8. WordPress Actions Basic Functions • remove_action() – http://codex.wordpress.org/Function_Reference/remove_action This is what you use to remove a specific action that has been added … and it requires a call to an action, too?!
  • 9. WordPress Actions Basic Functions • remove_action() Example: function remove_my_action(){ remove_action( „my_action‟, „my_functionality‟, 10 ); } add_action( „init‟, „remove_my_action‟ ); Note: the priority being used; and, adding to the init action.
  • 10. WordPress Filters FILTERS ARE THE HOOKS THAT WORDPRESS LAUNCHES TO MODIFY TEXT OF VARIOUS TYPES BEFORE ADDING IT TO THE DATABASE OR SENDING IT TO THE BROWSER SCREEN. YOUR PLUGIN CAN SPECIFY THAT ONE OR MORE OF ITSPHP FUNCTIONS IS EXECUTED TO MODIFY SPECIFIC TYPES OF TEXT AT THESE TIMES, USING THE FILTER API. ~~ WORDPRESS CODEX
  • 11. WordPress Filters WordPress Hooks Filters • http://codex.wordpress.org/Plugin_API/Filter_Reference
  • 12. WordPress Filters Basic Functions • apply_filters – http://codex.wordpress.org/Function_Reference/apply_filters • add_filter – http://codex.wordpress.org/Function_Reference/add_filter • remove_filter – http://codex.wordpress.org/Function_Reference/remove_filter
  • 13. WordPress Filters Basic Functions • apply_filters – http://codex.wordpress.org/Function_Reference/apply_filters This is where the filter fires in the code, and where your hook will attach itself. Example: apply_filters( „my_filter‟, „my_original_value‟ );
  • 14. WordPress Filters Basic Functions • add_filter – http://codex.wordpress.org/Function_Reference/add_filter This is what you use to add your filter (or new value) to the apply_filters() item. Example: add_filter( „my_filter‟, „my_new_value‟ );
  • 15. WordPress Filters Basic Functions • remove_filter – http://codex.wordpress.org/Function_Reference/remove_filter This is what you use to remove a specific filter that has been used … and it is best to follow the remove_action() example as the safest method to implement.
  • 16. WordPress Filters Basic functions • remove_filter() Example: function remove_my_filter(){ remove_filter( „my_filter‟, „my_new_value‟, 20 ); } add_action( „init‟, „remove_my_filter‟ ); Note: the priority being used; and, adding to the init action.
  • 17. Hooking with WordPress 1. Open your local test environment 2. Make sure the BNS Bio plugin is installed and activated 3. Add the [bns_bio] shortcode to a post 4. Open your text editor 5. Navigate to the BNS Bio folder 6. Ready?
  • 18. BNS Bio Plugin The BNS Bio plugin adds a simple shortcode to your WordPress installation that outputs the author details of the post it is added to. The plugin uses a different approach to options by utilizing “extension” plugins versus an options panel. This is primarily for the purposes of using BNS Bio as example code.
  • 19. BNS Bio Structures • Uses the Class PHP structure – Uses a simple constuctor method • __construct() – Enqueues its own stylesheet with an optional call to a custom stylesheet • Scripts_and_Styles() – Collect the author details which it passes back to the shortcode `bns_bio` • author_block()
  • 20. BNS Bio __construct() The constructor method of the BNS Bio class calls an action to add the Scripts_and_Styles functionality … /** Add Scripts and Styles */ add_action( 'wp_enqueue_scripts', array( $this, 'Scripts_and_Styles' ) ); … note the difference from the earlier example.
  • 21. WordPress Actions Basic Functions • add_action() – http://codex.wordpress.org/Function_Reference/add_action Example: add_action( „my_action‟, „my_functionality‟, 10 ); Using in a Class example: add_action( „my_action‟, array( $this, „my_functionality‟ ) );
  • 22. BNS Bio author_block() • This is the main code of the plugin. • There are many instances of hooks (actions and filters) available to work with. Examples: do_action( 'bns_bio_before_all' ); apply_filters( 'bns_bio_author_url_text', … apply_filters( 'bns_bio_author_desc', …
  • 23. BNS Bio Box This is an “extension plugin” for BNS Bio. It adds a rounded corner border, or box, around the BNS Bio output by hooking an additional CSS container into the „bns_bio_before_all‟ and „bns_bio_after_all‟ actions; then adding the appropriate style properties to create the look.
  • 24. BNS Bio Box Code The essentials: • Define the functions to be used • Hook them into the appropriate action – add_action( 'bns_bio_before_all', 'bns_bio_open_box' ); – add_action( 'bns_bio_after_all', 'bns_bio_close_box' ); • Add the stylesheet (using the common enqueue method; and another add_action call)
  • 25. BNS Bio List This is another “extension plugin” for BNS Bio that changes the output to an unordered list by hooking into the available actions and adding the appropriate HTML („ul‟ and „li‟) elements. It also includes its own enqueued stylesheet and calls an optional custom stylesheet if available.
  • 26. BNS Bio List Code The essentials: • Add the „ul‟ container: – add_action( 'bns_bio_before_all', function(){ echo '<ul class="bns-bio-list">'; }, 20 ); – add_action( 'bns_bio_after_all', function(){ echo '</ul><!-- .bns-bio-list -->'; }, 9 ); • Add the „li‟ containers: – add_action( 'bns_bio_before_author_name', 'bns_bio_list_item' ); – add_action( 'bns_bio_before_author_url', 'bns_bio_list_item' ); – add_action( 'bns_bio_before_author_email', 'bns_bio_list_item' ); – add_action( 'bns_bio_before_author_desc', 'bns_bio_list_item' ); NB: I used two different methods for adding the functionality: a call to a predefined function („bns_bio_list_item‟) and anonymous functions1
  • 27. BNS Bio List Notes 1. The anonymous functions were used for example purposes and may not work with versions of PHP earlier than 5.3.0 where they were introduced.
  • 28. BNS Bio Hide This is another “extension plugin” for BNS Bio that changes the output by hiding the author email details. There are actually three items that need to be addressed to hide the email line in the output: • What displays before the email • What displays as the author (pre) email text • What displays as the author email
  • 29. WordPress Plugins 103 The essentials: • remove_action( 'bns_bio_before_author_email', 'bns_bio_list_item' ); – (as applied to the init hook – see original „remove_action‟ example) • add_filter( 'bns_bio_author_email_text', '__return_null', 100 ); • add_filter( 'bns_bio_author_email', '__return_null', 100 ); Notes: • The remove_action priority is left as the default as the „bns_bio_list_item‟ function was added at the default priority (these priorities must match). • Filters are essentially a “last one standing” mechanism; and in this case, the WordPress „__return_null‟ function was called at a very late priority to help insure it is last to run and therefore the final output.
  • 30. WordPress Plugins 103 Hooking with WordPress Questions and Answers
  • 31. Credits Presented by Edward Caissie for the WordPress GTA Meetup group on November 19, 2012. Contact Details: Email: edward.caissie@gmail.com URL: http://edwardcaissie.com
  • 32. Quick Resources Codex References - Actions: http://codex.wordpress.org/Plugin_API/Action_Reference http://codex.wordpress.org/Function_Reference/do_action http://codex.wordpress.org/Function_Reference/add_action http://codex.wordpress.org/Function_Reference/remove_filter Codex References – Filters: http://codex.wordpress.org/Plugin_API/Filter_Reference http://codex.wordpress.org/Function_Reference/apply_filters http://codex.wordpress.org/Function_Reference/add_filter http://codex.wordpress.org/Function_Reference/remove_filter
  • 33. WordPress Power Point Template Credits m62 visualcommunications: http://www.m62.net/powerpoint-templates/technology-templates/wordpress-powerpoint-template/
  • 34. WordPress Plugins 103: Hooking with WordPress Thank You.