Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Restrict specific pages in your WordPress site for logged in users only.
<?php
/**
* Restrict specific pages in your WordPress site for logged in users only.
*
*/
function my_restricted_pages_require_user_login() {
global $current_user;
// Allow logged in users.
if ( is_user_logged_in() ) {
return;
}
// Restrict all visitors from these pages.
$restricted_pages = array(
'private-page-1',
'private-page-2',
);
if ( is_page( $restricted_pages ) ) {
wp_redirect( home_url( 'wp-login.php?redirect_to=' . urlencode( $_SERVER['REQUEST_URI'] ) ) );
} else {
return;
}
}
add_action( 'template_redirect', 'my_restricted_pages_require_user_login' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.