get_rest_url()WP 4.4.0

Получает URL конечной точки REST API. Позволяет указать сайт сети.

Возвращаемый URL не очищается для вывода на экран. Поэтому перед выводом используйте функцию esc_url().

Работает на основе: get_home_url(), rest_get_url_prefix()
Основа для: rest_url(), rest_output_link_wp_head()
1 раз — 0.000043 сек (очень быстро) | 50000 раз — 0.82 сек (очень быстро) | PHP 7.0.5, WP 4.4.2
Хуки из функции

Возвращает

Строку. URL до конечной точки REST API.

Использование

get_rest_url( $blog_id, $path, $scheme );
$blog_id(число)
ID сайта/блога. Передается в get_home_url()
По умолчанию: null (текущий сайт)
$path(строка)
REST маршрут (путь). Добавляется в конце URL, последний слэш удаляется, если он есть.
По умолчанию: '/'
$scheme(строка)
Схема очистки данных. Передается в get_home_url().
По умолчанию: 'rest'

Примеры

0

#1 Демонстрация работы

Получим URL различных конечных точек REST

echo get_rest_url();
//> http://wp-kama.ru/wp-json/

echo get_rest_url( 5 );
//> http://subsite.wp-kama.ru/wp-json/

echo get_rest_url( 0, '/foo' );
//> http://wp-kama.ru/wp-json/foo

echo get_rest_url( 0, '/foo', 'https' );
//> https://wp-kama.ru/wp-json/foo

Заметки

  • Global. WP_Rewrite. $wp_rewrite WordPress rewrite component.

Список изменений

С версии 4.4.0 Введена.

Код get_rest_url() WP 6.5.2

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 );
}