get_rest_url()
Retrieves the URL to a REST endpoint on a site.
Note: The returned URL is NOT escaped.
Used By: rest_url(), rest_output_link_wp_head()
1 time — 0.000043 sec (very fast) | 50000 times — 0.82 sec (very fast) | PHP 7.0.5, WP 4.4.2
Hooks from the function
Return
String
. Full URL to the endpoint.
Usage
get_rest_url( $blog_id, $path, $scheme );
- $blog_id(int|null)
- Blog ID.
Default: of null returns URL for current blog - $path(string)
- REST route.
Default: '/' - $scheme(string)
- Sanitization scheme.
Default: 'rest'
Examples
#1 Demo: Get the URLs of REST endpoints
Get the URLs of the different REST endpoints:
echo get_rest_url(); //> http://example.com/wp-json/ echo get_rest_url( 5 ); //> http://subsite.example.com/wp-json/ echo get_rest_url( null, '/foo' ); //> http://example.com/wp-json/foo echo get_rest_url( null, '/foo', 'https' ); //> https://example.com/wp-json/foo
#2 Get the REST endpoint for JS code
If you’re using the REST API, you’re most likely using JavaScript.
You can get the REST endpoint for a site by getting the href
attribute of a <link>
tag WordPress adds to the <head> tag of your site:
var endpoint = document.querySelector( 'link[rel="https://api.w.org/"]' ).href;
That code will extract the endpoint from the tag:
<link rel="https://api.w.org/" href="https://example.com/wp-json/" />
Notes
- Global. WP_Rewrite. $wp_rewrite WordPress rewrite component.
Changelog
Since 4.4.0 | Introduced. |