Pawan Kumawat

Add Custom Dashboard Widgets in WordPress.

To add custom dashboard widgets in WordPress, you can use the `wp_add_dashboard_widget` function. This function allows you to create and display custom widgets on the WordPress dashboard.

The `wp_add_dashboard_widget` function is used to register and display a custom dashboard widget. It requires three parameters:

1. `$widget_id` (string) – The ID of the widget. This ID must be unique to avoid conflicts with other dashboard widgets. It’s used as a reference for the widget, so make sure to choose a unique and descriptive ID.

2. `$widget_name` (string) – The name or title of the widget. This is the text that will be displayed as the title of the dashboard widget.

3. `$callback` (callable) – The callback function that renders the content of the widget. This function will be responsible for generating the HTML and content to be displayed in the custom dashboard widget. It should accept no arguments as WordPress will pass the necessary data automatically.

Here’s the function signature of `wp_add_dashboard_widget`:

wp_add_dashboard_widget( string $widget_id, string $widget_name, callable $callback );

Now, let’s break down the parameters in detail:

– `$widget_id`: The ID of the custom dashboard widget. It should be a unique string that identifies the widget. For example, `’custom_dashboard_widget’` or `’my_custom_widget’`.

– `$widget_name`: The title or name of the custom dashboard widget. It is the text that will be displayed as the title of the widget on the dashboard.

– `$callback`: The callback function that generates the content of the widget. This function is responsible for rendering the HTML and content that will be displayed inside the widget. The callback function should not accept any arguments, as WordPress will pass the necessary data automatically.

Here’s a step-by-step guide to adding a custom dashboard widget:

1. Open your theme’s `functions.php` file or create a custom plugin file.

2. Use the `wp_add_dashboard_widget` function to create the custom dashboard widget. This function requires two parameters: the widget’s ID and its title.

function custom_dashboard_widget() {
    // Widget content goes here
    echo 'This is a custom dashboard widget content.';

function custom_add_dashboard_widgets() {
    wp_add_dashboard_widget('custom_dashboard_widget', 'Custom Dashboard Widget', 'custom_dashboard_widget');
add_action('wp_dashboard_setup', 'custom_add_dashboard_widgets');


In the above code, we define two functions: `custom_dashboard_widget` for the widget content and `custom_add_dashboard_widgets` to register the widget. We use `wp_add_dashboard_widget` to add the widget with the ID `custom_dashboard_widget` and the title “Custom Dashboard Widget.” The content of the widget is defined in the `custom_dashboard_widget` function.

3. Save the changes and visit the WordPress dashboard.

Now, you’ll see a new widget titled “Custom Dashboard Widget” on the dashboard. The content of the widget will display “This is a custom dashboard widget content.”

You can customize the `custom_dashboard_widget` function to display any content you like. For example, you can display helpful information, links, statistics, or anything else relevant to your site’s administrators.

Please note that the custom dashboard widget is visible to all users with the “manage_options” capability, typically administrators. Other user roles might not be able to see the custom dashboard widget unless their capabilities are explicitly set to include it. If you want to control the visibility of the widget for specific user roles, you can use the `wp_add_dashboard_widget` function along with appropriate user role checks.


Related Blogs

Add Custom Product Data Tab

Learn how to enhance your WooCommerce product editor by adding a custom product data tab. Follow a step-by-step guide using the woocommerce_product_data_tabs filter and the woocommerce_product_data_panels action. Create a custom tab with your desired label, and include your own fields or content within the panel. Customize your product data to collect and display additional information, providing a tailored experience for your WooCommerce products.

How to Remove Product Tabs on Product Page

To remove product tabs on the WooCommerce product page, you can use the `woocommerce_product_tabs` filter. This filter allows you to modify or remove the default product tabs provided by WooCommerce. By removing the tabs you don’t want, you can customize the product page layout to suit your needs.

Add same product to cart twice instead of changing quantity in Cart

By default, WooCommerce allows customers to change the quantity of a product in the cart by updating the quantity input field on the cart page. If the quantity is increased, the product will be added to the cart only once with the updated quantity. However, if you want to allow customers to add the same product multiple times as separate items in the cart, you’ll need to customize the cart behavior.