Function and usage of get_admin_url() in wordpress

Question

Answers ( 1 )

    0
    2024-01-06T12:43:53+00:00

    The get_admin_url() function in WordPress is designed to retrieve the URL of the admin area for a specific site. Here's a detailed explanation of the function and its usage:

    Function: get_admin_url()

    Purpose:

    • Retrieves the URL to the admin area for a given WordPress site.

    Syntax:

    get_admin_url( int|null $blog_id = null, string $path = '', string $scheme = 'admin' ): string
    

    Parameters:

    1. $blog_id (int|null, optional):

      • Description: The ID of the site you want to get the admin URL for.
      • Default: null, which implies the current site.
    2. $path (string, optional):

      • Description: A path to append to the admin URL. It's relative to the admin URL.
      • Default: An empty string '', meaning no additional path is appended.
    3. $scheme (string, optional):

      • Description: Determines the scheme of the URL (http, https, or other).
      • Default: 'admin', which obeys the force_ssl_admin() and is_ssl() settings. If your site is set up to force SSL in the admin area, or if the current connection is HTTPS, this will return an HTTPS URL.

    Return Value:

    • Type: string
    • Description: The function returns the admin URL for the specified site, with any optional path appended. The scheme of the URL (HTTP or HTTPS) is determined based on the $scheme parameter and the site's SSL settings.

    Sample Usage:

    1. Basic Usage (Current Site):

      $admin_url = get_admin_url();
      echo $admin_url; // Outputs the admin URL of the current site.
      
    2. Specifying a Site by ID:

      $site_id = 3;
      $admin_url = get_admin_url($site_id);
      echo $admin_url; // Outputs the admin URL for the site with ID 3.
      
    3. Appending a Path:

      $path = 'post-new.php';
      $admin_url = get_admin_url(null, $path);
      echo $admin_url; // Outputs the URL for creating a new post in the admin area.
      
    4. Forcing HTTPS:

      $admin_url = get_admin_url(null, '', 'https');
      echo $admin_url; // Outputs the admin URL with the HTTPS scheme.
      

    This function is particularly useful for plugin and theme developers who need to generate links to different parts of the WordPress admin area.

Leave an answer