Wednesday 20 April 2022

On logged in user can view WP site? How to Make Member only site?

Add following code in theme "functions.php"  file




/*************************************************************************************************************
 * Only logged in user can view site except Login, Register, Password-Recovery & successful Registration pages
 *************************************************************************************************************/
add_action( 'wp', 'members_only' );
function members_only() {
    // Restrict unlogged user
    if( !is_user_logged_in() ) {
        // id specified page then execute
        // 789 is page id for "Registration" page.
        // 792 is page id for "Login" page is also Home page.
        // 796 is page id for "Password Recovery" page.
        // 868 is page id for "Successful Registration" page.
        // is_front_page() for website main page.
        if (is_page(789) || is_page(792) || is_page(796) || is_page(868) || is_front_page() ) {
           // echo "This is current page";
        }
        else {
            // Redirect to Home page.
            //wp_redirect( get_home_url() );
            wp_redirect( 'https://www.example.com/wp-login.php' );
            exit;
        }
    }
}

Ankit Shah PHP Expert

IT New Code suggest a solution for your problem related to coding, Specially .PHP, Wordpress, WooCommerce, Magento, HTML and CSS. Visit Our website for Information.

0 comments:

Post a Comment

 

Copyright @ 2016 IT New Code | Developing Code | Designing Code.

Designed by: Ankit Shah