Adding Custom Dashboard Widgets to WordPress

Posted on 20th June 2023

Adding Custom Dashboard Widgets to WordPress

One of the great things about WordPress is its extensibility. Developers can easily create custom plugins and themes to add new features and functionality to WordPress websites.

In this article, we’ll show you how to create custom dashboard widgets for WordPress. Dashboard widgets are those little boxes that appear on the main dashboard screen of your WordPress site. They give you quick access to information and settings that you might need to use on a regular basis.

Creating a custom dashboard widget is a great way to improve the user experience of your WordPress site. It can also help you promote your other plugins or products, or even just show off your latest blog post.

There are two parts to creating a custom dashboard widget: the PHP code that goes in your plugin or theme, and the JavaScript code that goes in youradmin.js file. Let’s take a look at each of these in turn.

The PHP Code

The first thing you need to do is write the PHP code for your dashboard widget. This code will go in your plugin or theme.

We’ll start by creating a function that registers our widget. This function will give our widget a name and an ID, and it will tell WordPress where to find the JavaScript file that we’ll create later.

function wp_dashboard_widget_register() {
wp_register_script( ‘wp-dashboard-widget-js’, plugins_url( ‘admin.js’, __FILE__ ), array( ‘jquery’ ) );
wp_register_style( ‘wp-dashboard-widget-css’, plugins_url( ‘admin.css’, __FILE__ ) );
wp_enqueue_script( ‘wp-dashboard-widget-js’ );
wp_enqueue_style( ‘wp-dashboard-widget-css’ );
}
add_action( ‘admin_enqueue_scripts’, ‘wp_dashboard_widget_register’ );

The next thing we need to do is create the function that will actually output our widget to the dashboard. This function will take two arguments: the first is the id of the widget, and the second is the name of the widget.

function wp_dashboard_widget_output( $widget_id, $widget_name ) {
// output our widget content here
}

Finally, we need to tell WordPress to use our output function when it renders the dashboard. We do this by hooking into the ‘wp_dashboard_setup’ action and passing our function as a callback.

function wp_dashboard_widget_setup() {
wp_add_dashboard_widget( ‘wp_dashboard_widget’, ‘My Widget’, ‘wp_dashboard_widget_output’, $control_callback = null );
}
add_action( ‘wp_dashboard_setup’, ‘wp_dashboard_widget_setup’ );

That’s all the PHP code we need. Now let’s take a look at the JavaScript.

The JavaScript Code

The JavaScript code for our dashboard widget is going to be very simple. All we need to do is write a function that outputs our widget content to the dashboard.

We’ll start by getting a reference to the element that our widget will be rendered into. We do this by calling the WP_Widget object with our widget id.

var widget = WP_Widget( ‘wp_dashboard_widget’ );

Next, we need to define our output function. This function will take two arguments: the first is the id of our widget, and the second is the name of our widget.

function wp_dashboard_widget_output( widget_id, widget_name ) {
// output our widget content here
}

Finally, we need to tell WordPress to use our output function when it renders the dashboard. We do this by hooking into the ‘wp_dashboard_setup’ action and passing our function as a callback.

add_action( ‘wp_dashboard_setup’, ‘wp_dashboard_widget_output’ );

And that’s all the JavaScript we need.

Putting It All Together

Now that we’ve seen the PHP and JavaScript code for our custom dashboard widget, let’s put it all together and see how it works.

The first thing we need to do is write the PHP code for our dashboard widget. This code will go in your plugin or theme.

function wp_dashboard_widget_register() {
wp_register_script( ‘wp-dashboard-widget-js’, plugins_url( ‘admin.js’, __FILE__ ), array( ‘jquery’ ) );
wp_register_style( ‘wp-dashboard-widget-css’, plugins_url( ‘admin.css’, __FILE__ ) );
wp_enqueue_script( ‘wp-dashboard-widget-js’ );
wp_enqueue_style( ‘wp-dashboard-widget-css’ );
}
add_action( ‘admin_enqueue_scripts’, ‘wp_dashboard_widget_register’ );

The next thing we need to do is create the function that will actually output our widget to the dashboard. This function will take two arguments: the first is the id of the widget, and the second is the name of the widget.

function wp_dashboard_widget_output( $widget_id, $widget_name ) {
// output our widget content here
}

Finally, we need to tell WordPress to use our output function when it renders the dashboard. We do this by hooking into the ‘wp_dashboard_setup’ action and passing our function as a callback.

function wp_dashboard_widget_setup() {
wp_add_dashboard_widget( ‘wp_dashboard_widget’, ‘My Widget’, ‘wp_dashboard_widget_output’, $control_callback = null );
}
add_action( ‘wp_dashboard_setup’, ‘wp_dashboard_widget_setup’ );

That’s all the PHP code we need. Now let’s take a look at the JavaScript.

The JavaScript code for our dashboard widget is going to be very simple. All we need to do is write a function that outputs our widget content to the dashboard.

We’ll start by getting a reference to the element that our widget will be rendered into. We do this by calling the WP_Widget object with our widget id.

var widget = WP_Widget( ‘wp_dashboard_widget’ );

Next, we need to define our output function. This function will take two arguments: the first is the id of our widget, and the second is the name of our widget.

function wp_dashboard_widget_output( widget_id, widget_name ) {
// output our widget content here
}

Finally, we need to tell WordPress to use our output function when it renders the dashboard. We do this by hooking into the ‘wp_dashboard_setup’ action and passing our function as a callback.

add_action( ‘wp_dashboard_setup’, ‘wp_dashboard_widget_output’ );

And that’s all the JavaScript we need.

Putting It All Together

Now that we’ve seen the PHP and JavaScript code for our custom dashboard widget, let’s put it all together and see how it works.

The first thing we need to do is write the PHP code for our dashboard widget. This code will go in your plugin or theme.

function wp_dashboard_widget_register() {
wp_register_script( ‘wp-dashboard-widget-js’, plugins_url( ‘admin.js’, __FILE__ ), array( ‘jquery’ ) );
wp_register_style( ‘wp-dashboard-widget-css’, plugins_url( ‘admin.css’, __FILE__ ) );
wp_enqueue_script( ‘wp-dashboard-widget-js’ );
wp_enqueue_style( ‘wp-dashboard-widget-css’ );
}
add_action( ‘admin_enqueue_scripts’, ‘wp_dashboard_widget_register’ );

The next thing we need to do is create the function that will actually output our widget to the dashboard. This function will take two arguments: the first is the id of the widget, and the second is the name of the widget.

function wp_dashboard_widget_output( $widget_id, $widget_name ) {
// output our widget content here
}

Finally, we need to tell WordPress to use our output function when it renders the dashboard. We do this by hooking into the ‘wp_dashboard_setup’ action and passing our function as a callback.

function wp_dashboard_widget_setup() {
wp_add_dashboard_widget( ‘wp_dashboard_widget’, ‘My Widget’, ‘wp_dashboard_widget_output’, $control_callback = null );
}
add_action( ‘