In this video I will go over how you can activate a coming soon or maintenance page only on selected pages.
I will go over how to do this with only code so this will be a more advanced tutorial. But don’t worry I will walk you through all the code so you can understand how everything works.
Timestamps:
- 0:00 Introduction
- 2:10 Code Snippets Overview
- 3:37 Part 1 – Page Redirects
- 9:05 Part 2 – Blog Post Redirects
- 11:24 Part 3 – Page Array Redirects
Part 1: Page Function Code
add_action( 'template_redirect', 'redirect_to_specific_page' );
function redirect_to_specific_page() {
if ( is_page('About') && ! is_user_logged_in() ) {
wp_redirect( 'https://yourdomain.com', 302 );
exit;
}
}
Part 2: Blog Post Function Code
add_action( 'template_redirect', 'redirect_to_specific_post' );
function redirect_to_specific_post() {
if ( is_single('Sticky Column') && ! is_user_logged_in() ) {
wp_redirect( 'https://yourdomain.com', 302 );
exit;
}
}
Part 3: Page Array Function Code
add_action( 'template_redirect', 'redirect_to_specific_page' );
function redirect_to_specific_page() {
if ( is_page( array('About', 'resources') ) && ! is_user_logged_in() ) {
wp_redirect( 'https://yourdomain.com', 302 );
exit;
}
}
Code Snippets WordPress Plugin:
https://wordpress.org/plugins/code-snippets/
WordPress Code Reference:
https://developer.wordpress.org/reference/functions/is_page/
Stack Exchange Code Reference:
https://wordpress.stackexchange.com/questions/131879/how-to-redirect-non-logged-in-users-to-a-specific-page