在wordpress的白名单主页

时间:2017-08-09 17:58:12

标签: php wordpress

我正在使用强制登录wordpress插件限制用户登录,然后查看某些页面但我想查看主页没有登录我试图在白名单中添加主页但它不工作我正在使用我的自定义主题。可以建议我应该使用什么插件/我应该为此目的做什么。

    public string MakeRequest(string parameters)
    {
        CookieContainer cookie = new CookieContainer();

        string URL = EndPoint + parameters;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);

        request.CookieContainer = cookie;

        request.Method = Method.ToString();
        request.ContentLength = 0;
        request.ContentType = ContentType;

        if (!string.IsNullOrEmpty(PostData) && Method == HttpVerb.POST)
        {
            var encoding = new UTF8Encoding();
            var bytes = Encoding.GetEncoding("iso-8859-1").GetBytes(PostData);
            request.ContentLength = bytes.Length;

            using (var writeStream = request.GetRequestStream())
            {
                writeStream.Write(bytes, 0, bytes.Length);
            }
        }

        using (var response = (HttpWebResponse)request.GetResponse())
        {
            var responseValue = string.Empty;

            if (response.StatusCode != HttpStatusCode.OK)
            {
                var message = String.Format("Request failed. Received HTTP {0}", response.StatusCode);
                throw new ApplicationException(message);
            }

            // grab the response
            using (var responseStream = response.GetResponseStream())
            {
                if (responseStream != null)
                    using (var reader = new StreamReader(responseStream))
                    {
                        responseValue = reader.ReadToEnd();
                    }
            }

            return responseValue;
        }
    }

1 个答案:

答案 0 :(得分:0)

将此代码插入主题的functions.php。它会检查您是否已登录。接下来,如果当前页面不是主页或首页,它会将用户重定向到登录页面。

并且您不需要其他插件来重定向,因此请在测试此代码之前禁用任何强制登录插件。

function logged_in_only() {
    if ( ! is_user_logged_in() ) {
        if( !is_home() || !is_front_page() )
            auth_redirect();
    }
}
add_action( 'template_redirect', 'logged_in_only' );