Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kimcoleman/93995037e3c6fb6cbceed82b207e9ec8 to your computer and use it in GitHub Desktop.
Save kimcoleman/93995037e3c6fb6cbceed82b207e9ec8 to your computer and use it in GitHub Desktop.
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