Function and usage of admin_url() in wordpress


Answers ( 1 )


    The admin_url() function in WordPress is used to retrieve the URL of the administration area (admin panel) for the current site. This function is particularly useful for plugin and theme developers who need to create links to the admin area.

    Function Signature:

    admin_url( string $path = '', string $scheme = 'admin' ): string


    1. $path (string, optional): This parameter is used to specify a path relative to the admin URL. For example, if you want to link to a specific page in the admin area, you can include the path to that page here. The default value is an empty string '', which means that if you don't provide a path, the function will return the URL of the admin area's main page.

    2. $scheme (string, optional): This parameter determines the scheme of the URL. The default value is 'admin'. When set to 'admin', the function respects the settings determined by force_ssl_admin() and is_ssl(). This means it will automatically choose between 'http' or 'https' based on the site's SSL configuration. You can also explicitly set this parameter to 'http' or 'https' to force the respective scheme.

    Return Value:

    • string: The function returns a string that is the admin URL. If a path is provided, it appends this path to the admin URL.

    Example Usage:

    Basic Usage:

    // Get the URL of the WordPress admin area
    $admin_url = admin_url();
    echo 'Admin area URL: ' . $admin_url;

    With a Specific Path:

    // Get the URL of the WordPress Themes page in the admin area
    $themes_url = admin_url('themes.php');
    echo 'Themes page URL: ' . $themes_url;

    Forcing HTTPS Scheme:

    // Get the URL of the admin area with https scheme
    $admin_url_secure = admin_url('', 'https');
    echo 'Secure Admin URL: ' . $admin_url_secure;

    These examples demonstrate how admin_url() can be used in different scenarios to generate links to the WordPress admin area or specific admin pages, with flexibility in terms of the URL scheme.

Leave an answer