如果用户未登录,则重定向到登录页面

时间:2014-10-22 19:10:56

标签: php wordpress-plugin wordpress

我正在使用wordpress网站,如果访问者没有登录,我想将访问者重定向到登录页面,但是对于某些页面。我使用下面的代码,但它不适合我。帮助我在这段代码中错误的地方

function login_required() {
if (!is_user_logged_in() ) {

          $restricted = array(6,13,552,12,121,491,534,23,233,24,234,74,117,419); // all your restricted pages
          if ( in_array(get_the_ID(), $restricted ) ) {
            wp_redirect(home_url()); 
            exit();
        }
    }
}
add_action( 'template_redirect', 'login_required');

2 个答案:

答案 0 :(得分:0)

试试这个:

function login_required() {
    global $post;
    $restricted = array( 6, 13, 552, 12, 121, 491, 534, 23, 233, 24, 234, 74, 117, 419 ); // all your restricted pages
    if( in_array( $post->ID, $restricted ) && ! is_user_logged_in() ) {
        wp_redirect( home_url() ); 
        exit();
    }
}
add_action( 'template_redirect', 'login_required' );

答案 1 :(得分:0)

您可以使用Page Restrict插件,使用此插件可以选择要求登录的页面。

见下图:

Page Restrict Config