Web, WordPress

WordPress 3 Function: Sites in Dashboard menu

Edit on May 26, 2011: With the release of WP 3.1, the admin bar has links to each of your sites. Way easier, but that doesn’t keep this from working. I just won’t make this into a plugin. Also, subdomains need a different treatment!

In the process of creating a WordPress theme for work (trying to move to WP as CMS! YAY!), I realized that the procedure for accessing the Dashboard for a site besides the one you are on can be a little obtuse and involves too much clicking. Since a huge part of the focus and reasoning behind this project is ease of use for content editors (death to Contribute), I thought it would be smart to have direct links to each applicable Dashboard within the Dashboard admin menu. In conjunction with Ozh’s wonderful Admin Drop Down Menu, I have to say that it looks pretty freaking awesome. I put this function in the theme’s functions.php file so that it applies to all network sites, but I wonder if it would be a smart thing to write and release as a plugin. So, here for you to use or rip apart – only mild testing has been done, so don’t be too mean:

/* add dashboard links to each user site underneath the Dashboard admin menu */

function hhs_sites_menu() {
	global $current_user;
	$sites = get_blogs_of_user( $current_user->id );

	// make sure multisite is on, the current user has permissions, and has more than one site
	if ( is_multisite() && current_user_can('read') && count($sites) > 1 ) {
		foreach ( $sites as $site ) {
			// make the URL a relative path; escape twice in case the user is in a network site (subdirectory)
			$relative_admin_url = preg_replace('/http:\/\/www\.yourdomain\.com/', '../..', get_admin_url($site->userblog_id) );

			add_dashboard_page( $site->blogname, $site->blogname, 'read', esc_url($relative_admin_url) );
		}
	}
}

add_action('admin_menu', 'hhs_sites_menu');

I’d like to deal with the URL to each Dashboard in a better way. Escaping up two directories is hacky and ugly and wouldn’t work for somebody on a subdomain multisite installation of WP3. It has to do with the way WP wants an internal page within your current wp-admin – the better way would probably be to use a make the callback function redirect to the appropriate Dashboard. This would also allow me to use CSS to define icons for the drop-down version of the admin menu. I could use CSS now, but the ID that is generated is super ugly (i.e. oamsub_http:_______wp-admin – YUCK). Maybe during work hours tomorrow I’ll chew on it a little more.

If I were to make this into a plugin, I’d want/need to:

  • Make it compatible with both subdomain and subdirectory multisite setups
  • Only allow network activation (makes no sense to only have it activated in one, at least to me)
  • Make it easy for a network admin to assign a custom icon to each site for use wherever applicable (i.e. the admin menu drop-down)

Thoughts? Hesitations?

Standard

Leave a Reply