get_dashboard_url

Retrieves the URL to the user's dashboard.

Description

(string) get_dashboard_url( (int) $user_id = 0, (string) $path = '', (string) $scheme = 'admin' ); 

If a user does not belong to any site, the global user dashboard is used. If the user belongs to the current site, the dashboard for the current site is returned. If the user cannot edit the current site, the dashboard to the user's primary site is returned.

Returns (string)

Dashboard URL link with optional path appended.

Parameters (3)

0. $user_id — Optional. (int)
User ID. Defaults to current user.
1. $path — Optional. (string) => ''
Path relative to the dashboard. Use only paths known to both site and user admins. Default empty.
2. $scheme — Optional. (string) => 'admin'
The scheme to use. Default is admin,, which obeys force_ssl_admin() and is_ssl(). http or https can be passed to force those schemes.

Usage

  1. if ( !function_exists( 'get_dashboard_url' ) ) { 
  2. require_once ABSPATH . WPINC . '/link-template.php'; 
  3.  
  4. // Optional. User ID. Defaults to current user. 
  5. $user_id = -1; 
  6.  
  7. // Optional path relative to the dashboard. Use only paths known to 
  8. // both site and user admins. Default empty. 
  9. $path = ''; 
  10.  
  11. // The scheme to use. Default is 'admin', which obeys force_ssl_admin() 
  12. // and is_ssl(). 'http' or 'https' can be passed to force those schemes. 
  13. $scheme = 'admin'; 
  14.  
  15. // NOTICE! Understand what this does before running. 
  16. $result = get_dashboard_url($user_id, $path, $scheme); 
  17.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/link-template.php  
  1. function get_dashboard_url( $user_id = 0, $path = '', $scheme = 'admin' ) { 
  2. $user_id = $user_id ? (int) $user_id : get_current_user_id(); 
  3.  
  4. $blogs = get_blogs_of_user( $user_id ); 
  5. if ( ! is_super_admin() && empty($blogs) ) { 
  6. $url = user_admin_url( $path, $scheme ); 
  7. } elseif ( ! is_multisite() ) { 
  8. $url = admin_url( $path, $scheme ); 
  9. } else { 
  10. $current_blog = get_current_blog_id(); 
  11. if ( $current_blog && ( is_super_admin( $user_id ) || in_array( $current_blog, array_keys( $blogs ) ) ) ) { 
  12. $url = admin_url( $path, $scheme ); 
  13. } else { 
  14. $active = get_active_blog_for_user( $user_id ); 
  15. if ( $active ) 
  16. $url = get_admin_url( $active->blog_id, $path, $scheme ); 
  17. else 
  18. $url = user_admin_url( $path, $scheme ); 
  19.  
  20. /** 
  21. * Filters the dashboard URL for a user. 
  22. * @since 3.1.0 
  23. * @param string $url The complete URL including scheme and path. 
  24. * @param int $user_id The user ID. 
  25. * @param string $path Path relative to the URL. Blank string if no path is specified. 
  26. * @param string $scheme Scheme to give the URL context. Accepts 'http', 'https', 'login',  
  27. * 'login_post', 'admin', 'relative' or null. 
  28. */ 
  29. return apply_filters( 'user_dashboard_url', $url, $user_id, $path, $scheme);