3. What are Hooks?
ā¢ Actions - Actions are the hooks that the WordPress core
launches at speciļ¬c points during execution, or when speciļ¬c
events occur.Your plugin can specify that one or more of its
PHP functions are executed at these points, using the Action
API.
ā¢ 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 its PHP functions is executed to modify
speciļ¬c types of text at these times, using the Filter API.
20. Actions
(do stuff)
ā¢ do_action() - Creates a hook for attaching actions via
add_action()
ā¢ add_action() - Hooks a function onto a speciļ¬c action
created with do_action()
ā¢ remove_action() - Removes a function attached to a
speciļ¬ed action hook
21. Actions: do_action()
do_action(Ā $tag,Ā $arg, $extra_argĀ );
ā¢ $tag - (string) (required) The name of the hook you wish to create.
Default: None
ā¢ $arg - (mixed) (optional) The list of arguments this hook accepts.
Default: ''
...their code...
22. Actions: add_action()
add_action(Ā $tag,Ā $function_to_add,Ā $priority,Ā $accepted_argsĀ );
ā¢ $tag - (string) (required) The name of the action you wish to hook onto. Default:
None
ā¢ $function_to_add - (callback) (required) The name of the function you wish to be
called. Default: None
ā¢ $priority - (int) (optional) How important your function is. Alter this to make your
function be called before or after other functions. Default: 10
ā¢ $accepted_args - (int) (optional) How many arguments your function takes. Default: 1
...our code...
26. do_action( 'before_awesome_widget' );
which action? which function of ours?
add_action( ābefore_awesome_widgetā, āpuppyā );
function puppy() {
$src = 'http://placedog.com/g/188/150';
echo '<img src="' . $src . '" />';
}
...in your functions.php script....
27. do_action( 'before_awesome_widget' );
which action? which function of ours?
add_action( ābefore_awesome_widgetā, āpuppyā );
function puppy() {
$src = 'http://placedog.com/g/188/150';
echo '<img src="' . $src . '" />';
}
...in your functions.php script....
28. do_action( 'before_awesome_widget' );
which action? which function of ours?
add_action( ābefore_awesome_widgetā, āpuppyā );
function puppy() {
$src = 'http://placedog.com/g/188/150';
echo '<img src="' . $src . '" />';
}
Use a unique function name
...in your functions.php script....
29.
30. Multiple Calls to Action
add_action( ābefore_awesome_widgetā, āpuppyā );
function puppy() {
$src = 'http://placedog.com/g/188/150';
echo '<img src="' . $src . '" />';
}
add_action( ābefore_awesome_widgetā, āwarningā );
function warning() {
echo '<p>Warning: puppies!</p>';
}
...in your functions.php script....
31. Multiple Calls to Action
add_action( ābefore_awesome_widgetā, āpuppyā );
function puppy() {
$src = 'http://placedog.com/g/188/150';
Same
echo '<img src="' . $src . '" />';
Action
}
add_action( ābefore_awesome_widgetā, āwarningā );
function warning() {
echo '<p>Warning: puppies!</p>';
}
...in your functions.php script....
32. Multiple Calls to Action
add_action( ābefore_awesome_widgetā, āpuppyā );
function puppy() {
$src = 'http://placedog.com/g/188/150';
Diff. F
Same
echo '<img src="' . $src . '" />';
unctio
Action
}
ns
add_action( ābefore_awesome_widgetā, āwarningā );
function warning() {
echo '<p>Warning: puppies!</p>';
}
...in your functions.php script....
44. Filters
(change text)
ā¢ apply_ļ¬lters() - Calls the functions added to a ļ¬lter
hook via
add_ļ¬lter()
ā¢ add_ļ¬lter() - Hooks a function onto a speciļ¬c ļ¬lter
action created with add_ļ¬lters()
ā¢ remove_ļ¬lter() - Removes a function attached to a
speciļ¬ed ļ¬lter action hook
45. Filters: apply_ļ¬lters()
apply_ļ¬lters(Ā $tag,Ā $value,Ā $varĀ ...Ā );
ā¢ $tag - (string) (required) The name of the ļ¬lter hook.
Default: None
ā¢ $value - (mixed) (required) The value which the ļ¬lters
hooked to $tag may modify.
Default: None
ā¢ $var - (mixed) (optional) One or more additional
variables passed to the ļ¬lter functions.
Default: None
46. Filters: add_ļ¬lter()
add_ļ¬lter(Ā $tag,Ā $function_to_add,Ā $priority,Ā $accepted_argsĀ );
ā¢ $tag - (string) (required) The name of the ļ¬lter to hook the $function_to_add
to. Default: None
ā¢ $function_to_add - (callback) (required) The name of the function to be called
when the ļ¬lter is applied. Default: None
ā¢ $priority - (int) (optional) Used to specify the order in which the functions
associated with a particular action are executed. Functions with the same
priority are executed in the order in which they were added to the action.
Default: 10
ā¢ $accepted_args - (int) (optional) Number of arguments the function(s) accept
(s). Default: 1
47. echo apply_ļ¬lters( 'ļ¬lter_awesome_text', '<h2>awesome</h2>' );
which ļ¬lter? which function of ours?
add_ļ¬lter( āļ¬lter_awesome_textā, āļ¬lter_awesomeā );
function ļ¬lter_awesome( $text ) {
$text .= ā<h4>puppies</h4>ā;
return $text;
}
...in your functions.php script....
48. echo apply_ļ¬lters( 'ļ¬lter_awesome_text', '<h2>awesome</h2>' );
add_ļ¬lter( āļ¬lter_awesome_textā, āļ¬lter_awesomeā );
function ļ¬lter_awesome( $text ) {
$text .= ā<h4>puppies</h4>ā;
return $text;
} Use a unique function name
...in your functions.php script....
81. Where Hooks?
ā¢ Plugin/Theme Wiki
ā¢ Documentation
ā¢ Actual Code
(search for do_action and apply_ļ¬lters)
ā¢ Google search!
ā¢ http://codex.wordpress.org/Plugin_API