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.

Revisions

  1. adczk created this gist May 17, 2024.
    30 changes: 30 additions & 0 deletions wp-redirect-all-guests-to-homepage.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    <?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();
    }
    }
    }