Categories:> WordPress

WordPress Admin Dashboard Hacks – Removing stuff

We were developing a custom subscription management plugin for Kamikaze (a cool magazine in Romania) where the subscribed users had to be redirected to the WordPress Dashboard after logging in.

WordPress by default has some small “widgets” that are intended for a tech savvy blogger or for an administrator, not for a basic internet user who barely knows how to check his e-mail. This is why we had to remove these so called widgets from the dashboard, but only for the users and not for the administrators.

Below is a list of these widgets and a quick solution to remove them if they are not needed:

1. Screen options (right top)

This is not a usual removal, but we didn’t want the users to play with this so we removed it.

First open your theme’s functions.php file, where you can put this custom code for tweaking the administration dashboard.

Now we create a function which will be attached to the dashboard using wordpress built in action hooks.

function remove_screenoptions() {
echo '<style type="text/css">#screen-meta-links { display: none; } </style>';
}

There’s no custom function or reference to this widget so we had to remove it by using CSS, therefor hiding the actual div which can trigger the slide-down screen options menu.

Second, we add an action hook to the wordpress admin so it will trigger the function when we log in to the wordpress dashboard

add_action('admin_head', 'remove_screenoptions');

Now we should restrict the action hook to the users only, so we wrap the action code with an if statement:

if ( !current_user_can( 'edit_users' ) ) {
add_action('admin_head', 'remove_screenoptions');
}

This can be also applied to any element in the WordPress admin, by hiding the container div of that element.

2. Removing update notifications for users

This one is a nice improvement for the users if they have to log in to the wordpress dashboard. They don’t need to be bothered with updates and such, so we remove it with another function in the functions.php file of your theme:

function no_update_notification() {
remove_action('admin_notices', 'update_nag', 3);
}

after we’ve created the function we have to hook it with an action to the wordpress admin, but as we did above we wrap an if statement around it which filters this rule only to users and not administrators:

if (!current_user_can('edit_users')) {
add_action('admin_notices', 'no_update_notification', 1);
}

 

3. Removing dashboard widgets

Sometimes we need to remove default dashboard widgets, so the users won’t have access to insights or statistics an administrator should have on it’s dashboard.

For example, we remove the Right Now dashboard widget by creating the following function in the functions.php file our theme:

function example_remove_dashboard_widgets() {
	// Globalize the metaboxes array, this holds all the widgets for wp-admin
 	global $wp_meta_boxes;

	// Remove right now
	unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_right_now']);
}

We hook the function to the WP Dashboard Setup action, and filter it so it executes only for the users:

if (!current_user_can('edit_users')) {
// Hoook into the 'wp_dashboard_setup' action to register our function
add_action('wp_dashboard_setup', 'example_remove_dashboard_widgets' );
}

You can remove as many widgets as you want from the dashboard, by repeating the “unset()” function and changing the last value to the ID of the widget.

You can find the ID of a widget by inspecting the element (widget’s div) with Firebug or Google Chrome’s inspector.

4. Removing Menu items

When you have a user management setup in WordPress, you need to have control over what menu items the users have access to, even if their user level gives them permission to use these items.

Let’s say you’ve create a custom profile for the users and you don’t want them to see or use the default WordPress “Profile” menu in their dashboard. There are two ways to accomplish this in your theme’s function.php file.

a. Custom function

function remove_menus () {
global $menu;
                // We define which menu/submenu items we want to restrict
		$restricted = array(__('Users'),__('Profile'));
		end ($menu);
		while (prev($menu)){
			$value = explode(' ',$menu[key($menu)][0]);
			if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){unset($menu[key($menu)]);}
		}
}
if ( !current_user_can( 'edit_users' ) ) {
add_action('admin_menu', 'remove_menus');
}

In the “$restricted” variable we can define in an array the menus/submenus we want to restrict, you can use multiple values in the array.

b. Using the WordPress Functions

We set the priority to 999 for the action to run at the end of all other actions.

add_action( 'admin_menu', 'devpress_remove_menus', 999 );
function devpress_remove_menus() {
	remove_submenu_page( 'themes.php', 'theme-editor.php' );
}

In this approach you have to define the filename where the menu link points to. You can find these filenames in the “wp-admin/menu-slug.php” file on your WordPress installation.

Hopefully these tweaks will help you set up and control your WordPress Dashboard exactly as you want it.

If you know any other useful hacks or modifications don’t hesitate to post a comment!

Share