禁用除索引、注册、登录之外的所有其他页面,直到用户登录

时间:2011-10-21 作者:Ezhil

我需要阻止访问除索引、wp登录外的所有其他内容。php,注册。php,直到用户登录。如何完成它。

我创造了一切,但当我尝试mysite.com/members 它向页面显示如何禁用它。。

4 个回复
SO网友:EarnestoDev

使用编写的功能代码PHP 5.3+ Closures. 通篇阅读评论,了解概念和流程。

// Hook right before Themes kick in
add_action(\'wp\', function(){
    // Allow viewing of home/front_page
    if(is_home() or is_front_page()) return;
    // Maybe allow access to archives (non singulars) to?
    // (when you hide important content after <!--more-->)
    // if(is_archive()) return; // Uncomment to allow access to archives
    // Allow access to BuddyPress registration/login page (experimental)
    if(is_callable(\'bb_get_location\')){
        if(preg_match(\'~^(login|registration)-page$~i\', bb_get_location())){
            return; // Allow access
        }
    }
    // Skip BuddyPress registration page directly (slightly experimental)
    if(is_page() and is_array($bp_pages = get_option(\'bp-pages\'))){
        $current_page = get_queried_object_id();
        // Allow direct access to registration page
        if(!empty($bp_pages[\'register\']) and ($bp_pages[\'register\'] == $current_page)){
            return; // Bail on registration page
        }
        // If you also want access to other special BuddyPress pages do this:
        if(in_array($current_page, $bp_pages)){
            // return; // Uncomment me to allows access to them
        }
    }
    // Allow access to BBPress registration/login page (experimental)
    if(is_callable(\'bbp_get_query_name\')){
        if(preg_match(\'~^bbp_(login|register)$~i\', bbp_get_query_name())){
            return; // Allow access
        }
    }
    // If user is logged it let it be
    if(is_user_logged_in()) return;
    // Block Robots using X-Robots-Tag: header
    // (no point in allowing Robots to index this)
    if(!headers_sent()){ // Be defensive
        header(\'X-Robots-Tag\', \'noindex, nofollow, noarchive\');
    }
    // Redirect to Login URL here
    wp_redirect(wp_login_url($_SERVER[\'REQUEST_URI\']), 302);
    die; // Bail here
});
// And add some Memberhsip info on wp-login.php
add_action(\'login_footer\', function(){
    // Print some information here about Membership and site
    echo \'<p style="text-align: center;">\', \'Awesome Memberhsip stuff.\', \'</p>\';
});
// Block indexing of login page
add_action(\'login_init\', function(){
    // Block Robots using X-Robots-Tag: header
    if(!headers_sent()){ // Be defensive
        header(\'X-Robots-Tag\', \'noindex, nofollow, noarchive\');
    }
});
问候您。

SO网友:soulseekah

/members 显示,因为它被重写为index.php?pagename=membersindex.php?category=membersWordPress Rewrite 发动机启动。

根据“成员”实际是什么以及其他页面是什么,您可以使用is_user_logged_in() 函数,并使用它在用户未登录时满足某些条件时有条件地重定向。

作为最简单的示例(这在模板文件中):

如果(!is\\u user\\u logged\\u in())退出(\'访问受限\');

您还可以编写插件来钩住查询,让它在满足某些条件的情况下阻止页面或重定向,例如:

function wpse31700_restrict_access() {
    global $wp_query;
    $query_vars = $wp_query->query_vars;

    // Allow by returning
    $allowed_pages = array( \'\', \'contact\' );
     if ( in_array($query_vars[\'pagename\'], $allowed_pages )
        return;

    // Or restrict by redirecting
    $restricted_pages = array( \'members\', \'pricelist\', \'etc\' );
    if ( !is_user_logged_in() and in_array($query_vars[\'pagename\'], $restricted_pages )
        wp_redirect( ... );
}
add_action( \'wp\', \'wpse31700_restrict_access\' );
上面的代码只是为了说明这个概念,它不会开箱即用。您可以检查pagenames、 发布namesauthor_name, 甚至可以通过ID查看帖子/页面的元值,例如,查看它们是否包含“受限”值。

SO网友:Taylor Dewey

因此,您希望“锁定”除首页以外的所有内容(或者必须是index.php?)还有注册和登录页面的管理端,对吗?

不幸的是,当我们在注册或登录页面上时,没有模板条件标记可以告诉我们,但我在这里找到了一个提供条件标记的简介:https://stackoverflow.com/questions/5266945/wordpress-how-detect-if-current-page-is-the-login-page

从这里开始,就是编写函数的问题。我将其连接到wp操作挂钩中,因为已经晚了,模板已经被选中(我们知道它是否是主页)。无论如何,请试用此脚本。

function is_login_page() {
    return in_array($GLOBALS[\'pagenow\'], array(\'wp-login.php\', \'wp-register.php\'));
}

function access_control(){
    if ( !is_front_page() && !is_user_logged_in() && !is_login_page() ){
        wp_die(\'Sorry, you must be logged into access this content\', \'Access Denied\', array( 
        \'response\' => 401,
        \'back_link\' => true
        ));
    }
}

add_action( \'wp\', \'access_control\' );

SO网友:Dan

不久前,我在一个项目中编写了您正在寻找的确切代码。下面的代码假定您使用的是子主题,而403。php文件存在于子目录中。(我复制了404.php,并对其进行了一些小改动。)

请注意,此代码可以使用登录/注册页面,即使代码中没有任何提示。

function wpse31700_403_error( $template ){
    if( !is_user_logged_in() && !is_front_page() ){
        header(\'HTTP/1.1 403 Forbidden\');
        return get_stylesheet_directory().\'/403.php\';
    }
    return $template;
}
add_filter(\'template_include\', \'wpse31700_403_error\', 1, 1);
此外,如果要在用户未登录时修改主菜单以不包含其他链接,可以利用wp_nav_menu_items 滤器有关使用该过滤器的良好指南位于here.

结束

相关推荐

Change login error messages

我看了一下这些问题,但找不到类似的问题。我需要更改用户尝试使用错误用户名或密码登录时显示的自定义错误消息。例如,更改此:”ERROR: 您为用户名输入的密码%1$s 不正确。丢失密码?对此“错误信息”(仅举一个例子)我试图使用“add\\u filter”,但我对它不熟悉,因此,如果您有任何帮助,我将不胜感激!谢谢