Upon a fresh installation of WordPress, you are generally given a handful of default admin widgets such as ‘Activity‘, ‘Welcome‘, and ‘Quick Draft‘ as shown below.
Thankfully if these are of no use to you then you can easily hide those widgets using the screen options in the top right-hand corner of your admin screen.
In this article, we are going to show you a very simple method to add your own custom box into the mix so that you can display information that is more relevant to your own needs.
The admin dashboard is the first place you see after login so by no means should be taken for granted. I almost always use custom displays here when building client projects to help them get quick access to the information they need, whether that be the latest euro to pound conversion rate, their daily sales figures, or even quick access to contact details they frequently require, the space is there to use as you please!
Creating The Widget
Creating a widget programmatically requires effectively two functions, the first one will hook into the WordPress dashboard setup process to create the box itself, whilst the second will hold the content we wish to display within that box.
I will start by showing you all of the code required from which we will break it down into segments. This will need to be entered into your theme or plugin functions.php file.
function wpcodetips_admin_date_widget() { wp_add_dashboard_widget( 'wpcodetips_widget_date', 'My WPCodeTips Date Widget' , 'wpcodetips_admin_date_widget_handler' ); } add_action( 'wp_dashboard_setup', 'wpcodetips_admin_date_widget' ); function wpcodetips_admin_date_widget_handler() { echo date('d/m/Y'); }
On line 4 we use the action ‘wp_dashboard_setup‘ to hook in and declare that we wish to create our own dashboard widget inside of the function ‘wpcodetips_admin_date_widget‘, in other terms, we are asking WordPress when you start to set up and build the dashboard could you also pick up this function contents as well.
Next, within our function you will see that on line 2 we call ‘wp_add_dashboard_widget‘ which, as stated in the documentation, takes the following first three arguments:
Widget ID
This is the value that will be used as the ‘id’ attribute of the widget. In our case ‘wpcodetips_widget_date‘.
Widget Name
The title of the widget that will be displayed at the top of the new dashboard box. In our example ‘My WPCodeTips Date Widget‘.
Callback
The name of the function containing the data we wish to display within the dashboard widget box. We have gone for ‘wpcodetips_admin_date_widget_handler‘.
Finally, on line 6 we use our callback function to input the data we wish to display, in our case just a very simple implementation of the PHP date() function to echo out today’s date, but there are no limits as to which kind of data you display here, the world is your oyster!
All being well you should now see your brand new shiny widget within the WordPress admin dashboard, nice work!
Conclusion
Just a short article today, but another example of the vast flexibility offered by WordPress to create websites just the way you want them. Complex client projects can quickly involve a huge amount of data points in a number of different places, the admin dashboard is the perfect spot to bring those all into one easy to access location, all with just a few lines of code!
I hope you have found this tutorial useful and I would love to hear how you have implemented it into your own website! As ever, if you are struggling with adding the feature to your own website project then please leave a comment below and I will try my best to help you get there!