如何计算附加到操作挂钩上的函数的数量?

时间:2012-12-23 作者:PrivateUser

我有一些这样的行为

    function simple_notification() {
    echo \'<div>This is a notification</div>\';
    }
    add_action( \'site_notices\', \'simple_notification\' );

    function simple_notification2() {
    echo \'<div>This is a notification</div>\';
    }
    add_action( \'site_notices\', \'simple_notification2\' );

    function simple_notification3() {
    echo \'<div>This is a notification</div>\';
    }
    add_action( \'site_notices\', \'simple_notification3\' );
现在我通过调用do_action 在我的模板中

<?php do_action(\'site_notices\'); ?>
我想在名为notifications 有人能告诉我怎么计算吗?

更新:

如您所见,当我使用do_action(\'site_notices\');

所以我想在菜单中显示如下notifications (3)

2 个回复
最合适的回答,由SO网友:Bainternet 整理而成

不要问我怎么做,但我实际上有一个函数可以计算挂接到标记的函数

/**
 * 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();

SO网友:Pontus Abrahamsson

问题相当广泛。但这里有一个示例,说明当meny通知存储在数组中时,它们是如何存在的。。

function wpse_16722_notifications() {
        // Add notifications in an array
        $notifications = array(
            \'This is a notification\',
            \'This is a notification 2\',
            \'This is a notification 3\',
            \'This is a notification 4\'
        );

        // Count theme for the (number of notifications)
        $amount = count( $notifications );

        return __(\'Notifications\', \'domain\') . \' (\'. $amount .\')\';
}
您只需在需要它们的地方回显函数,如下所示:

<?php echo wpse_16722_notifications(); ?>

结束

相关推荐

How to load WP functions?

我有一个mymail.php 发送电子邮件的脚本(我使用require_once( $_SERVER[\'DOCUMENT_ROOT\'] . \'/wp-includes/class-phpmailer.php\' );.) 并输出纯文本字符串(如“电子邮件发送成功”)我想在我的脚本中使用WP中定义的函数。我要调用的特定函数是get_option() 以检索网站所有者的电子邮件用户。要为特定get_option() 函数以及导入整个WP核心的内容(如主题的.php文件可用的上下文)?