# auth_redirect()

URL: https://chugunov.pro/api-wordpress/functions/auth_redirect/
Проверено на WordPress 6.9, обновлено 06.08.2026.
Источник: независимый русскоязычный справочник chugunov.pro. Не является официальной документацией WordPress.

Тип: функция.
Появился в версии: 1.5.0.

## Сигнатура

```php
auth_redirect()
```

## Описание

Когда этот код вызывается со страницы, он проверяет, вошёл ли в систему пользователь, просматривающий страницу.
Если пользователь не вошёл в систему, он перенаправляется на страницу входа. Пользователь перенаправляется так, что после входа он сразу попадёт на ту страницу, к которой изначально пытался получить доступ.

## Исходный код

Файл: `wp-includes/pluggable.php:1280`

```php
function auth_redirect() {
	$secure = ( is_ssl() || force_ssl_admin() );

	/**
	 * Filters whether to use a secure authentication redirect.
	 *
	 * @since 3.1.0
	 *
	 * @param bool $secure Whether to use a secure authentication redirect. Default false.
	 */
	$secure = apply_filters( 'secure_auth_redirect', $secure );

	// If https is required and request is http, redirect.
	if ( $secure && ! is_ssl() && str_contains( $_SERVER['REQUEST_URI'], 'wp-admin' ) ) {
		if ( str_starts_with( $_SERVER['REQUEST_URI'], 'http' ) ) {
			wp_redirect( set_url_scheme( $_SERVER['REQUEST_URI'], 'https' ) );
			exit;
		} else {
			wp_redirect( 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
			exit;
		}
	}

	/**
	 * Filters the authentication redirect scheme.
	 *
	 * @since 2.9.0
	 *
	 * @param string $scheme Authentication redirect scheme. Default empty.
	 */
	$scheme = apply_filters( 'auth_redirect_scheme', '' );

	$user_id = wp_validate_auth_cookie( '', $scheme );
	if ( $user_id ) {
		/**
		 * Fires before the authentication redirect.
		 *
		 * @since 2.8.0
		 *
		 * @param int $user_id User ID.
		 */
		do_action( 'auth_redirect', $user_id );

		// If the user wants ssl but the session is not ssl, redirect.
		if ( ! $secure && get_user_option( 'use_ssl', $user_id ) && str_contains( $_SERVER['REQUEST_URI'], 'wp-admin' ) ) {
			if ( str_starts_with( $_SERVER['REQUEST_URI'], 'http' ) ) {
				wp_redirect( set_url_scheme( $_SERVER['REQUEST_URI'], 'https' ) );
				exit;
			} else {
				wp_redirect( 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
				exit;
			}
		}

		return; // The cookie is good, so we're done.
	}

	// The cookie is no good, so force login.
	nocache_headers();

	if ( str_contains( $_SERVER['REQUEST_URI'], '/options.php' ) && wp_get_referer() ) {
		$redirect = wp_get_referer();
	} else {
		$redirect = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
	}

	$login_url = wp_login_url( $redirect, true );

	wp_redirect( $login_url );
	exit;
}
```

## История изменений

- 1.5.0 — Introduced.

## Связанные

Использует: [`wp_redirect`](https://chugunov.pro/api-wordpress/functions/wp_redirect/), [`wp_validate_auth_cookie`](https://chugunov.pro/api-wordpress/functions/wp_validate_auth_cookie/), [`wp_login_url`](https://chugunov.pro/api-wordpress/functions/wp_login_url/), [`is_ssl`](https://chugunov.pro/api-wordpress/functions/is_ssl/), [`force_ssl_admin`](https://chugunov.pro/api-wordpress/functions/force_ssl_admin/), [`wp_get_referer`](https://chugunov.pro/api-wordpress/functions/wp_get_referer/), [`nocache_headers`](https://chugunov.pro/api-wordpress/functions/nocache_headers/), [`set_url_scheme`](https://chugunov.pro/api-wordpress/functions/set_url_scheme/), [`get_user_option`](https://chugunov.pro/api-wordpress/functions/get_user_option/), [`apply_filters`](https://chugunov.pro/api-wordpress/functions/apply_filters/), [`do_action`](https://chugunov.pro/api-wordpress/functions/do_action/).
Используется в: `WP_Customize_Manager::setup_theme`.

Оригинал в официальной документации: https://developer.wordpress.org/reference/functions/auth_redirect/
