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

More Related Content

PPT
Writing Pluggable Software
PDF
Functional Structures in PHP
PDF
Workshop quality assurance for php projects - phpbelfast
PPT
PDF
Hooks WCSD12
PDF
Getting testy with Perl
PDF
The Loop
PDF
WCLV13 JavaScript
Writing Pluggable Software
Functional Structures in PHP
Workshop quality assurance for php projects - phpbelfast
Hooks WCSD12
Getting testy with Perl
The Loop
WCLV13 JavaScript

What's hot (20)

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

Similar to Actions filters (20)

PPTX
2016 WordCamp Pittsburgh - Let's Write a Plugin
PDF
Jumping Into WordPress Plugin Programming
PPTX
WordPress Hooks Action & Filters
PPTX
Let’s write a plugin
PDF
Stop Hacking WordPress, Start Working with it - Charly Leetham - WordCamp Syd...
PPTX
Wordpress hooks
PDF
Demystifying Hooks, Actions & Filters - WordCamp Belfast 2018
ODP
Beginning WordPress Plugin Development
PDF
Introduction to WordPress Hooks 2016
PDF
Hooks, Actions, and Filters Oh My!
PDF
How I Became a WordPress Hacker
PPTX
Hooking with WordPress by Rahul Prajapati - COEP FOSSMeet March 2019
PPTX
Writing extensible plugins
PDF
Plugin development demystified 2017
PPT
Word press Plugins by WordPress Experts
PDF
Bending word press to your will
PDF
My first WordPress Plugin
PPT
KEY
How To Write a WordPress Plugin
PDF
Hooked on WordPress: WordCamp Columbus
2016 WordCamp Pittsburgh - Let's Write a Plugin
Jumping Into WordPress Plugin Programming
WordPress Hooks Action & Filters
Let’s write a plugin
Stop Hacking WordPress, Start Working with it - Charly Leetham - WordCamp Syd...
Wordpress hooks
Demystifying Hooks, Actions & Filters - WordCamp Belfast 2018
Beginning WordPress Plugin Development
Introduction to WordPress Hooks 2016
Hooks, Actions, and Filters Oh My!
How I Became a WordPress Hacker
Hooking with WordPress by Rahul Prajapati - COEP FOSSMeet March 2019
Writing extensible plugins
Plugin development demystified 2017
Word press Plugins by WordPress Experts
Bending word press to your will
My first WordPress Plugin
How To Write a WordPress Plugin
Hooked on WordPress: WordCamp Columbus
Ad

Recently uploaded (20)

PDF
OpenACC and Open Hackathons Monthly Highlights July 2025
PPTX
Configure Apache Mutual Authentication
PDF
Getting started with AI Agents and Multi-Agent Systems
PPTX
AI IN MARKETING- PRESENTED BY ANWAR KABIR 1st June 2025.pptx
PPTX
Chapter 5: Probability Theory and Statistics
PDF
How IoT Sensor Integration in 2025 is Transforming Industries Worldwide
PPTX
GROUP4NURSINGINFORMATICSREPORT-2 PRESENTATION
PDF
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
PPT
Module 1.ppt Iot fundamentals and Architecture
PDF
UiPath Agentic Automation session 1: RPA to Agents
PDF
Credit Without Borders: AI and Financial Inclusion in Bangladesh
PDF
Flame analysis and combustion estimation using large language and vision assi...
PDF
A review of recent deep learning applications in wood surface defect identifi...
PPTX
Microsoft Excel 365/2024 Beginner's training
PDF
Developing a website for English-speaking practice to English as a foreign la...
DOCX
search engine optimization ppt fir known well about this
PDF
sustainability-14-14877-v2.pddhzftheheeeee
PPT
What is a Computer? Input Devices /output devices
PDF
STKI Israel Market Study 2025 version august
PPTX
Benefits of Physical activity for teenagers.pptx
OpenACC and Open Hackathons Monthly Highlights July 2025
Configure Apache Mutual Authentication
Getting started with AI Agents and Multi-Agent Systems
AI IN MARKETING- PRESENTED BY ANWAR KABIR 1st June 2025.pptx
Chapter 5: Probability Theory and Statistics
How IoT Sensor Integration in 2025 is Transforming Industries Worldwide
GROUP4NURSINGINFORMATICSREPORT-2 PRESENTATION
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
Module 1.ppt Iot fundamentals and Architecture
UiPath Agentic Automation session 1: RPA to Agents
Credit Without Borders: AI and Financial Inclusion in Bangladesh
Flame analysis and combustion estimation using large language and vision assi...
A review of recent deep learning applications in wood surface defect identifi...
Microsoft Excel 365/2024 Beginner's training
Developing a website for English-speaking practice to English as a foreign la...
search engine optimization ppt fir known well about this
sustainability-14-14877-v2.pddhzftheheeeee
What is a Computer? Input Devices /output devices
STKI Israel Market Study 2025 version august
Benefits of Physical activity for teenagers.pptx

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.

Editor's Notes