飞龙博客

深入经藏 智慧如海

wordpress action hook samples add_action do_action

feilong.org 修订于2011-05-03 12:26:19 1,157

Below is the wordpress action hook samples of add_action then do_action.This post url is http://feilong.org/add-action-do-action-wordpress-hook edit by feilong in nanjing

First in functions.php of your wordpress theme

function hooked_function_one($var1,$var2){//the number is 2
echo '<p>'.$var1.'and'.$var2.'</p>';
}add_action( 'i_am_hook_name', 'hooked_function_one', 10, 2 );//must the same number as var 
function hooked_function_two($var){
echo '<p>'.$var.'</p>';
}add_action( 'i_am_hook_name', 'hooked_function_two', 91,1 );
 
function feilong_function(){
echo 'feilong is happy today.';
}add_action('feilonghook','feilong_function',2);//feilong_function is hooked
 
function feilong_footer_message_plugin_setup() {
add_action('wp_footer','feilong_blog_useful',100);//add a functon to the hook 'wp_footer',which is in wordpress core
}
function feilong_blog_useful(){
echo 'Is feilong.org useful for friends?';
}

Then in page.php of your wordpress theme

$c = 'var one ';
$d = ' var two'; 
$e='this var is not used in fact.';
do_action('i_am_hook_name',$c,$d,$e);// $e is not used and can be cut off;
 
remove_action( 'i_am_hook_name', 'hooked_function_two', 91,1 );//remove the second hooked functon only
do_action('i_am_hook_name',$c,$d);// if remove_action with the order 91,then only hooked_function_one is hooked
 
do_action('feilonghook');// no args is passed to the hooked function
//remove_action( 'feilonghook', 'feilong_function', 2 );// remove the hook to the function
//do_action('feilonghook');// if remove_action is true,nothing will happen this time.
 
if( has_action( 'org' ) )
echo '<p> org hooks some function(s).</p>';
else
echo '<p> org hooks nothing.</p>';
 
if( has_action( 'i_am_hook_name' ) )
echo '<p> i_am_hook_name hooks some function(s).</p>';
else
echo '<p> i_am_hook_name hooks nothing.</p>';
 
echo did_action('i_am_hook_name');//check if done action of i_am_hook_name,return the times.
 
if(did_action('plugins_loaded'))
define('feilong_plugins_ready.',true);//if i_am_hook_name is done,then define the var
 
add_action('plugins_loaded','feilong_footer_message_plugin_setup');//add a functon to the hook 'plugins_loaded'plugins_loaded is in wordpress core

What does a hook in wordpress mean? In feilong's eyes,it just like a hook! A hook hooks something(that is,function or functions) or hooks nothing! If use the hook(do_action),the hooked fuction(s) wound start working!

更新网址:http://feilong.org/add-action-do-action-wordpress-hook
最初发布:20110502 12:49:24 feilong.org 于广州

加入收藏夹,查看更方便。

所在分类: wordpress

新作:

旧文:

飞龙前端QQ群