Right hook to redirect frontend visitors while respecting rest api plugins to run first?
Question
I don’t need a frontend on the WP site. All visitors will be redirected to an external url where my frontend app lives.
I now have the following code in the root of my theme functions.php:
if ( ! is_admin() ) {
wp_redirect( 'http://www.example.com', 200 );
exit;
}
Unfortunately this runs before the plugins needed to set up the rest api have run.
Where should I place this code to get it right?
Warning!
See answers(s) below.
Found a solution myself:
add_action( 'wp_loaded', function () {
// check if there is a 'wp-json' URI part
if (! strpos($_SERVER['REQUEST_URI'], 'wp-json')) {
// No rest api request. Now check if we are on frontend
if ( ! is_admin() ) {
// We are on frontend: redirect now
wp_redirect( 'http://www.example.com', 200 );
exit();
}
}
});
The above code works fine (tested) if put in a plugin file.
The above code does not work (tested) when put in the root of theme functions.php
file.
0
redirect, rest-api
6 years
2016-11-29T00:40:29-05:00
2016-11-29T00:40:29-05:00 0 Answers
98 views
0
Leave an answer