32
loading...
This website collects cookies to deliver better user experience
add_action($hook_name, $your_function_name, $priority, $accepted_args );
Function your_function_name()
{
// your code
}
<?php
// Let’s get the cat some food
// This enables Dave to go the store after work
add_action(‘after_work’, ‘get_cat_food’, 10, 2);
// This defines how does it
Function get_cat_food($dave_has_wallet, $cat_is_hungry) {
// Dave only goes if he has the cash and the cat needs food
if($dave_has_wallet && $cat_is_hungry) {
Echo ‘Dave is on his way to the store’;
}
?>
do_action(‘after_work’, $dave_has_wallet=true, $cat_is_hungry=true);
Function page_who_is_perfoming_action($a,$b)
{
Echo ‘<code>’;
print_r($a); // ‘print_r ’ is the array data inside the first argument
Echo ‘</code>’;
Echo ‘<br/>’.$b; // echo line break and value of second argument
}
add_action(‘page_i_am_performing_action’, ‘page_who_is _performing action’, 10, 2);
// Define the arguments for the action hook
$a=array(
‘eye patch’=> ‘yes’,
‘parrot’=> true,
‘wooden leg’=> 1
);
$b= (‘and action states: “I had my dinner with dave.”’, ‘textdomain’);
// Executes the action hook named ‘i_am_performing_action’
do_action(‘page_i_am_performing_action’, $a, $b);
add_filter($hook_name, $your_function_name, $priority, $accepted_args );
// Giving the cat a speech
add_filter(‘cat_meows’, ‘cat_can_speak’);
Function cat_can_speak($meow) {
$meow= “I am hungry now”;
Return $meow; }
add_filter( 'hook', 'example_callback' ); // Where $priority is default 10, $accepted_args is default 1
// Accepting two arguments (three possible).
function example_callback( $value, $arg2 ) {
…………….
return $maybe_modified_value;
}
add_filter( 'hook', 'example_callback', 10, 2 ); // Where $priority is 10, $accepted_args is 2