• About Us
    • New York
  • Work
  • Capabilities
  • Careers
  • Technology
  • Blog
  • Contact Us
  • About Us
    • New York
  • Work
  • Capabilities
  • Careers
  • Technology
  • Blog
  • Contact Us
November 21, 2011

Removing (Or Adding) WordPress Dashboard Widgets

Posted by Christopher Davis

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.

Removing Dashboard Widgets

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.

PHP
1
2
3
4
5
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:

PHP
1
2
3
4
5
6
7
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.

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

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

PHP
1
2
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.

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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 Widget

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.

PHP
1
2
3
4
5
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.

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

Hello World Dashboard Widget

widgetwordpresswordpress dashboardwordpress widgets
Previous
Next

Latest White Papers

  • Shifting Plans for 2020 & Beyond
  • Game On: How Brands Can Log Into A Diverse Multi-Billion Dollar Industry
  • What CCPA Means For Brands
  • How Google is Improving Consumer Data Privacy
  • Ways to Prepare for the Cookieless Future
  • See all White Papers

Featured Posts

  • Ad Age Names PMG #1 Best Place to Work in 2021
  • MediaPost Names PMG Independent Agency of the Year
  • PMG Client Portfolio Trends During Amazon Prime Day 2020
  • A Closer Look at the Congressional Big Tech Market Power Report
  • What to Know About Reddit

Categories

  • Consumer Insights
  • Content
  • Creative Design
  • Data Analytics
  • Development
  • Digital TV & Video
  • Ecommerce
  • Industry News
  • Local
  • Mobile
  • Paid Search
  • PMG Culture
  • Programmatic & Display
  • SEO
  • Social Media
  • Structured Data
Fort Worth

2845 West 7th Street
Fort Worth, TX 76107

Dallas

3102 Oak Lawn Avenue
Suite 650
Dallas, TX 75219

Austin

823 Congress Avenue
Suite 800
Austin, TX 78701

London

33 Broadwick Street
London
W1F 0DQ

New York

120 East 23rd Street
New York, NY 10010

Get in touch

(817) 420 9970
info@pmg.com

Subscribe to the PMG Newsletter
© 2021 PMG Worldwide, LLC, All Rights Reserved
  • Contact
  • Privacy Policy
 Tweet
 Share
 Tweet
 Share
 Tweet
 Share
 LinkedIn
We and our partners use cookies to personalize content, analyze traffic, and deliver ads. By using our website, you agree to the use of cookies as described in our Cookie Policy.