This website uses cookies to ensure you get the best possible experience. See our Cookies Policy.

PMG Digital Made for Humans

Removing (Or Adding) WordPress Dashboard Widgets

3 MINUTE READ | November 21, 2011

Removing (Or Adding) WordPress Dashboard Widgets

WordPress comes with a bunch of default dashboard widgets. Me? I appreciate them: I’m a WordPress super-nerd, so when I see “Other WordPress News” and such things, the compulsive clicking starts.

When we make a site for a client, however, they don’t need to see that sort of thing. In fact, I’d rather display some things that are more relevant for them.

Dashboard widgets are really just meta boxes, despite them having their own specialized function called wp_add_dashboard_widget. You can check out all the relevant code for the WP dashboard in 

wp-admin/includes/dashboard.php
.To get started we need to hook into wp_dashboard_setup.

add_action( ‘wp_dashboard_setup’, ‘pmg_rm_meta_boxes’ );
function pmg_rm_meta_boxes()
{
    
}

Because dashboard widgets are just meta boxes, we can use the handy remove_meta_box function to get rid of them. Remove meta box, takes three arguments: the widget ID, the page, and the context.

To find those three things, we need to dig into the dashboard code a bit or you can do this:

add_action( ‘wp_dashboard_setup’, ‘pmg_rm_meta_boxes’ );
function pmg_rm_meta_boxes()
{
    global $wp_meta_boxes;
    echo ‘[crayon-5188052b5e3f5 ]’;
    print_r( $wp_meta_boxes );
    echo ‘
‘;
exit();
}
[/crayon]

Which will print out the currently registered meta boxes nice and pretty then stop the script. It spits out something like this.

The root key of that array is dashboard. That’s our page.

remove_meta_box( $id, ‘dashboard’, $context );

The next level down in the array are two more keys: normal and side. Those are our contexts!

remove_meta_box( $id, ‘dashboard’, ‘normal’ );
remove_meta_box( $id, ‘dashboard’, ‘side’ );

The next level down are the actual meta box ID strings. Below is the code to remove every meta box from the WordPress admin dashboard. Each one is commented to tell you which widget is being removed. This can be placed in your functions.php file or in a plugin.

add_action( ‘wp_dashboard_setup’, ‘pmg_rm_meta_boxes’ );
function pmg_rm_meta_boxes()
{
    /**
     * Removes the “Right Now” widget that tells you post/comment counts
     * and what theme you’re using.
     */
    remove_meta_box( ‘dashboard_right_now’, ‘dashboard’, ‘normal’ );
    
    /**
     * Removes the recent comments widget
     */
    remove_meta_box( ‘dashboard_recent_comments’, ‘dashboard’, ‘normal’ );
    
    /**
     * Removes the incoming links widget.
     */
    remove_meta_box( ‘dashboard_incoming_links’, ‘dashboard’, ‘normal’ );
    
    /**
     * Removes the plugins widgets that displays the most popular,
     * newest, and recently updated plugins
     */
    remove_meta_box( ‘dashboard_plugins’, ‘dashboard’, ‘normal’ );
    
    /**
     * Removes the quick press widget that allows you post right from the dashboard
     */
    remove_meta_box( ‘dashboard_quick_press’, ‘dashboard’, ‘side’ );
    
    /**
     * Removes the widget containing the list of recent drafts
     */
    remove_meta_box( ‘dashboard_recent_drafts’, ‘dashboard’, ‘side’ );
    
    /**
     * Removes the “WordPress Blog” widget
     */
    remove_meta_box( ‘dashboard_primary’, ‘dashboard’, ‘side’ );
    
    /**
     * Removes the “Other WordPress News” widget
     */
    remove_meta_box( ‘dashboard_secondary’, ‘dashboard’, ‘side’ );
    
    
}

Adding dashboard widgets is just as easy as removing them. Hook into wp_dashboard_setup again, and call wp_add_dashboard_widget inside your hooked function. wp_add_dashboard_widget takes three arguments (four, actually, the last isn’t required): the widget ID, the meta box title, and the callback function.

add_filter( ‘wp_dashboard_setup’, ‘pmg_add_dashboard_widget’ );
function pmg_add_dashboard_widget()
{
    wp_add_dashboard_widget( ‘pmg-say-hello’, __( ‘Oh, Hi!’ ), ‘pmg_widget_cb’ );
}

Your callback function works just like any other meta box: echo out what you need! You also have access to the entire WordPress API inside your widget callback, so you could do things like fetch RSS feeds, or query external API’s and display the data. This is just a simple “Hello, World!” example.

Stay in touch

Bringing news to you

Subscribe to our newsletter

function pmg_widget_cb()
{    
    // just like any other meta box.  Echo out what you want here!
    echo ‘Hello, World!’;
}


Posted by Christopher Davis

Related Content

thumbnail image

Get Informed

PMG Innovation Challenge Inspires New Alli Technology Solutions

4 MINUTES READ | November 2, 2021

Get Informed

Applying Function Options to Domain Entities in Go

11 MINUTES READ | October 21, 2019

thumbnail image

Get Informed

My Experience Teaching Through Jupyter Notebooks

4 MINUTES READ | September 21, 2019

Get Informed

Trading Symfony’s Form Component for Data Transfer Objects

8 MINUTES READ | September 3, 2019

Get Inspired

Working with an Automation Mindset

5 MINUTES READ | August 22, 2019

Get Informed

Parsing Redshift Logs to Understand Data Usage

7 MINUTES READ | May 6, 2019

Get Inspired

3 Tips for Showing Value in the Tech You Build

5 MINUTES READ | April 24, 2019

thumbnail image

Get Informed

Testing React

13 MINUTES READ | March 12, 2019

Get Inspired

Tips for Designing & Testing Software Without a UX Specialist

4 MINUTES READ | March 6, 2019

Get Informed

A Beginner’s Experience with Terraform

4 MINUTES READ | December 20, 2018

ALL POSTS