SlideShare uma empresa Scribd logo
1 de 29
Hooked on WordPress
     Actions and Filters
John Dillick
              @johndillick


Core developer on Shopp e-commerce plugin
for WordPress

Developing for and supporting WordPress for
3 years

Technical writer and information sponge.

Not a design guy; Leave graphics and design
to other guys.
Who’s this for?
Desire to extend and customize WordPress

Some PHP knowledge

Not already a WordPress plugin developer, or
just need a place to sit and zone out for 45
minutes.

Don’t mind hearing me say “Um” at least a
thousand times. I’m not a professional
speaker.
WTF are actions/filters

RTFM - see http://codex.wordpress.org/
Plugin_API

Actions are points of execution in WordPress
that your plugin can “hook” code to execute.

Filters give your code an opportunity to add/
remove/modify data before it will be used by
WordPress and other plugins.
What actions/filter are
     out there?
Short answer: Too many to cover in 45
minutes.

Covering some important ones; Dig into codex
and WordPress code itself to discover
amazing possibilities.

Covering what the hooks look like, syntax
stuff, and how to leverage them.
Actions, why?
Devs think, ‘wouldn’t it be nice to have “this”
happen when “that” happens?’

One way to do this: modify WordPress code
directly.
                                    NO!
Actions are provided by the developer of a
particular feature of WordPress to give you an
opportunity to do “something” too.

Often provide you an opportunity to put things
into motion before, during, or after another
action
What does an action
    “hook” look like?

   <?php do_action(‘action_name’); ?>
                 <?php
do_action_ref_array(‘action_name’,$args);
                   ?>
How to hook in:


If the “hook” name in WP is ‘do_this’:

<?php do_action(‘do_this’); ?>

You hook in by providing a “callback” (function
name):

<?php add_action(‘do_this’,‘my_function’); ?>
Callbacks
Quick PHP review, a callback is:

1. String name of a function: ‘funk’
<?php function funk () {} ?>

<?php

//callback

add_action(‘do_something’,‘funk’);
Callbacks, cont.
2. An array with an Object and a string function
name.
<?php

class A {

    function funk () {}

}

$A = new A();

//callback for $A->funk();

add_action( ‘do_something’, array($A,‘funk’) ); ?>
Callbacks, cont.
3. An array with a class name and a string static
function name.
<?php

class B {

     static function funk () {}

}

//callback for B::funk();

add_action( ‘do_something’, array(‘B’,‘funk’) );

?>
Callbacks, cont.
3. Callback returned from a function, such as
create_function()
<?php

// callback for anonymous function

$my_func = create_function(“$args”,”echo ‘hello world!’;”);

add_action( ‘do_something’, $my_func );

?>
add_action()
The full form:

<?php

add_action( $tagname, $callback, $priority,
$argcount );

?>
add_action() cont.

$priority: (Default 10) The smaller the number,
the quicker your function will happen, the
bigger, the later.

$argcount: (Default 1) If the action passes 1 or
more arguments as useful data, you can tell
how many you want passed to your callback.
do_action() example
<?php //your code

add_action(‘hello_set’,’a’,10,1);

add_action(‘hello_set’,’b’,1);

function a ( &$hello ) { $hello = ‘hola, el mundo’; }

function b () { exit(); } ?>

<?php

// Primitive action hook, pretend it’s in WordPress

$hello = ‘hello world’;

do_action(‘hello_set’, $hello);

echo $hello;

?>
What output?
Answer: Nothing

Because function ‘b’ was called with a higher
priority, and exited, ‘a’ never ran... nor anything
else afterwards.

All plugins have an opportunity to break all other
plugins at some point.

Use standard WordPress function calls whenever
possible.
Filtering

WordPress is using some data to do
something, for instance, to output the body of
a post on my blog.

As a developer, I want to be able to add,
remove, or modify that data before WordPress
uses it.
How to filter:

If the “filter” tag name in WP is ‘the_content’:

<?php apply_filters(‘the_content’,$postcontent); ?>

You hook in by providing a “callback”, just like with
actions hooks.

<?php add_filter(‘the_content’,‘my_function’); ?>

my_function will always expect at least one arg,
and will always return an arg.
Full filter syntax

add_filter($tagname, $callback, $priority,
$num_of_args);

Your filter is expected to return something.

Forgetting to return something will certainly
break something.
Wordpress with ‘P’!
Back in 2010, WordPress decided to take
measures to ensure the WordPress trademark was
used properly.
<?php

add_filter(‘the_content’,‘capital_P_dangit’);

add_filter(‘the_title’,‘capital_P_dangit’);

add_filter(‘comment_text’,‘capital_P_dangit’);

?>
If I had core commit...
... you would all know.
<?php

function john_dillick_rocks ( $content ) {

!    $content = str_replace(‘Dillick sucks’,‘Dillick rocks’,$content);

!    return $content . ‘ - BTW, you know John Dillick rocks, right?’;

}

add_filter(‘the_content’,‘john_dillick_rocks’);

add_filter(‘the_title’,‘john_dillick_rocks’);

add_filter(‘comment_text’,‘john_dillick_rocks’);

?>
Removing Action/Filter
remove_action($tag,$callback,$priority,$nargs);

remove_filter($tag,$callback,$priority,$nargs);

remove_all_actions($tag,$priority);

remove_all_filters($tag,$priority);
<?php

foreach ( array(‘the_content’,‘the_title’,‘comment_text’) as $tag ) {

!   remove_action($tag,’capital_P_dangit’);

!   // remove_action($tag,‘john_dillick_rocks’);

}
Check for an action/filter
has_action($tag,$callback);

has_filter($tag,$callback);
<?php

function enlightened () {

!    if ( has_filter(‘the_content’,‘john_dillick_rocks’) )

!    !   echo “<p>This site powered by Sasquatch.</p>”;

!    else echo “<p>Nothing to see here. Move along.</p>”;

}

add_action(‘wp_footer’,’enlightened’);

?>
Common Actions

add_action(‘init’,$callback);
Early action for plugin initialization.

add_action(‘admin_init’,$callback);
Called on admin initialization.
Common Actions
add_action(‘wp_head’,$callback);

Called in theme when page header is output.


add_action(‘admin_head’,$callback);
Called in admin when page header is output.

add_action(‘wp_footer’,$callback);
Called in theme when page footer is output.

add_action(‘admin_footer’,$callback);
Called in admin when page footer is output.
Action Reference


http://codex.wordpress.org/Plugin_API/
Action_Reference
Common Filters

add_filter(‘the_content’,$callback);
Filters post and page content.

add_filter(‘the_excerpt’,$callback);
Filters post and page excerpt content.
Common Filters

add_filter(‘the_title’,$callback);
Filters post and page content titles.

add_filter(‘wp_title’,$callback);
Filters blog page title.
Filters Reference


http://codex.wordpress.org/Plugin_API/
Filter_Reference

Mais conteúdo relacionado

Mais procurados

Building a Pyramid: Symfony Testing Strategies
Building a Pyramid: Symfony Testing StrategiesBuilding a Pyramid: Symfony Testing Strategies
Building a Pyramid: Symfony Testing StrategiesCiaranMcNulty
 
Hack in the Box Keynote 2006
Hack in the Box Keynote 2006Hack in the Box Keynote 2006
Hack in the Box Keynote 2006Mark Curphey
 
Spiffy Applications With JavaScript
Spiffy Applications With JavaScriptSpiffy Applications With JavaScript
Spiffy Applications With JavaScriptMark Casias
 
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
 
Your Business. Your Language. Your Code - dpc13
Your Business. Your Language. Your Code - dpc13Your Business. Your Language. Your Code - dpc13
Your Business. Your Language. Your Code - dpc13Stephan Hochdörfer
 
Zephir - A Wind of Change for writing PHP extensions
Zephir - A Wind of Change for writing PHP extensionsZephir - A Wind of Change for writing PHP extensions
Zephir - A Wind of Change for writing PHP extensionsMark Baker
 
You don’t know query - WordCamp UK Edinburgh 2012
You don’t know query - WordCamp UK Edinburgh 2012You don’t know query - WordCamp UK Edinburgh 2012
You don’t know query - WordCamp UK Edinburgh 2012l3rady
 
P H P Part I I, By Kian
P H P  Part  I I,  By  KianP H P  Part  I I,  By  Kian
P H P Part I I, By Kianphelios
 
Smarter Interfaces with jQuery (and Drupal)
Smarter Interfaces with jQuery (and Drupal)Smarter Interfaces with jQuery (and Drupal)
Smarter Interfaces with jQuery (and Drupal)aasarava
 
How does get template part works in twenty ten theme
How does get template part works in twenty ten themeHow does get template part works in twenty ten theme
How does get template part works in twenty ten thememohd rozani abd ghani
 
A Little Backbone For Your App
A Little Backbone For Your AppA Little Backbone For Your App
A Little Backbone For Your AppLuca Mearelli
 
Dealing with Legacy Perl Code - Peter Scott
Dealing with Legacy Perl Code - Peter ScottDealing with Legacy Perl Code - Peter Scott
Dealing with Legacy Perl Code - Peter ScottO'Reilly Media
 
Tobias Nyholm "Deep dive into Symfony 4 internals"
Tobias Nyholm "Deep dive into Symfony 4 internals"Tobias Nyholm "Deep dive into Symfony 4 internals"
Tobias Nyholm "Deep dive into Symfony 4 internals"Fwdays
 
OSCON Google App Engine Codelab - July 2010
OSCON Google App Engine Codelab - July 2010OSCON Google App Engine Codelab - July 2010
OSCON Google App Engine Codelab - July 2010ikailan
 

Mais procurados (20)

Building a Pyramid: Symfony Testing Strategies
Building a Pyramid: Symfony Testing StrategiesBuilding a Pyramid: Symfony Testing Strategies
Building a Pyramid: Symfony Testing Strategies
 
QA for PHP projects
QA for PHP projectsQA for PHP projects
QA for PHP projects
 
Hack in the Box Keynote 2006
Hack in the Box Keynote 2006Hack in the Box Keynote 2006
Hack in the Box Keynote 2006
 
Django a whirlwind tour
Django   a whirlwind tourDjango   a whirlwind tour
Django a whirlwind tour
 
Spiffy Applications With JavaScript
Spiffy Applications With JavaScriptSpiffy Applications With JavaScript
Spiffy Applications With JavaScript
 
The Best (and Worst) of Django
The Best (and Worst) of DjangoThe Best (and Worst) of Django
The Best (and Worst) of Django
 
Your Business. Your Language. Your Code - dpc13
Your Business. Your Language. Your Code - dpc13Your Business. Your Language. Your Code - dpc13
Your Business. Your Language. Your Code - dpc13
 
Get your teeth into Plack
Get your teeth into PlackGet your teeth into Plack
Get your teeth into Plack
 
Extending Twig
Extending TwigExtending Twig
Extending Twig
 
Zephir - A Wind of Change for writing PHP extensions
Zephir - A Wind of Change for writing PHP extensionsZephir - A Wind of Change for writing PHP extensions
Zephir - A Wind of Change for writing PHP extensions
 
You don’t know query - WordCamp UK Edinburgh 2012
You don’t know query - WordCamp UK Edinburgh 2012You don’t know query - WordCamp UK Edinburgh 2012
You don’t know query - WordCamp UK Edinburgh 2012
 
P H P Part I I, By Kian
P H P  Part  I I,  By  KianP H P  Part  I I,  By  Kian
P H P Part I I, By Kian
 
Smarter Interfaces with jQuery (and Drupal)
Smarter Interfaces with jQuery (and Drupal)Smarter Interfaces with jQuery (and Drupal)
Smarter Interfaces with jQuery (and Drupal)
 
New in php 7
New in php 7New in php 7
New in php 7
 
How does get template part works in twenty ten theme
How does get template part works in twenty ten themeHow does get template part works in twenty ten theme
How does get template part works in twenty ten theme
 
A Little Backbone For Your App
A Little Backbone For Your AppA Little Backbone For Your App
A Little Backbone For Your App
 
Dealing with Legacy Perl Code - Peter Scott
Dealing with Legacy Perl Code - Peter ScottDealing with Legacy Perl Code - Peter Scott
Dealing with Legacy Perl Code - Peter Scott
 
A Phing fairy tale - ConFoo13
A Phing fairy tale - ConFoo13A Phing fairy tale - ConFoo13
A Phing fairy tale - ConFoo13
 
Tobias Nyholm "Deep dive into Symfony 4 internals"
Tobias Nyholm "Deep dive into Symfony 4 internals"Tobias Nyholm "Deep dive into Symfony 4 internals"
Tobias Nyholm "Deep dive into Symfony 4 internals"
 
OSCON Google App Engine Codelab - July 2010
OSCON Google App Engine Codelab - July 2010OSCON Google App Engine Codelab - July 2010
OSCON Google App Engine Codelab - July 2010
 

Semelhante a Actions filters

Jumping Into WordPress Plugin Programming
Jumping Into WordPress Plugin ProgrammingJumping Into WordPress Plugin Programming
Jumping Into WordPress Plugin ProgrammingDougal Campbell
 
Demystifying Hooks, Actions & Filters - WordCamp Belfast 2018
Demystifying Hooks, Actions & Filters - WordCamp Belfast 2018Demystifying Hooks, Actions & Filters - WordCamp Belfast 2018
Demystifying Hooks, Actions & Filters - WordCamp Belfast 2018Damien Carbery
 
WordPress Plugin development
WordPress Plugin developmentWordPress Plugin development
WordPress Plugin developmentMostafa Soufi
 
How To Write a WordPress Plugin
How To Write a WordPress PluginHow To Write a WordPress Plugin
How To Write a WordPress PluginAndy Stratton
 
CodeIgniter PHP MVC Framework
CodeIgniter PHP MVC FrameworkCodeIgniter PHP MVC Framework
CodeIgniter PHP MVC FrameworkBo-Yi Wu
 
WordPress Plugin Basics
WordPress Plugin BasicsWordPress Plugin Basics
WordPress Plugin BasicsAmanda Giles
 
Plug in development
Plug in developmentPlug in development
Plug in developmentLucky Ali
 
[Bristol WordPress] Supercharging WordPress Development
[Bristol WordPress] Supercharging WordPress Development[Bristol WordPress] Supercharging WordPress Development
[Bristol WordPress] Supercharging WordPress DevelopmentAdam Tomat
 
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
 
Creating Your First WordPress Plugin
Creating Your First WordPress PluginCreating Your First WordPress Plugin
Creating Your First WordPress PluginColin Loretz
 
Becoming a better WordPress Developer
Becoming a better WordPress DeveloperBecoming a better WordPress Developer
Becoming a better WordPress DeveloperJoey Kudish
 
Best Practices in Plugin Development (WordCamp Seattle)
Best Practices in Plugin Development (WordCamp Seattle)Best Practices in Plugin Development (WordCamp Seattle)
Best Practices in Plugin Development (WordCamp Seattle)andrewnacin
 
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
 
Write your first WordPress plugin
Write your first WordPress pluginWrite your first WordPress plugin
Write your first WordPress pluginAnthony Montalbano
 
How I Became a WordPress Hacker
How I Became a WordPress HackerHow I Became a WordPress Hacker
How I Became a WordPress HackerMike Zielonka
 
WordPress basic fundamental of plugin development and creating shortcode
WordPress basic fundamental of plugin development and creating shortcodeWordPress basic fundamental of plugin development and creating shortcode
WordPress basic fundamental of plugin development and creating shortcodeRakesh Kushwaha
 
Как получить чёрный пояс по WordPress?
Как получить чёрный пояс по WordPress?Как получить чёрный пояс по WordPress?
Как получить чёрный пояс по WordPress?Yevhen Kotelnytskyi
 
PHP-03-Functions.ppt
PHP-03-Functions.pptPHP-03-Functions.ppt
PHP-03-Functions.pptJamers2
 

Semelhante a Actions filters (20)

Jumping Into WordPress Plugin Programming
Jumping Into WordPress Plugin ProgrammingJumping Into WordPress Plugin Programming
Jumping Into WordPress Plugin Programming
 
Demystifying Hooks, Actions & Filters - WordCamp Belfast 2018
Demystifying Hooks, Actions & Filters - WordCamp Belfast 2018Demystifying Hooks, Actions & Filters - WordCamp Belfast 2018
Demystifying Hooks, Actions & Filters - WordCamp Belfast 2018
 
WordPress Plugin development
WordPress Plugin developmentWordPress Plugin development
WordPress Plugin development
 
09 Oo Php Register
09 Oo Php Register09 Oo Php Register
09 Oo Php Register
 
How To Write a WordPress Plugin
How To Write a WordPress PluginHow To Write a WordPress Plugin
How To Write a WordPress Plugin
 
CodeIgniter PHP MVC Framework
CodeIgniter PHP MVC FrameworkCodeIgniter PHP MVC Framework
CodeIgniter PHP MVC Framework
 
WordPress Plugin Basics
WordPress Plugin BasicsWordPress Plugin Basics
WordPress Plugin Basics
 
Plug in development
Plug in developmentPlug in development
Plug in development
 
[Bristol WordPress] Supercharging WordPress Development
[Bristol WordPress] Supercharging WordPress Development[Bristol WordPress] Supercharging WordPress Development
[Bristol WordPress] Supercharging WordPress Development
 
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)
 
Creating Your First WordPress Plugin
Creating Your First WordPress PluginCreating Your First WordPress Plugin
Creating Your First WordPress Plugin
 
Becoming a better WordPress Developer
Becoming a better WordPress DeveloperBecoming a better WordPress Developer
Becoming a better WordPress Developer
 
Best Practices in Plugin Development (WordCamp Seattle)
Best Practices in Plugin Development (WordCamp Seattle)Best Practices in Plugin Development (WordCamp Seattle)
Best Practices in Plugin Development (WordCamp Seattle)
 
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)
 
Write your first WordPress plugin
Write your first WordPress pluginWrite your first WordPress plugin
Write your first WordPress plugin
 
How I Became a WordPress Hacker
How I Became a WordPress HackerHow I Became a WordPress Hacker
How I Became a WordPress Hacker
 
WordPress basic fundamental of plugin development and creating shortcode
WordPress basic fundamental of plugin development and creating shortcodeWordPress basic fundamental of plugin development and creating shortcode
WordPress basic fundamental of plugin development and creating shortcode
 
Как получить чёрный пояс по WordPress?
Как получить чёрный пояс по WordPress?Как получить чёрный пояс по WordPress?
Как получить чёрный пояс по WordPress?
 
PHP-03-Functions.ppt
PHP-03-Functions.pptPHP-03-Functions.ppt
PHP-03-Functions.ppt
 
PHP-03-Functions.ppt
PHP-03-Functions.pptPHP-03-Functions.ppt
PHP-03-Functions.ppt
 

Último

Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
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
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdfChristopherTHyatt
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
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
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfhans926745
 
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
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
[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
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
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
 

Último (20)

Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
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...
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
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...
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
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
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
[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
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
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
 

Actions filters

  • 1. Hooked on WordPress Actions and Filters
  • 2. John Dillick @johndillick Core developer on Shopp e-commerce plugin for WordPress Developing for and supporting WordPress for 3 years Technical writer and information sponge. Not a design guy; Leave graphics and design to other guys.
  • 3. Who’s this for? Desire to extend and customize WordPress Some PHP knowledge Not already a WordPress plugin developer, or just need a place to sit and zone out for 45 minutes. Don’t mind hearing me say “Um” at least a thousand times. I’m not a professional speaker.
  • 4. WTF are actions/filters RTFM - see http://codex.wordpress.org/ Plugin_API Actions are points of execution in WordPress that your plugin can “hook” code to execute. Filters give your code an opportunity to add/ remove/modify data before it will be used by WordPress and other plugins.
  • 5. What actions/filter are out there? Short answer: Too many to cover in 45 minutes. Covering some important ones; Dig into codex and WordPress code itself to discover amazing possibilities. Covering what the hooks look like, syntax stuff, and how to leverage them.
  • 6. Actions, why? Devs think, ‘wouldn’t it be nice to have “this” happen when “that” happens?’ One way to do this: modify WordPress code directly. NO! Actions are provided by the developer of a particular feature of WordPress to give you an opportunity to do “something” too. Often provide you an opportunity to put things into motion before, during, or after another action
  • 7. What does an action “hook” look like? <?php do_action(‘action_name’); ?> <?php do_action_ref_array(‘action_name’,$args); ?>
  • 8. How to hook in: If the “hook” name in WP is ‘do_this’: <?php do_action(‘do_this’); ?> You hook in by providing a “callback” (function name): <?php add_action(‘do_this’,‘my_function’); ?>
  • 9. Callbacks Quick PHP review, a callback is: 1. String name of a function: ‘funk’ <?php function funk () {} ?> <?php //callback add_action(‘do_something’,‘funk’);
  • 10. Callbacks, cont. 2. An array with an Object and a string function name. <?php class A { function funk () {} } $A = new A(); //callback for $A->funk(); add_action( ‘do_something’, array($A,‘funk’) ); ?>
  • 11. Callbacks, cont. 3. An array with a class name and a string static function name. <?php class B { static function funk () {} } //callback for B::funk(); add_action( ‘do_something’, array(‘B’,‘funk’) ); ?>
  • 12. Callbacks, cont. 3. Callback returned from a function, such as create_function() <?php // callback for anonymous function $my_func = create_function(“$args”,”echo ‘hello world!’;”); add_action( ‘do_something’, $my_func ); ?>
  • 13. add_action() The full form: <?php add_action( $tagname, $callback, $priority, $argcount ); ?>
  • 14. add_action() cont. $priority: (Default 10) The smaller the number, the quicker your function will happen, the bigger, the later. $argcount: (Default 1) If the action passes 1 or more arguments as useful data, you can tell how many you want passed to your callback.
  • 15. do_action() example <?php //your code add_action(‘hello_set’,’a’,10,1); add_action(‘hello_set’,’b’,1); function a ( &$hello ) { $hello = ‘hola, el mundo’; } function b () { exit(); } ?> <?php // Primitive action hook, pretend it’s in WordPress $hello = ‘hello world’; do_action(‘hello_set’, $hello); echo $hello; ?>
  • 16. What output? Answer: Nothing Because function ‘b’ was called with a higher priority, and exited, ‘a’ never ran... nor anything else afterwards. All plugins have an opportunity to break all other plugins at some point. Use standard WordPress function calls whenever possible.
  • 17. Filtering WordPress is using some data to do something, for instance, to output the body of a post on my blog. As a developer, I want to be able to add, remove, or modify that data before WordPress uses it.
  • 18. How to filter: If the “filter” tag name in WP is ‘the_content’: <?php apply_filters(‘the_content’,$postcontent); ?> You hook in by providing a “callback”, just like with actions hooks. <?php add_filter(‘the_content’,‘my_function’); ?> my_function will always expect at least one arg, and will always return an arg.
  • 19. Full filter syntax add_filter($tagname, $callback, $priority, $num_of_args); Your filter is expected to return something. Forgetting to return something will certainly break something.
  • 20. Wordpress with ‘P’! Back in 2010, WordPress decided to take measures to ensure the WordPress trademark was used properly. <?php add_filter(‘the_content’,‘capital_P_dangit’); add_filter(‘the_title’,‘capital_P_dangit’); add_filter(‘comment_text’,‘capital_P_dangit’); ?>
  • 21. If I had core commit... ... you would all know. <?php function john_dillick_rocks ( $content ) { ! $content = str_replace(‘Dillick sucks’,‘Dillick rocks’,$content); ! return $content . ‘ - BTW, you know John Dillick rocks, right?’; } add_filter(‘the_content’,‘john_dillick_rocks’); add_filter(‘the_title’,‘john_dillick_rocks’); add_filter(‘comment_text’,‘john_dillick_rocks’); ?>
  • 22. Removing Action/Filter remove_action($tag,$callback,$priority,$nargs); remove_filter($tag,$callback,$priority,$nargs); remove_all_actions($tag,$priority); remove_all_filters($tag,$priority); <?php foreach ( array(‘the_content’,‘the_title’,‘comment_text’) as $tag ) { ! remove_action($tag,’capital_P_dangit’); ! // remove_action($tag,‘john_dillick_rocks’); }
  • 23. Check for an action/filter has_action($tag,$callback); has_filter($tag,$callback); <?php function enlightened () { ! if ( has_filter(‘the_content’,‘john_dillick_rocks’) ) ! ! echo “<p>This site powered by Sasquatch.</p>”; ! else echo “<p>Nothing to see here. Move along.</p>”; } add_action(‘wp_footer’,’enlightened’); ?>
  • 24. Common Actions add_action(‘init’,$callback); Early action for plugin initialization. add_action(‘admin_init’,$callback); Called on admin initialization.
  • 25. Common Actions add_action(‘wp_head’,$callback); Called in theme when page header is output. add_action(‘admin_head’,$callback); Called in admin when page header is output. add_action(‘wp_footer’,$callback); Called in theme when page footer is output. add_action(‘admin_footer’,$callback); Called in admin when page footer is output.
  • 27. Common Filters add_filter(‘the_content’,$callback); Filters post and page content. add_filter(‘the_excerpt’,$callback); Filters post and page excerpt content.
  • 28. Common Filters add_filter(‘the_title’,$callback); Filters post and page content titles. add_filter(‘wp_title’,$callback); Filters blog page title.

Notas do Editor

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n