Creating a custom WordPress admin
// May 26th, 2011 // No Comments » // labs
Have you ever try to customize the wordpress dashboard? I’m sure that you think about it at least once if you made any development with wordpress. Luckily, WordPress’ flexible nature allows for almost every part of it to be easily changed, Dashboard inclusive. The easy way is to create a plugin for it, so let’s take look on how we can creat a plugin first.
Creating a plugin:
The first step is to create a folder at “/wp-content/plugins/”, for example, called “custom-admin”. Save this file inside of the folder and call it custom-admin.php. This file must contain the next code at the begining:
This is the “header” and provides information about the plugin to be viewed on the Plugin Panel. It provides the name, URI of the plugin, the description, author, and version. Now we have the plugin and we have to activate it from the admin panel like any other plugin. And we ready to start developing our plugin.
Custom footer
Now that we are ready to start customazing the dashboard let’s start with something easy, for example, think about adding your own footer. If you want to modify the text you should add a function like this:
function custom_admin_footer() {
echo 'This theme was made by Mr. WordPress.';
}
add_filter('admin_footer', 'custom_admin_footer');
This will replace the default Thank you for creating with WordPress. | Documentation | Feedback wordpress admin footer.
Menu Items
Now, we are going to remove some items from the admin menu with the remove_menu_page() function:
This snippet remove the Links Manager and Tools menu items, and if you want to remove another item, you should change the parameter with the slug of the menu (typically the name of the PHP script for the built in menu items; example: edit-comments.php).
Dashboard widgtes
Another thing that we can do is removing the default dashboard widgtes remove_meta_box():
function custom_admin_remove_dashboard_widgets() {
remove_meta_box( 'dashboard_quick_press', 'dashboard', 'side' );
remove_meta_box( 'dashboard_incoming_links', 'dashboard', 'side' );
}
add_action('wp_dashboard_setup', 'custom_admin_remove_dashboard_widgets' );
This snippet will remove the quick press and the incoming links widget in the home page of the dashboard. Also, we can add a custom widget if you want:
function custom_admin_dashboard_widget_function() {
// Display whatever it is you want to show
echo "Hello World, I'm a great Dashboard Widget";
}
// Create the function use in the action hook
function custom_admin_add_dashboard_widgets() {
wp_add_dashboard_widget('custom_admin_dashboard_widget', 'Example Dashboard Widget', 'custom_admin_dashboard_widget_function');
}
// Hook into the 'wp_dashboard_setup' action to register our other functions
add_action('wp_dashboard_setup', 'custom_admin_add_dashboard_widgets' );
You can find more info in the Dashboard Widgets API page.
Custom CSS for the dashboard
Another useful tip is adding a custom CSS file for the admin panel. This will let you override the default styles of the wordpress dashboard. Here is an example of how you can do it:
function custom_admin_css() {
echo site_url() . "/wp-content/plugins/custom-admin/custom-admin.css";
}
add_action('admin_head','my_wp_admin_css');
This will add a CSS file at the end of the head tag in the HTML, make sure that path is ok for you.
Custom admin based on roles
To finish this post I will show you how you can do different actions depending on the user role. As you know, WordPress uses a concept of Roles, designed to give the blog owner the ability to control and assign what users can and cannot do in the blog. So, we can extend this feature, adding or removing menu items, widgets, css and other stuff. For this, the first step is check the user role. For this example, I will only check if the user has an ‘administrator’ role with the function “current_user_can()”:
In the previous code, we are added a custom widget only if the user is not an administrator. Obviusly this bring us a lot of capabilities to customize the dashboard if you want users with differents roles have some custom widget or only some items in the menu. This is what you need. As I wrote at the begining, WordPress is a very flexible CMS and we put our own limits.
