I need to make a site I am developing be mostly private, accessible by standard Wordpress login. Most plugins are way overkill for what I need, I don't need user roles, tiers, monetization etc. I just need the entire site to be inaccessible, except for the home page and one or two other pages, unless the user logs in with their WP credentials. So accessing any page (except for certain whitelisted pages) would redirect the user to the standard WP login.
I found this script:
function make_wordpress_site_private(){
global $wp;
if (!is_user_logged_in() && $GLOBALS['pagenow'] !== 'wp-login.php'){
wp_redirect(wp_login_url($wp -> request));
exit;
}
}
add_action('wp', 'make_wordpress_site_private');
It does 90% of what I need but I am unsure how to whitelist certain pages.
if (!is_user_logged_in() && $GLOBALS['pagenow'] !== 'wp-login.php' && !is_page(array('page1', 'page2', 'page3')) )
You can check the documentation for is_page here: developer.wordpress.org/reference/functions/is_page