implementing plugins loaded action hook in wordpress

My plugin code looks like below,

if (!current_user_can('administrator')){
function hide_post_page_options() {
//global $post;
// Set the display css property to none for add category and add tag functions
$hide_post_options = "<style type="text/css"> .jaxtag { display: none; } #category-adder { display: none; } </style>";
print($hide_post_options);
}
add_action( 'admin_head', 'hide_post_page_options'  );
}

But when I activate I get an error

Read More
Fatal error: Call to undefined function wp_get_current_user()

I could get around this by including the pluggable.php in capabilities.php. But I don’t think doing a change in those files is a better way. Because wp_get_current_user() is a pluggable function it is only available after the plugins are loaded. Is there a way to use this without making changed to core files?

Related posts

Leave a Reply

1 comment

  1. Instead of hiding it with CSS, I would suggest you to remove it from Menu if it’s not admin, that would be a WordPress approach

    add_action('admin_menu', 'dot1_remove_dahsboard_menu', 111);
    
    function dot1_remove_dahsboard_menu(){
       global $submenu, $menu;
    
       //to check array key you want to unset in your case, print the array
       echo "<pre>";
       print_r($menu);
       echo "</pre>";
    
       echo "<pre>";
       print_r($submenu);
       echo "</pre>";
       /* Unset menu array */
       if(  !current_user_can('manage_options')  ){
            unset($menu['10']);
       }
    
       /* Unset submenu array */
      if(  !current_user_can('manage_options')  ){
            unset($submenu['edit.php']['10']);
      }
    }