-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
52 lines (42 loc) · 1.05 KB
/
functions.php
File metadata and controls
52 lines (42 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
/**
* Only Login theme — block public front-end except API-like requests.
*
* @package only_admin
*/
defined( 'ABSPATH' ) || exit;
add_action( 'template_redirect', 'only_admin_redirect_visitors', 0 );
/**
* Send visitors to login or admin; do not trap REST or common API entry points.
*
* @return void
*/
function only_admin_redirect_visitors() {
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
return;
}
if ( function_exists( 'wp_is_serving_rest_request' ) && wp_is_serving_rest_request() ) {
return;
}
global $wp;
if ( ! empty( $wp->query_vars['rest_route'] ) ) {
return;
}
// WooCommerce legacy REST entry point.
if ( get_query_var( 'wc-api', false ) ) {
return;
}
$request_uri = '';
if ( isset( $_SERVER['REQUEST_URI'] ) ) {
$request_uri = sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) );
}
if ( '' !== $request_uri && false !== strpos( $request_uri, 'wp-json' ) ) {
return;
}
if ( is_user_logged_in() ) {
wp_safe_redirect( admin_url() );
} else {
wp_safe_redirect( wp_login_url() );
}
exit;
}