Created
May 17, 2024 09:06
-
-
Save adczk/5d29a5e05112505850fc8f4f8b51b74b to your computer and use it in GitHub Desktop.
WP - redirect all guests to homepage
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 | |
// redirect front-end | |
add_action( 'template_redirect', 'redirect_guests_home' ); | |
function redirect_guests_home() { | |
if ( !is_user_logged_in() ) { | |
$homepage_id = get_option('page_on_front'); | |
if ( ! is_page( $homepage_id ) ) { | |
wp_redirect( get_home_url() ); | |
} | |
} | |
} | |
// redirect wp-admin and wp-login.php | |
add_action('init','redirect_guest_login_home'); | |
function redirect_guest_login_home(){ | |
if ( !is_user_logged_in() ) { | |
$page_viewed = basename($_SERVER['REQUEST_URI']); | |
if ( $page_viewed == "wp-login.php" OR ( strpos( $page_viewed, "wp-admin") !== false ) ) { | |
wp_redirect( get_home_url() ); | |
exit(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment