3

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

<?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)

  • What do you want to calculate? – Pontus Abrahamsson Dec 23 '12 at 12:45
  • Please see my update – Giri Dec 23 '12 at 12:49
8

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();
  • This cant be correct, It would count other object as well. I think you need to go one level deeper to catch all hooks with diffrent priorities to begin counting like count($wp_filter['wp_head']->callbacks). But shared priority keys keep the number of callbacks one nestled array deeper... Also if they come from a class like a plugin, the array contains an object, with many callbacks inside that object... – Jonas Lundman Jun 9 '18 at 14:19
0

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(); ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.