get_rest_url

Retrieves the URL to a REST endpoint on a site.

Description

(string) get_rest_url( (constant) $blog_id = null, (string) $path = '/', (string) $scheme = 'rest' ); 

Note: The returned URL is NOT escaped.

Returns (string)

Full URL to the endpoint.

Parameters (3)

0. $blog_id — Optional. (constant) => null
Blog ID. Default of null returns URL for current blog.
1. $path — Optional. (string) => '/'
REST route. Default /..
2. $scheme — Optional. (string) => 'rest'
Sanitization scheme. Default rest..

Usage

  1. if ( !function_exists( 'get_rest_url' ) ) { 
  2. require_once ABSPATH . WPINC . '/rest-api.php'; 
  3.  
  4. // Optional. Blog ID. Default of null returns URL for current blog. 
  5. $blog_id = null; 
  6.  
  7. // Optional. REST route. Default '/'. 
  8. $path = '/'; 
  9.  
  10. // Optional. Sanitization scheme. Default 'rest'. 
  11. $scheme = 'rest'; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = get_rest_url($blog_id, $path, $scheme); 
  15.  

Defined (1)

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

/wp-includes/rest-api.php  
  1. function get_rest_url( $blog_id = null, $path = '/', $scheme = 'rest' ) { 
  2. if ( empty( $path ) ) { 
  3. $path = '/'; 
  4.  
  5. if ( is_multisite() && get_blog_option( $blog_id, 'permalink_structure' ) || get_option( 'permalink_structure' ) ) { 
  6. global $wp_rewrite; 
  7.  
  8. if ( $wp_rewrite->using_index_permalinks() ) { 
  9. $url = get_home_url( $blog_id, $wp_rewrite->index . '/' . rest_get_url_prefix(), $scheme ); 
  10. } else { 
  11. $url = get_home_url( $blog_id, rest_get_url_prefix(), $scheme ); 
  12.  
  13. $url .= '/' . ltrim( $path, '/' ); 
  14. } else { 
  15. $url = trailingslashit( get_home_url( $blog_id, '', $scheme ) ); 
  16.  
  17. $path = '/' . ltrim( $path, '/' ); 
  18.  
  19. $url = add_query_arg( 'rest_route', $path, $url ); 
  20.  
  21. if ( is_ssl() ) { 
  22. // If the current host is the same as the REST URL host, force the REST URL scheme to HTTPS. 
  23. if ( $_SERVER['SERVER_NAME'] === parse_url( get_home_url( $blog_id ), PHP_URL_HOST ) ) { 
  24. $url = set_url_scheme( $url, 'https' ); 
  25.  
  26. /** 
  27. * Filters the REST URL. 
  28. * Use this filter to adjust the url returned by the get_rest_url() function. 
  29. * @since 4.4.0 
  30. * @param string $url REST URL. 
  31. * @param string $path REST route. 
  32. * @param int $blog_id Blog ID. 
  33. * @param string $scheme Sanitization scheme. 
  34. */ 
  35. return apply_filters( 'rest_url', $url, $path, $blog_id, $scheme );