WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am learning to write WordPress plugin and testing out a simple case: hide the admin bar base on user role. So far I have the following in my plugin file:

$hidemenu= new HideMenu();
class HideMenu
    function hideMenu() {
        add_filter( 'show_admin_bar' , array($this, 'hideAdminBar'));

    function hideAdminBar() {
            if (!(current_user_can("administrator")) 
                            return false;

This is base on the example given in WordPress reference. Any idea why this is not working?

share|improve this question
You need to call the class inside a function. This function has to be attached to the right hook. 'init' for example. Much smarter would be to have a static init method inside the class that's calling the class once. Then handle the rest during your construct method where you add all sort of filters and functions to their appropriate hooks. – kaiser Feb 29 '12 at 2:15
Can you show me in code what you mean? Thanks... – ltfishie Feb 29 '12 at 2:29
Just take a look at php5 classes. Currently you're writing in php4 standard. Also take a look at __construct(). – kaiser Feb 29 '12 at 12:10
up vote 2 down vote accepted

Have you looked at the WP Custom Admin Bar plugin? At the very least it has some code chunks you can deconstruct to point you in the right direction.

share|improve this answer
Thanks for your suggestion. I will take a look there. – ltfishie Feb 29 '12 at 4:53

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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