Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如果用户未登录,则重定向到登录页面_Php_Wordpress - Fatal编程技术网

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

Php 如果用户未登录,则重定向到登录页面,php,wordpress,Php,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 pag

我在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');
试试这个:

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' );

您可以使用页面限制插件,使用该插件,您可以选择需要登录的页面

见下图:


您需要解释“未工作”在这里的含义。它做了什么或没有做什么?在function.php文件中添加上述代码后,当我点击一些页面时,页面没有重定向到登录页面,而这些页面是数组
$restricted
中的页面?是的,在$restricted数组中定义页面ID此插件还打开页面没有重定向不工作!!您的代码只适用于我使用buddypress创建的不在组中的页面