admin管理员组

文章数量:1426901

Looking for some advice on the below function. I have a web app type site built in WP. There is nothing on the site relevant to non-users, apart from the login form on the homepage. I've got some membership plugins that lock content, but as an added layer of security I was thinking of adding the following:

function lock_non_users_to_front() {
    if( !is_user_logged_in() && !is_front_page() ) {
        wp_redirect( home_url() );
    }
}
add_action( 'template_redirect', 'lock_non_users_to_front' );

Essentially, if anyone tries to access a url that isn't the homepage, and they aren't logged in, it'll just kick them back to the homepage.

Any thoughts/improvements?

Looking for some advice on the below function. I have a web app type site built in WP. There is nothing on the site relevant to non-users, apart from the login form on the homepage. I've got some membership plugins that lock content, but as an added layer of security I was thinking of adding the following:

function lock_non_users_to_front() {
    if( !is_user_logged_in() && !is_front_page() ) {
        wp_redirect( home_url() );
    }
}
add_action( 'template_redirect', 'lock_non_users_to_front' );

Essentially, if anyone tries to access a url that isn't the homepage, and they aren't logged in, it'll just kick them back to the homepage.

Any thoughts/improvements?

Share Improve this question edited May 16, 2019 at 18:31 warm__tape asked May 16, 2019 at 10:26 warm__tapewarm__tape 611 silver badge11 bronze badges 1
  • 1 Why not use "home_url()" instead of hard coding url? Also die() after wp_redirect(). – gmatta Commented May 16, 2019 at 11:10
Add a comment  | 

1 Answer 1

Reset to default 1

The comment by 'warm_tape' is correct. Use home_url(), then put a die() after the wp_redirect to ensure that the accessed page will 'die' and not interfere with your main page.

本文标签: loginAdvice on redirect to lock site from unauthorized users