get_rest_url()
Gets the URL of the REST API endpoint. Allows specifying the site of the network.
The returned URL is not sanitized for output. Therefore, use the function esc_url() before outputting.
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
Returns
String. URL to the REST API endpoint.
Usage
get_rest_url( $blog_id, $path, $scheme );
- $blog_id(integer)
- ID of the site/blog. Passed to get_home_url()
Default: null (current site) - $path(string)
- REST route (path). Added to the end of the URL, the last slash is removed if present.
Default: '/' - $scheme(string)
- Data cleaning scheme. Passed to get_home_url().
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. |