3. What are Hooks?
• 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, 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
specific 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 specific action
created with do_action()
• remove_action() - Removes a function attached to a
specified 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_filters() - Calls the functions added to a filter
hook via
add_filter()
• add_filter() - Hooks a function onto a specific filter
action created with add_filters()
• remove_filter() - Removes a function attached to a
specified filter action hook
45. Filters: apply_filters()
apply_filters( $tag, $value, $var ... );
• $tag - (string) (required) The name of the filter hook.
Default: None
• $value - (mixed) (required) The value which the filters
hooked to $tag may modify.
Default: None
• $var - (mixed) (optional) One or more additional
variables passed to the filter functions.
Default: None
46. Filters: add_filter()
add_filter( $tag, $function_to_add, $priority, $accepted_args );
• $tag - (string) (required) The name of the filter to hook the $function_to_add
to. Default: None
• $function_to_add - (callback) (required) The name of the function to be called
when the filter 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_filters( 'filter_awesome_text', '<h2>awesome</h2>' );
which filter? which function of ours?
add_filter( ‘filter_awesome_text’, ‘filter_awesome’ );
function filter_awesome( $text ) {
$text .= ‘<h4>puppies</h4>’;
return $text;
}
...in your functions.php script....
48. echo apply_filters( 'filter_awesome_text', '<h2>awesome</h2>' );
add_filter( ‘filter_awesome_text’, ‘filter_awesome’ );
function filter_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_filters)
• Google search!
• http://codex.wordpress.org/Plugin_API