1

I need to add a count number to a option menu item in the admin sidebar. How is this done?

I found a similar post:

Modifying admin sidebar contents to show pending posts indicator

0

If you mean like this enter image description here

Then here is an handy class to get it done very quickly:

/**
* add_num_to_admin_menu
* @author Ohad Raz <admin@bainternet.info>
*/
class add_num_to_admin_menu
{
    public $menus = array();
    /**
     * Class constructor
     * @author Ohad Raz <admin@bainternet.info>
     * 
     * @param array $args menu => number format
     */
    function __construct($args = array()){
        if (!is_admin())return;
        $this->menus = $args;
        add_filter( 'add_menu_classes', array($this,'_Add_num'));
    }
    /**
     * the actual function that adds the number to the menu
     * 
     * @author Ohad Raz <admin@bainternet.info>
     * @param  array $menu admin menu
     * @return array
     */
    function _Add_num($menu){
        //var_dump($menu);
        foreach ((array)$this->menus as $name => $number) {
            // loop through $menu items, find match, add indicator
            foreach( $menu as $menu_key => $menu_data ) {
                if( $name != $menu_data[2] )
                    continue;
                $menu[$menu_key][0] .= " <span class='update-plugins count-$number'><span class='plugin-count'>" . number_format_i18n($number) . '</span></span>';
            }
        }

        return $menu;
    }
    /**
     * Add a number to a menu
     * 
     * @author Ohad Raz <admin@bainternet.info>
     * @param string $menu  menu to add the number to
     * @param int $number number to add
     */
    function addMenuNum($menu,$number){
        $this->menus[$menu] = $number;
    }
}

Usage:

//create an instance of the class
$obj = new add_num_to_admin_menu();
//add a number to a menu node
$obj->addMenuNum('edit.php',7);
//repeat as many times as needed
$obj->addMenuNum('upload.php',20);
$obj->addMenuNum('link-manager.php',15);
$obj->addMenuNum('users.php',10);

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.