1

Is there a to hide all dashboard widgets, not remove them? I want my widget to be the first widget but according to the docs there's no way to specify where your widget shows up since it's all "core", so my solution would be to hide all widgets initially. I ran across this but this only hides the welcome widget, I tried to duplicate the hiding of the welcome widget but it didn't work (nothing happened):

function remove_dashboard_widgets() {
    global $wp_meta_boxes;


    //unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_right_now']);
    //unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_quick_press']);
    //unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);
    update_user_meta( get_current_user_id(), 'show_welcome_panel', false );
    update_user_meta( get_current_user_id(), 'dashboard_right_now', false );
    update_user_meta( get_current_user_id(), 'dashboard_quick_press', false );
    //remove_meta_box( 'dashboard_activity', 'dashboard', 'normal');

}
add_action('wp_dashboard_setup', 'remove_dashboard_widgets' );
2

First of all I have to say taht after a quick investigations seems that no 'dashboard_right_now' or 'dashboard_quick_press' usermeta exists... so setting it to false like in you code can't do anything.

That said

according to the docs there's no way to specify where your widget shows up

Docs lie.

When an user logout and then login again, all widgets stay in same position and order, and once there is no magic, that information must be saved somewhere.

Client-side stuff is not a possibility, because even if an user login in a different PC, widgets position doesn't change.. so position and order must me saved in database.

After a quick search, seems there are 2 different options that control dashboard widgets posision:

  • meta-box-order_dashboard
  • metaboxhidden_dashboard

The first one is an array where keys are:

'normal', 'side', 'column3' and 'column4'.

Since WP 3.8, dashoard has 4 columns by default1, and each of these keys refers to one column, the 1st is called 'normal' an the second 'side', I guess, for backward compatibility.

Every key, contain a comma-separed string of the widgets ids that are shown in that column, so if you want to force your widget to be the only widget in first column you have to make 'normal' column contain only your widget id, and move all other widgets to another column, something like this:

function order_dashboard_widgets() {
  $uid =  get_current_user_id();
  // hide welcome panel
  update_user_meta( $uid, 'show_welcome_panel', '0' );
  // add your widget
  wp_add_dashboard_widget(
    'unsocial_widget', 'Antisocial Dashboard Widget',
    function() {
      echo '<h1>I like to be the alone!</h1>';
    }
  );
  $order = (array) get_user_meta( $uid, 'meta-box-order_dashboard', true );
  if ( ! isset( $order['normal'] ) ) $order['normal'] = '';
  if ( $order['normal'] !== 'unsocial_widget' ) {
    $normal = explode( ',', $order['normal'] );
    $unsocial_key = array_search( 'unsocial_widget', $normal, true );
    if ( $unsocial_key !== false ) {
      unset( $normal[$unsocial_key] );
    }
    $order['side'] = implode( ',', $normal ) . ',' . $order['side'];
    $order['normal'] = 'unsocial_widget'; // yours is the only widget in 1st column
    update_user_meta( $uid, 'meta-box-order_dashboard', $order );
  }
}
add_action( 'wp_dashboard_setup', 'order_dashboard_widgets' );

In this way your widget will always be the first and only widget in first column.

Now, to answer you question, yes, is also possible hide all other widgets using 'metaboxhidden_dashboard' user meta: it contains an array of widget ids that must be hidden, so you can fill it with all the dashboard widgets.

function remove_dashboard_widgets() {
  // add your widget
  wp_add_dashboard_widget(
    'unsocial_widget', 'Antisocial Dashboard Widget',
    function() {
      echo '<h1>I like to be the alone!</h1>';
    }
  );
  $uid =  get_current_user_id();
  // hide welcome panel
  update_user_meta( $uid, 'show_welcome_panel', '0' );
  // get the current hidden metaboxes
  $hidden = get_user_meta( $uid, 'metaboxhidden_dashboard' );
  // the metaboxes to be hidden
  $to_hide = array(
    'dashboard_right_now',
    'dashboard_activity',
    'dashboard_quick_press',
    'dashboard_primary'
  );
  // if not already hidden, hide
  if ( $hidden !== $to_hide ) {
    update_user_meta( $uid, 'metaboxhidden_dashboard', $to_hide );
  }
}
add_action( 'wp_dashboard_setup', 'remove_dashboard_widgets' );

However, in this way users will not be able anymore to hide, show or move widgets, because at every login they are again all hidden (but the yours, off course...), this is the reason why I prefer previous solution...

Maybe, if you want to hide all other widgets, you can do it only a at first time an user visit the dashboard, and in all subsequest visits, just order the widgets using previous function. (You can save an user meta on first vist to know if the current has already visited dashboard).

Note: Of course, any other plugin can use same code, so you can never be absolutely sure that the yours is the first widget... after all this answer is public now ;)


1 Using few lines of code is possible to have again possibility to use a custom number of columns but even in that case, the array stay the same: if you allow less than 4 columns the further keys are not deleted.

  • Fantastic GM :D ! One thing I did end up changing, I wrapped the $order ... if ... in an if statement $hidden = get_user_meta( $uid, 'metaboxhidden_dashboard' ); if(!in_array('unsocial_widget', $hidden[0])){$order... so if the user decides to hide the widget, there's not a constant gap at the top of the dashboard widget page. – Howdy_McGee Apr 15 '14 at 15:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.