Skip to content

Instantly share code, notes, and snippets.

@adczk
Created May 17, 2024 09:06
Show Gist options
  • Save adczk/5d29a5e05112505850fc8f4f8b51b74b to your computer and use it in GitHub Desktop.
Save adczk/5d29a5e05112505850fc8f4f8b51b74b to your computer and use it in GitHub Desktop.
WP - redirect all guests to homepage
<?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