Musings of ErisDS
beta
ErisDS

Recently I worked on a WordPress site where I wanted to be able to customise the dashboard for different user roles. WordPress has the default roles of administrators, editors, authors, contributors and subscribers. It’s not too much to assume you might not want to show your editors or authors all the details of your blog posts etc and placing these short code snippets into your theme’s functions.php file will allow you to do just that.

Clearing up

Lets get rid of the incoming links widget for authors and editors and then clean up some of the other boxes for everyone:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function tidy_dashboard()
{
  global $wp_meta_boxes, $current_user;
 
  // remove incoming links info for authors or editors
  if(in_array('author', $current_user->roles) || in_array('editor', $current_user->roles))
  {
    unset($wp_meta_boxes['dashboard']['normal ']['core']['dashboard_incoming_links']);
  }
   
  // remove the plugins info and news feeds for everyone
  unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']);
  unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);
  unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']);
 
}
//add our function to the dashboard setup hook
add_action('wp_dashboard_setup', 'tidy_dashboard');

Here’s a full list of how to unset each of the current default dashboard widgets:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//Right Now - Comments, Posts, Pages at a glance
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_right_now']);
//Recent Comments
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_comments']);
//Incoming Links
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_incoming_links']);
//Plugins - Popular, New and Recently updated Wordpress Plugins
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']);

//Wordpress Development Blog Feed
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);
//Other Wordpress News Feed
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']);
//Quick Press Form
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_quick_press']);
//Recent Drafts List
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_recent_drafts']);

Add your own – Private pages menu

Perhaps rather than the default information, you have other items you’d like to be on the dashboard for authors or editors etc? I often add a dashboard widget to list private pages. Here’s how:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// function to display widget
function display_dashboard_widget()
{
  //define arguments for WP_Query()
  $qargs = array(
    'post_type'=>'page',
    'post_status'=>'private'
  );
  // perform the query
  $q = new WP_Query();
  $q->query($qargs);

  // setup the content with a list
  $widget_content = '<ul>';
 
  // execute the WP loop
  while ($q->have_posts()) : $q->the_post();
    $widget_content .= '<li><a href="'.get_permalink() .'" rel="bookmark">'. get_the_title() .'</a></li>';
  endwhile;
 
  $widget_content .= '</ul>';
 
  // return the content you want displayed
  return $widget_content;
}

//function to setup widget
function add_dashboard_widgets()
{
  // create a dashboard widget called "private_page_menu_dashboard_widget" with the title "Private Pages Menu" and call our display function to draw it
  wp_add_dashboard_widget('private_page_menu_dashboard_widget', 'Private Pages Menu', 'display_dashboard_widget' );
}

// finally we have to hook our function into the dashboard setup using add_action
add_action('wp_dashboard_setup', 'add_dashboard_widgets');

The only problem with wp_add_dashboard_widget is that it doesn’t allow you to define where on the dashboard the widget is placed. Instead it just places the box at the bottom of the left hand side in the ['normal']['core'] array.

I want my private page menu to display at the top on the right hand side of the dashboard, so I am going to add a few more lines to add_dashboard_widgets() to shuffle the boxes around the way I want them. This will be overridden by user settings if the boxes are dragged around, but insures that my authors and editors see the box in an obvious position when they first login.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
function add_dashboard_widgets()
{
  // don't forget the global to get all dashboard widgets
  global $wp_meta_boxes;
 
  // create a dashboard widget called "private_page_menu_dashboard_widget" with the title "Private Pages Menu" and call our display function to draw it
  wp_add_dashboard_widget('private_page_menu_dashboard_widget', 'Private Pages Menu', 'display_dashboard_widget' );

  // reorder the boxes - first save the left and right columns into variables
  $left_dashboard = $wp_meta_boxes['dashboard']['normal']['core'];
  $right_dashboard = $wp_meta_boxes['dashboard']['side']['core'];

  // take a copy of the new widget from the left column
  $my_widget = array('private_page_menu_dashboard_widget' => $left_dashboard['private_page_menu_dashboard_widget']);
 
  // remove the new widget from the left column
  unset($left_dashboard['private_page_menu_dashboard_widget']);

  // use array_merge so that the new widget is pushed on to the beginning of the right column's array  
  $right_dashboard = array_merge($my_widget, $right_dashboard);

  // finally replace the left and right columns with the new reordered versions
  $wp_meta_boxes['dashboard']['normal']['core'] = $left_dashboard; 
  $wp_meta_boxes['dashboard']['side']['core'] = $right_dashboard;
}

I hope you find these tricks as useful as I do!

Resources

A Note on Snippets: When customising a CMS such as WordPress it is often the simplest pieces of code which are the hardest to either find or remember. These snippets are placed here for my own reference and will hopefully be useful to others. If you find them useful or have any suggestions, please let me know.

Share this...

  •  Add 'Snippet: Wordpress Admin - Tidy Dashboard Widgets By Role' to Del.icio.us
  • Add 'Snippet: Wordpress Admin - Tidy Dashboard Widgets By Role' to Twitter
  • Add 'Snippet: Wordpress Admin - Tidy Dashboard Widgets By Role' to digg
  • Add 'Snippet: Wordpress Admin - Tidy Dashboard Widgets By Role' to FURL
  • Add 'Snippet: Wordpress Admin - Tidy Dashboard Widgets By Role' to reddit
  • Add 'Snippet: Wordpress Admin - Tidy Dashboard Widgets By Role' to Technorati
  • Add 'Snippet: Wordpress Admin - Tidy Dashboard Widgets By Role' to Newsvine
  • Add 'Snippet: Wordpress Admin - Tidy Dashboard Widgets By Role' to Stumble Upon
  • Add 'Snippet: Wordpress Admin - Tidy Dashboard Widgets By Role' to Google Bookmarks
  • Add 'Snippet: Wordpress Admin - Tidy Dashboard Widgets By Role' to FaceBook

Comments

24 Comments to "Snippet: WordPress Admin – Tidy Dashboard Widgets By Role"
  1. 30th Jun

    Laura Kalbag says:

    Very useful! WordPress can get a bit messy with all the plugin authors adding their own feeds.

    Definitely adding this to the bookmarks list.

  2. 24th Aug

    Matt says:

    Great post! Worked like a charm. Can I lose the “add new” under Pages drop down for editors? I don’t want them to have the ability to create new pages.

  3. 24th Aug

    ErisDS says:

    You can, and I have a code sample somewhere. I’ll look at whipping together a post on it this week.

  4. 30th Aug

    ErisDS says:

    @Matt I’ve put together a post on removing the “add new link” – http://bit.ly/d3o3nI

  5. 22nd Sep

    mongobread says:

    Definitely a winner, however I was thinking if it’s possible to tidy dashboard by user instead of role?

  6. 30th Sep

    pluc says:

    Great article ErisDS. I have a client who requested custom dashboard items – actually a full dashboard replacement. Your article gave me everything I needed to do that without having to look everything up myself. Thanks a lot!

  7. 6th Jan

    Dave says:

    You could always use this plugin http://wordpress.org/extend/plugins/dashboard-heaven/

    It allows you to set the visibility of individual widgets on the dashboard by user/role/capability

    Cheers,

  8. 3rd Feb

    Tony says:

    This is absolutely brilliant. I can’t believe I had to read through so many different online entries that only offered mild suggestions before I had the good fortune of tracking this one down.

    Now I’ve managed to clean up my client’s dashboard and then, using ‘iframes’ of all things, include access to a custom built application they use to run their league. The client is a soccer league and they have to continually go to different places to access their information and run their league in addition to logging into their WP-Admin to access the site’s CMS. With your post I’ve been able to dump all the other information that they don’t want, add in my own dashboard widget and then display their league management tool. Perfect.

    Thank you for this.

  9. 12th Mar

    Website Design, Lake Geneva, WI says:

    Oh, this is good stuff. Very good, indeed.

    Karl

  10. 20 Snippets and Hacks to Make Wordpress User-Friendly for your Clients

    28th Apr

  11. Bst Wordpress Hacks to Make User Friendly | 39Articles

    30th Apr

    Bst Wordpress Hacks to Make User Friendly | 39Articles pinged back:

    [...] Source → [...]

  12. 25th May

    Matth says:

    OMG! Thank you.

    I searched far and wide for a say to simply MOVE my excerpts box over to the right column, for no good reason, just my personal preference.

    “if the boxes are dragged around” <—- !!! what really?
    So I tried it and sure enough, that's all I needed to do. This seems like undocumented feature, so thanks for bringing it to my attention.

  13. 26th Aug

    Er.Sandeep Pal Singh Grewal says:

    Q1-I want to Remove tools option like posts in user admin area ? What is solution to this ,is solution is easy or difficult Please give me that?

  14. 7th Sep

    Internet Marketing Company Toronto says:

    This is great! Now I can easily customize my WordPress admin page to specific roles or group of people. I just hope I wont mess up the settings.
    -John Sumner

  15. 9 Snippets to Make WordPress User-Friendly (part 2) | jQuery4u

    30th Oct

    9 Snippets to Make WordPress User-Friendly (part 2) | jQuery4u pinged back:

    [...] This code will get rid of the ‘Incoming Links’ widget for authors and editors and then clean up some of the other boxes for everyone. Source [...]

  16. 26th Dec

    mahabub says:

    thanks for share this coding.

  17. dev.tribalpixel.ch » cool links

    15th Feb

  18. 19 Code Snippets to make WordPress More Manageable | Sky Tech Geek

    27th Apr

  19. Coding makes your blog more manageable. | Jonny Notes

    5th May

  20. wordpress有用代码 | Programer

    7th May

    wordpress有用代码 | Programer pinged back:

    [...] Source [...]

  21. 10th May

    Adam says:

    Thanks! This was a huge help. Nice work!

  22. SMPtheme :: قالب وبلاگ » ۲۰ هَک و ترفند در جهت کاربرپسند کردن وردپرس برای مشتریانتان

    2nd Sep

  23. 12 Useful Twitter Hacks and Snippets For WordPress | AEXT.NET MAGAZINE

    15th Oct

    12 Useful Twitter Hacks and Snippets For WordPress | AEXT.NET MAGAZINE pinged back:

    [...] 12. WordPress Admin Tidy Dashboard Widgets By Role [...]

  24. Spotlight: Wordpress Admin Menu - Remove

    26th Dec

    Spotlight: Wordpress Admin Menu - Remove "Add New" pages or posts link | Musings of ErisDS pinged back:

    [...] in June I posted about tidying up the WordPress admin dashboard. Recently I got a comment on that article asking if the admin menu could be altered for different [...]

Add your thoughts

  • XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>