How to count number of functions attached to an action hook?

I have some actions like this

    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' );

Now i’m displaying those action by calling do_action in my template

Read More
<?php do_action('site_notices'); ?>

I would like to display notice count near a menu link called notifications Can anyone tell me how to calculate it?

Update:

As you can see, three notices displayed when i use do_action('site_notices');

So I want to display in the menu like notifications (3)

Related posts

Leave a Reply

2 comments

  1. Don’t ask me way but i actually have a function to count hooked functions to a tag

    /**
     * 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;
    }
    

    but this is an example where a single class would be a much better solution instead of writing the same code over and over something like:

    /**
    * 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();
    
  2. The question is pretty wide. But here is an example how to show how meny notifications there are when they are stored in an array..

    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 .')';
    }
    

    You just echo the function where you want them like this:

    <?php echo wpse_16722_notifications(); ?>