不要问我怎么做,但我实际上有一个函数可以计算挂接到标记的函数
/**
* count_hooked_functions
* @author Ohad Raz
* @param string $tag hook name as string
* @return int the number of hooked functions to a specific hook
*/
function count_hooked_functions($tag = false){
global $wp_filter;
if ($tag){
if (isset($wp_filter[$tag])){
return count($wp_filter[$tag]);
}
}
return 0;
}
但这是一个示例,在这个示例中,单个类将是一个更好的解决方案,而不是反复编写相同的代码,例如:
/**
* my_site_notices
* @author Ohad Raz
*/
class my_site_notices
{
public $notices = array();
public $has_notices = false;
/**
* __construct class constructor
* @author Ohad Raz
*/
function __construct(){
add_action(\'site_notices\',array($this,\'simple_notification\'));
}
/**
* simple_notification
* a funciton which prints the added notification at the site_notices action hook
* @author Ohad Raz
* @access public
* @return Void
*/
public function simple_notification(){
if ($this->has_notices){
foreach ($notices as $n){
echo \'<div>\'.$n.\'</div>\';
}
}
}
/**
* getCount
* @author Ohad Raz
* @access public
* @return int the number of notifications
*/
public function getCount(){
return count($this->notices);
}
/**
* add
* @param string $n a notification to add
* @author Ohad Raz
* @access public
* @return void
*/
public function add($n = \'\'){
$this->notices[] = $n;
}
}//end class
/**
* Usage:
*/
global $my_notices;
$my_notices = new my_site_notices();
//to add a notification
$my_notices->add("this is my first notification");
$my_notices->add("this is my 2nd notification");
$my_notices->add("this is my 3rd notification");
//get the count
$count = $my_notices->getCount();