Adding Custom Dashboard Widget

By default, you would see quite a few Widgets (Quick Draft, At a Glance etc.) on “Dashboard” section of your WordPress site. However, this could also be a great place where you can add your custom Widget. What exactly you can do with such widget that entirely depends on you but the possibilities are endless. Recently, I worked on quite a few projects where I added custom Widgets on Dashboard for my clients.

Today, in this post I will show you how to add custom Dashboard widget. On some of my upcoming tutorials posts I will show some of the usage of such widgets that could be really handy. Definitely the application of such feature is limited to the requirement of your client. However, if you want to implement something special for your site, you should also be able to do that as well. So, lets get started.

First of all, remember that we would need to write two different function for a very basic Dashboard Widget. One for registering the widget itself and second one is to generate the content that needs to be displayed on the widget. Let’s register a Widget first.

// register the dashboard widget
function register_custom_dashboard_widgets() {
   wp_add_dashboard_widget(
      'custom_dashboard_widget', 
      'Custom Dashboard Widget',
      'custom_dashboard_widget_content');} 
add_action('wp_dashboard_setup','register_custom_dashboard_widgets');

As you can see, I created a custom function (register_custom_dashboard_widget) which is being hooked to an add_action function through wp_dashboard_setup. Inside the function I called another function (wp_add_dashboard_widget) to register a dashboard widget and assigned various parameters. The first parameter (custom_dashboard_widget) is the “widget id”, second parameter is the “widget_name” and third one is the callback function (custom_dashboard_widget_content).

This callback function is required to gather or to generate content that we want to display on our widget. Let’s take a look at the function below.

// add content through function
function custom_dashboard_widget_content() {
   echo 'Hello! This is my Custom Dashboard Widget Content'; }

All I am doing here in this function is simply echoing a sentence. Definitely you can get more creative with this function. Now, copy these snippets and add on your theme’s functions.php page.

Now, head towards the “Dashboard” section of your site. You should see a new Widget with “Dashboard Widget” title and the sentence that I added on my callback function. This is an example of a simple Dashboard widget. On my upcoming posts, I will show some of the things that you can do with such widgets. So, stay tuned.

Today In History

  •  
  •  

Comment

Leave a Reply

Note: Convet HTML, PHP, JavaScripts from HTMLify, before posting from comment section.
License: By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution. Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.