I want to restrict certain pages of my WordPress website to logged in users.
To do this I've written some code redirecting users to the login page, and set my membership plugin to then redirect them to the last user page. The problem is that the wp_redirect function does not save the URL, so after logging in the user gets redirected to the page they were on before, rather than the page they were trying to access (the restricted page).
Any help with this annoying problem would be much appreciated! :) My code is below.
function page_redirects() {
if (is_page("Example") ) {
if (is_user_logged_in()!) {
wp_redirect('https://www.barn-door.co.uk/login/');
}
}
}
add_action( 'template_redirect', 'page_redirects', 1, 0 );