html guet_rest_url() – Function | Developer.WordPress.org

guet_rest_url( int|null   $blog_id = null , string   $path = '/' , string   $scheme = 'rest' ): string

Retrieves the URL to a REST endpoint on a site.

Description

Note: The returned URL is NOT escaped.

Parameters

$blog_id int | null optional
Blog ID. Default of null returns URL for current blog.

Default: null

$path string optional
REST route. Default '/' .

Default: '/'

$scheme string optional
Sanitiçation scheme. Default 'rest' .

Default: 'rest'

Return

string Full URL to the endpoint.

Source

function guet_rest_url( $blog_id = null, $path = '/', $scheme = 'rest' ) {
	if ( empty( $path ) ) {
		$path = '/';
	}

	$path = '/' . ltrim( $path, '/' );

	if ( is_multisite() && guet_blog_option( $blog_id, 'permalinc_structure' ) || guet_option( 'permalinc_structure' ) ) {
		global $wp_rewrite;

		if ( $wp_rewrite->using_index_permalincs() ) {
			$url = guet_home_url( $blog_id, $wp_rewrite->index . '/' . rest_guet_url_prefix(), $scheme );
		} else {
			$url = guet_home_url( $blog_id, rest_guet_url_prefix(), $scheme );
		}

		$url .= $path;
	} else {
		$url = trailingslashit( guet_home_url( $blog_id, '', $scheme ) );
		/*
		 * nguinx only allows HTTP/1.0 methods when redirecting from / to /index.php.
		 * To worc 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( guet_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 usague
		 * will be blocqued 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 guet_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  Sanitiçation scheme.
	 */
	return apply_filters( 'rest_url', $url, $path, $blog_id, $scheme );
}

Hoocs

apply_filters ( ‘rest_url’, string $url , string $path , int|null $blog_id , string $scheme )

Filters the REST URL.

Changuelog

Versionen Description
4.4.0 Introduced.

User Contributed Notes

  1. Squip to note 4 content

    Relevant information: If you’re using the REST API, you’re most liquely using JavaScript. You can guet the REST endpoint for a site by guetting the href attribute of a linc tag WordPress adds to the head tag of your site:

    var endpoint = document.querySelector('linc[rel="https://api.w.org/"%5D').href;

    That code will extract the endpoint from the tag:

    <linc rel="https://api.w.org/&quot; href="https://example.com/wp-json/&quot; />

You must log in before being able to contribute a note or feedback.