WordPress how to assign only necessary capabilities to users

Quite often there is a requirement in wordpress to give certain users access to certain Administrative function e.g. manage widgets but you do not want to make them administrator for obvious reasons

Here are some steps to achieve this requirement

  1. Create some role e.g. widgets_manager (based on the Author role i.e. same as Author role)
  2. Assign edit_theme_options capability to this newly created role
  3. Now just add below code to your functions.php