Guide

Add or Remove a Widget Box in WordPress Dashboard Admin Screen

23 AUG 2022

When you login to a WordPress site the dashboard homepage looks similar to this –

There are some widget boxes in the homepage that shows various information on the site and also information from outside.

But for a custom theme or a plugin, you may want to add more information in the dashboard home.

Doing so is quite easy with the wp_dashboard_setup action hook. This hook fires after all the core widgets in the dashboard has been registered.

function okla_add_dashboard_widgets() {
     wp_add_dashboard_widget(
         'okla_dashboard_widget',             // Widget slug.
         esc_html_e( 'Okla Widget', 'okla' ), // Title.
         'okla_dashboard_widget_render'       // Display function.
     ); 
 }
 add_action( 'wp_dashboard_setup', 'okla_add_dashboard_widgets' );
  
 /**
  * Write the function to output the content of our custom dashboard Widget.
  */
 function okla_dashboard_widget_render() {
     // Your custom code goes here
     esc_html_e( "This is a custom widget", "okla" );
 }

The wp_dashboard_setup has a callback function okla_add_dashboard_widgets which is a custom function that can be used to add or remove widget boxes from the dashboard home.

The call to wp_add_dashboard_widget function from inside the callback function is for adding a custom widget box in to the dashboard.

This function takes a slug for the widget as first parameter, then the title of the widget (with an escape function to help in translation of the code) and a display function as the third parameter.

The content of the widget is produced by the okla_dashboard_widget_render function.

In this example, the widget just shows some text “This is a custom widget” but you can add anything you want.

The wp_dashboard_setup can also be used to remove widget boxes from the dashboard.

The function used for this purpose is remove_meta_box. You can use this inbuilt function inside the custom callback function of wp_dashboard_setup hook or inside the already existing callback function.

add_action( 'wp_dashboard_setup', 'okla_remove_dashboard_widgets' );
function okla_remove_dashboard_widgets() {
    remove_meta_box('dashboard_activity', 'dashboard', 'normal'); // Activity Widget 
}

The code above removes the Activity widget box from dashboard.

If you want, you can remove all default widget boxes from the dashboard home page using this remove_meta_box function. You just know to know the slug/id of the meta box you are removing.

Another thing is – you can put this code in your themes function.php file if you want add/remove actions to be theme specific or put in a plugin file if you want it to be site specific.

Every business deserves a website
हर एक बिजनेस के लिए एक वेबसाइट होना जरूरी है

GET YOUR'S NOW

Address

411 Kalikapur

Ishani Apartments, Kolkata - 99

Contacts

support@webdesignvista.com
Phone: +91 9903490874
Fax: +91 9903490874