get_rest_url

The timeline below displays how wordpress function get_rest_url has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.3

/**
 * Retrieves the URL to a REST endpoint on a site.
 *
 * Note: The returned URL is NOT escaped.
 *
 * @since 4.4.0
 *
 * @todo Check if this is even necessary
 * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
 *
 * @param int|null $blog_id Optional. Blog ID. Default of null returns URL for current blog.
 * @param string   $path    Optional. REST route. Default '/'.
 * @param string   $scheme  Optional. Sanitization scheme. Default 'rest'.
 * @return string Full URL to the endpoint.
 */
function get_rest_url($blog_id = null, $path = '/', $scheme = 'rest')
{
    if (empty($path)) {
        $path = '/';
    }
    $path = '/' . ltrim($path, '/');
    if (is_multisite() && get_blog_option($blog_id, 'permalink_structure') || get_option('permalink_structure')) {
        global $wp_rewrite;
        if ($wp_rewrite->using_index_permalinks()) {
            $url = get_home_url($blog_id, $wp_rewrite->index . '/' . rest_get_url_prefix(), $scheme);
        } else {
            $url = get_home_url($blog_id, rest_get_url_prefix(), $scheme);
        }
        $url .= $path;
    } else {
        $url = trailingslashit(get_home_url($blog_id, '', $scheme));
        /*
         * nginx only allows HTTP/1.0 methods when redirecting from / to /index.php.
         * To work around this, we manually add index.php to the URL, avoiding the redirect.
         */
        if (!str_ends_with($url, 'index.php')) {
            $url .= 'index.php';
        }
        $url = add_query_arg('rest_route', $path, $url);
    }
    if (is_ssl() && isset($_SERVER['SERVER_NAME'])) {
        // If the current host is the same as the REST URL host, force the REST URL scheme to HTTPS.
        if (parse_url(get_home_url($blog_id), PHP_URL_HOST) === $_SERVER['SERVER_NAME']) {
            $url = set_url_scheme($url, 'https');
        }
    }
    if (is_admin() && force_ssl_admin()) {
        /*
         * In this situation the home URL may be http:, and `is_ssl()` may be false,
         * but the admin is served over https: (one way or another), so REST API usage
         * will be blocked by browsers unless it is also served over HTTPS.
         */
        $url = set_url_scheme($url, 'https');
    }
    /**
     * Filters the REST URL.
     *
     * Use this filter to adjust the url returned by the get_rest_url() function.
     *
     * @since 4.4.0
     *
     * @param string   $url     REST URL.
     * @param string   $path    REST route.
     * @param int|null $blog_id Blog ID.
     * @param string   $scheme  Sanitization scheme.
     */
    return apply_filters('rest_url', $url, $path, $blog_id, $scheme);
}

WordPress Version: 5.7

/**
 * Retrieves the URL to a REST endpoint on a site.
 *
 * Note: The returned URL is NOT escaped.
 *
 * @since 4.4.0
 *
 * @todo Check if this is even necessary
 * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
 *
 * @param int|null $blog_id Optional. Blog ID. Default of null returns URL for current blog.
 * @param string   $path    Optional. REST route. Default '/'.
 * @param string   $scheme  Optional. Sanitization scheme. Default 'rest'.
 * @return string Full URL to the endpoint.
 */
function get_rest_url($blog_id = null, $path = '/', $scheme = 'rest')
{
    if (empty($path)) {
        $path = '/';
    }
    $path = '/' . ltrim($path, '/');
    if (is_multisite() && get_blog_option($blog_id, 'permalink_structure') || get_option('permalink_structure')) {
        global $wp_rewrite;
        if ($wp_rewrite->using_index_permalinks()) {
            $url = get_home_url($blog_id, $wp_rewrite->index . '/' . rest_get_url_prefix(), $scheme);
        } else {
            $url = get_home_url($blog_id, rest_get_url_prefix(), $scheme);
        }
        $url .= $path;
    } else {
        $url = trailingslashit(get_home_url($blog_id, '', $scheme));
        // nginx only allows HTTP/1.0 methods when redirecting from / to /index.php.
        // To work around this, we manually add index.php to the URL, avoiding the redirect.
        if ('index.php' !== substr($url, 9)) {
            $url .= 'index.php';
        }
        $url = add_query_arg('rest_route', $path, $url);
    }
    if (is_ssl() && isset($_SERVER['SERVER_NAME'])) {
        // If the current host is the same as the REST URL host, force the REST URL scheme to HTTPS.
        if (parse_url(get_home_url($blog_id), PHP_URL_HOST) === $_SERVER['SERVER_NAME']) {
            $url = set_url_scheme($url, 'https');
        }
    }
    if (is_admin() && force_ssl_admin()) {
        /*
         * In this situation the home URL may be http:, and `is_ssl()` may be false,
         * but the admin is served over https: (one way or another), so REST API usage
         * will be blocked by browsers unless it is also served over HTTPS.
         */
        $url = set_url_scheme($url, 'https');
    }
    /**
     * Filters the REST URL.
     *
     * Use this filter to adjust the url returned by the get_rest_url() function.
     *
     * @since 4.4.0
     *
     * @param string   $url     REST URL.
     * @param string   $path    REST route.
     * @param int|null $blog_id Blog ID.
     * @param string   $scheme  Sanitization scheme.
     */
    return apply_filters('rest_url', $url, $path, $blog_id, $scheme);
}

WordPress Version: 5.4

/**
 * Retrieves the URL to a REST endpoint on a site.
 *
 * Note: The returned URL is NOT escaped.
 *
 * @since 4.4.0
 *
 * @todo Check if this is even necessary
 * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
 *
 * @param int    $blog_id Optional. Blog ID. Default of null returns URL for current blog.
 * @param string $path    Optional. REST route. Default '/'.
 * @param string $scheme  Optional. Sanitization scheme. Default 'rest'.
 * @return string Full URL to the endpoint.
 */
function get_rest_url($blog_id = null, $path = '/', $scheme = 'rest')
{
    if (empty($path)) {
        $path = '/';
    }
    $path = '/' . ltrim($path, '/');
    if (is_multisite() && get_blog_option($blog_id, 'permalink_structure') || get_option('permalink_structure')) {
        global $wp_rewrite;
        if ($wp_rewrite->using_index_permalinks()) {
            $url = get_home_url($blog_id, $wp_rewrite->index . '/' . rest_get_url_prefix(), $scheme);
        } else {
            $url = get_home_url($blog_id, rest_get_url_prefix(), $scheme);
        }
        $url .= $path;
    } else {
        $url = trailingslashit(get_home_url($blog_id, '', $scheme));
        // nginx only allows HTTP/1.0 methods when redirecting from / to /index.php.
        // To work around this, we manually add index.php to the URL, avoiding the redirect.
        if ('index.php' !== substr($url, 9)) {
            $url .= 'index.php';
        }
        $url = add_query_arg('rest_route', $path, $url);
    }
    if (is_ssl() && isset($_SERVER['SERVER_NAME'])) {
        // If the current host is the same as the REST URL host, force the REST URL scheme to HTTPS.
        if (parse_url(get_home_url($blog_id), PHP_URL_HOST) === $_SERVER['SERVER_NAME']) {
            $url = set_url_scheme($url, 'https');
        }
    }
    if (is_admin() && force_ssl_admin()) {
        /*
         * In this situation the home URL may be http:, and `is_ssl()` may be false,
         * but the admin is served over https: (one way or another), so REST API usage
         * will be blocked by browsers unless it is also served over HTTPS.
         */
        $url = set_url_scheme($url, 'https');
    }
    /**
     * Filters the REST URL.
     *
     * Use this filter to adjust the url returned by the get_rest_url() function.
     *
     * @since 4.4.0
     *
     * @param string $url     REST URL.
     * @param string $path    REST route.
     * @param int    $blog_id Blog ID.
     * @param string $scheme  Sanitization scheme.
     */
    return apply_filters('rest_url', $url, $path, $blog_id, $scheme);
}

WordPress Version: 5.3

/**
 * Retrieves the URL to a REST endpoint on a site.
 *
 * Note: The returned URL is NOT escaped.
 *
 * @since 4.4.0
 *
 * @todo Check if this is even necessary
 * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
 *
 * @param int    $blog_id Optional. Blog ID. Default of null returns URL for current blog.
 * @param string $path    Optional. REST route. Default '/'.
 * @param string $scheme  Optional. Sanitization scheme. Default 'rest'.
 * @return string Full URL to the endpoint.
 */
function get_rest_url($blog_id = null, $path = '/', $scheme = 'rest')
{
    if (empty($path)) {
        $path = '/';
    }
    $path = '/' . ltrim($path, '/');
    if (is_multisite() && get_blog_option($blog_id, 'permalink_structure') || get_option('permalink_structure')) {
        global $wp_rewrite;
        if ($wp_rewrite->using_index_permalinks()) {
            $url = get_home_url($blog_id, $wp_rewrite->index . '/' . rest_get_url_prefix(), $scheme);
        } else {
            $url = get_home_url($blog_id, rest_get_url_prefix(), $scheme);
        }
        $url .= $path;
    } else {
        $url = trailingslashit(get_home_url($blog_id, '', $scheme));
        // nginx only allows HTTP/1.0 methods when redirecting from / to /index.php
        // To work around this, we manually add index.php to the URL, avoiding the redirect.
        if ('index.php' !== substr($url, 9)) {
            $url .= 'index.php';
        }
        $url = add_query_arg('rest_route', $path, $url);
    }
    if (is_ssl() && isset($_SERVER['SERVER_NAME'])) {
        // If the current host is the same as the REST URL host, force the REST URL scheme to HTTPS.
        if ($_SERVER['SERVER_NAME'] === parse_url(get_home_url($blog_id), PHP_URL_HOST)) {
            $url = set_url_scheme($url, 'https');
        }
    }
    if (is_admin() && force_ssl_admin()) {
        // In this situation the home URL may be http:, and `is_ssl()` may be
        // false, but the admin is served over https: (one way or another), so
        // REST API usage will be blocked by browsers unless it is also served
        // over HTTPS.
        $url = set_url_scheme($url, 'https');
    }
    /**
     * Filters the REST URL.
     *
     * Use this filter to adjust the url returned by the get_rest_url() function.
     *
     * @since 4.4.0
     *
     * @param string $url     REST URL.
     * @param string $path    REST route.
     * @param int    $blog_id Blog ID.
     * @param string $scheme  Sanitization scheme.
     */
    return apply_filters('rest_url', $url, $path, $blog_id, $scheme);
}

WordPress Version: 5.0

/**
 * Retrieves the URL to a REST endpoint on a site.
 *
 * Note: The returned URL is NOT escaped.
 *
 * @since 4.4.0
 *
 * @todo Check if this is even necessary
 * @global WP_Rewrite $wp_rewrite
 *
 * @param int    $blog_id Optional. Blog ID. Default of null returns URL for current blog.
 * @param string $path    Optional. REST route. Default '/'.
 * @param string $scheme  Optional. Sanitization scheme. Default 'rest'.
 * @return string Full URL to the endpoint.
 */
function get_rest_url($blog_id = null, $path = '/', $scheme = 'rest')
{
    if (empty($path)) {
        $path = '/';
    }
    $path = '/' . ltrim($path, '/');
    if (is_multisite() && get_blog_option($blog_id, 'permalink_structure') || get_option('permalink_structure')) {
        global $wp_rewrite;
        if ($wp_rewrite->using_index_permalinks()) {
            $url = get_home_url($blog_id, $wp_rewrite->index . '/' . rest_get_url_prefix(), $scheme);
        } else {
            $url = get_home_url($blog_id, rest_get_url_prefix(), $scheme);
        }
        $url .= $path;
    } else {
        $url = trailingslashit(get_home_url($blog_id, '', $scheme));
        // nginx only allows HTTP/1.0 methods when redirecting from / to /index.php
        // To work around this, we manually add index.php to the URL, avoiding the redirect.
        if ('index.php' !== substr($url, 9)) {
            $url .= 'index.php';
        }
        $url = add_query_arg('rest_route', $path, $url);
    }
    if (is_ssl()) {
        // If the current host is the same as the REST URL host, force the REST URL scheme to HTTPS.
        if ($_SERVER['SERVER_NAME'] === parse_url(get_home_url($blog_id), PHP_URL_HOST)) {
            $url = set_url_scheme($url, 'https');
        }
    }
    if (is_admin() && force_ssl_admin()) {
        // In this situation the home URL may be http:, and `is_ssl()` may be
        // false, but the admin is served over https: (one way or another), so
        // REST API usage will be blocked by browsers unless it is also served
        // over HTTPS.
        $url = set_url_scheme($url, 'https');
    }
    /**
     * Filters the REST URL.
     *
     * Use this filter to adjust the url returned by the get_rest_url() function.
     *
     * @since 4.4.0
     *
     * @param string $url     REST URL.
     * @param string $path    REST route.
     * @param int    $blog_id Blog ID.
     * @param string $scheme  Sanitization scheme.
     */
    return apply_filters('rest_url', $url, $path, $blog_id, $scheme);
}

WordPress Version: 8.1

/**
 * Retrieves the URL to a REST endpoint on a site.
 *
 * Note: The returned URL is NOT escaped.
 *
 * @since 4.4.0
 *
 * @todo Check if this is even necessary
 * @global WP_Rewrite $wp_rewrite
 *
 * @param int    $blog_id Optional. Blog ID. Default of null returns URL for current blog.
 * @param string $path    Optional. REST route. Default '/'.
 * @param string $scheme  Optional. Sanitization scheme. Default 'rest'.
 * @return string Full URL to the endpoint.
 */
function get_rest_url($blog_id = null, $path = '/', $scheme = 'rest')
{
    if (empty($path)) {
        $path = '/';
    }
    if (is_multisite() && get_blog_option($blog_id, 'permalink_structure') || get_option('permalink_structure')) {
        global $wp_rewrite;
        if ($wp_rewrite->using_index_permalinks()) {
            $url = get_home_url($blog_id, $wp_rewrite->index . '/' . rest_get_url_prefix(), $scheme);
        } else {
            $url = get_home_url($blog_id, rest_get_url_prefix(), $scheme);
        }
        $url .= '/' . ltrim($path, '/');
    } else {
        $url = trailingslashit(get_home_url($blog_id, '', $scheme));
        // nginx only allows HTTP/1.0 methods when redirecting from / to /index.php
        // To work around this, we manually add index.php to the URL, avoiding the redirect.
        if ('index.php' !== substr($url, 9)) {
            $url .= 'index.php';
        }
        $path = '/' . ltrim($path, '/');
        $url = add_query_arg('rest_route', $path, $url);
    }
    if (is_ssl()) {
        // If the current host is the same as the REST URL host, force the REST URL scheme to HTTPS.
        if ($_SERVER['SERVER_NAME'] === parse_url(get_home_url($blog_id), PHP_URL_HOST)) {
            $url = set_url_scheme($url, 'https');
        }
    }
    if (is_admin() && force_ssl_admin()) {
        // In this situation the home URL may be http:, and `is_ssl()` may be
        // false, but the admin is served over https: (one way or another), so
        // REST API usage will be blocked by browsers unless it is also served
        // over HTTPS.
        $url = set_url_scheme($url, 'https');
    }
    /**
     * Filters the REST URL.
     *
     * Use this filter to adjust the url returned by the get_rest_url() function.
     *
     * @since 4.4.0
     *
     * @param string $url     REST URL.
     * @param string $path    REST route.
     * @param int    $blog_id Blog ID.
     * @param string $scheme  Sanitization scheme.
     */
    return apply_filters('rest_url', $url, $path, $blog_id, $scheme);
}

WordPress Version: 4.8

/**
 * Retrieves the URL to a REST endpoint on a site.
 *
 * Note: The returned URL is NOT escaped.
 *
 * @since 4.4.0
 *
 * @todo Check if this is even necessary
 * @global WP_Rewrite $wp_rewrite
 *
 * @param int    $blog_id Optional. Blog ID. Default of null returns URL for current blog.
 * @param string $path    Optional. REST route. Default '/'.
 * @param string $scheme  Optional. Sanitization scheme. Default 'rest'.
 * @return string Full URL to the endpoint.
 */
function get_rest_url($blog_id = null, $path = '/', $scheme = 'rest')
{
    if (empty($path)) {
        $path = '/';
    }
    if (is_multisite() && get_blog_option($blog_id, 'permalink_structure') || get_option('permalink_structure')) {
        global $wp_rewrite;
        if ($wp_rewrite->using_index_permalinks()) {
            $url = get_home_url($blog_id, $wp_rewrite->index . '/' . rest_get_url_prefix(), $scheme);
        } else {
            $url = get_home_url($blog_id, rest_get_url_prefix(), $scheme);
        }
        $url .= '/' . ltrim($path, '/');
    } else {
        $url = trailingslashit(get_home_url($blog_id, '', $scheme));
        $path = '/' . ltrim($path, '/');
        $url = add_query_arg('rest_route', $path, $url);
    }
    if (is_ssl()) {
        // If the current host is the same as the REST URL host, force the REST URL scheme to HTTPS.
        if ($_SERVER['SERVER_NAME'] === parse_url(get_home_url($blog_id), PHP_URL_HOST)) {
            $url = set_url_scheme($url, 'https');
        }
    }
    if (is_admin() && force_ssl_admin()) {
        // In this situation the home URL may be http:, and `is_ssl()` may be
        // false, but the admin is served over https: (one way or another), so
        // REST API usage will be blocked by browsers unless it is also served
        // over HTTPS.
        $url = set_url_scheme($url, 'https');
    }
    /**
     * Filters the REST URL.
     *
     * Use this filter to adjust the url returned by the get_rest_url() function.
     *
     * @since 4.4.0
     *
     * @param string $url     REST URL.
     * @param string $path    REST route.
     * @param int    $blog_id Blog ID.
     * @param string $scheme  Sanitization scheme.
     */
    return apply_filters('rest_url', $url, $path, $blog_id, $scheme);
}

WordPress Version: 4.7

/**
 * Retrieves the URL to a REST endpoint on a site.
 *
 * Note: The returned URL is NOT escaped.
 *
 * @since 4.4.0
 *
 * @todo Check if this is even necessary
 * @global WP_Rewrite $wp_rewrite
 *
 * @param int    $blog_id Optional. Blog ID. Default of null returns URL for current blog.
 * @param string $path    Optional. REST route. Default '/'.
 * @param string $scheme  Optional. Sanitization scheme. Default 'rest'.
 * @return string Full URL to the endpoint.
 */
function get_rest_url($blog_id = null, $path = '/', $scheme = 'rest')
{
    if (empty($path)) {
        $path = '/';
    }
    if (is_multisite() && get_blog_option($blog_id, 'permalink_structure') || get_option('permalink_structure')) {
        global $wp_rewrite;
        if ($wp_rewrite->using_index_permalinks()) {
            $url = get_home_url($blog_id, $wp_rewrite->index . '/' . rest_get_url_prefix(), $scheme);
        } else {
            $url = get_home_url($blog_id, rest_get_url_prefix(), $scheme);
        }
        $url .= '/' . ltrim($path, '/');
    } else {
        $url = trailingslashit(get_home_url($blog_id, '', $scheme));
        $path = '/' . ltrim($path, '/');
        $url = add_query_arg('rest_route', $path, $url);
    }
    if (is_ssl()) {
        // If the current host is the same as the REST URL host, force the REST URL scheme to HTTPS.
        if ($_SERVER['SERVER_NAME'] === parse_url(get_home_url($blog_id), PHP_URL_HOST)) {
            $url = set_url_scheme($url, 'https');
        }
    }
    /**
     * Filters the REST URL.
     *
     * Use this filter to adjust the url returned by the get_rest_url() function.
     *
     * @since 4.4.0
     *
     * @param string $url     REST URL.
     * @param string $path    REST route.
     * @param int    $blog_id Blog ID.
     * @param string $scheme  Sanitization scheme.
     */
    return apply_filters('rest_url', $url, $path, $blog_id, $scheme);
}

WordPress Version: 4.6

/**
 * Retrieves the URL to a REST endpoint on a site.
 *
 * Note: The returned URL is NOT escaped.
 *
 * @since 4.4.0
 *
 * @todo Check if this is even necessary
 *
 * @param int    $blog_id Optional. Blog ID. Default of null returns URL for current blog.
 * @param string $path    Optional. REST route. Default '/'.
 * @param string $scheme  Optional. Sanitization scheme. Default 'rest'.
 * @return string Full URL to the endpoint.
 */
function get_rest_url($blog_id = null, $path = '/', $scheme = 'rest')
{
    if (empty($path)) {
        $path = '/';
    }
    if (is_multisite() && get_blog_option($blog_id, 'permalink_structure') || get_option('permalink_structure')) {
        $url = get_home_url($blog_id, rest_get_url_prefix(), $scheme);
        $url .= '/' . ltrim($path, '/');
    } else {
        $url = trailingslashit(get_home_url($blog_id, '', $scheme));
        $path = '/' . ltrim($path, '/');
        $url = add_query_arg('rest_route', $path, $url);
    }
    if (is_ssl()) {
        // If the current host is the same as the REST URL host, force the REST URL scheme to HTTPS.
        if ($_SERVER['SERVER_NAME'] === parse_url(get_home_url($blog_id), PHP_URL_HOST)) {
            $url = set_url_scheme($url, 'https');
        }
    }
    /**
     * Filters the REST URL.
     *
     * Use this filter to adjust the url returned by the get_rest_url() function.
     *
     * @since 4.4.0
     *
     * @param string $url     REST URL.
     * @param string $path    REST route.
     * @param int    $blog_id Blog ID.
     * @param string $scheme  Sanitization scheme.
     */
    return apply_filters('rest_url', $url, $path, $blog_id, $scheme);
}

WordPress Version: 4.4

/**
 * Retrieves the URL to a REST endpoint on a site.
 *
 * Note: The returned URL is NOT escaped.
 *
 * @since 4.4.0
 *
 * @todo Check if this is even necessary
 *
 * @param int    $blog_id Optional. Blog ID. Default of null returns URL for current blog.
 * @param string $path    Optional. REST route. Default '/'.
 * @param string $scheme  Optional. Sanitization scheme. Default 'rest'.
 * @return string Full URL to the endpoint.
 */
function get_rest_url($blog_id = null, $path = '/', $scheme = 'rest')
{
    if (empty($path)) {
        $path = '/';
    }
    if (is_multisite() && get_blog_option($blog_id, 'permalink_structure') || get_option('permalink_structure')) {
        $url = get_home_url($blog_id, rest_get_url_prefix(), $scheme);
        $url .= '/' . ltrim($path, '/');
    } else {
        $url = trailingslashit(get_home_url($blog_id, '', $scheme));
        $path = '/' . ltrim($path, '/');
        $url = add_query_arg('rest_route', $path, $url);
    }
    if (is_ssl()) {
        // If the current host is the same as the REST URL host, force the REST URL scheme to HTTPS.
        if ($_SERVER['SERVER_NAME'] === parse_url(get_home_url($blog_id), PHP_URL_HOST)) {
            $url = set_url_scheme($url, 'https');
        }
    }
    /**
     * Filter the REST URL.
     *
     * Use this filter to adjust the url returned by the `get_rest_url` function.
     *
     * @since 4.4.0
     *
     * @param string $url     REST URL.
     * @param string $path    REST route.
     * @param int    $blog_id Blog ID.
     * @param string $scheme  Sanitization scheme.
     */
    return apply_filters('rest_url', $url, $path, $blog_id, $scheme);
}