Created
January 15, 2019 20:21
-
-
Save jonbrockett/9fbbb6c9bb8bbd3a29d2e6c7938fb7aa to your computer and use it in GitHub Desktop.
WP - Private Page Redirect to Login with Custom Message
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Redirect private pages/posts to login */ | |
require_once( 'library/private-redirect.php' ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action('wp', 'my_private_page_404'); | |
function my_private_page_404() | |
{ | |
$queried_object = get_queried_object(); | |
if (isset($queried_object->post_status) && 'private' == $queried_object->post_status && !is_user_logged_in()) { | |
wp_safe_redirect(add_query_arg('private', '1', wp_login_url($_SERVER['REQUEST_URI']))); | |
exit; | |
} | |
} | |
add_filter('login_message', 'my_private_page_login_message'); | |
function my_private_page_login_message($message) | |
{ | |
if (isset($_REQUEST['private']) && $_REQUEST['private'] == 1) { | |
$message .= sprintf('<p class="message">%s</p>', __('The page you tried to visit is restricted. Please log in or register to continue.')); | |
} | |
return $message; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment