The document discusses WordPress actions and filters. Actions allow plugins to hook into points of execution, while filters allow plugins to modify data before it is used. It provides examples of how to use add_action(), add_filter(), remove_action(), remove_filter() and has_action/filter(). Common core actions and filters are also listed, such as init, wp_head, the_content, and the_title. The purpose is to demonstrate how plugins can leverage hooks to customize WordPress behavior.
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 );
?>
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’);
?>
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’);
?>
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.