首页 > 解决方案 > 如何检查访问重定向下载 url 以进行 wp 日志记录的访问者?

问题描述

我正在尝试设置一个中介,它在访问重定向下载链接之前检查访问者是否登录了我们的 wordpress 网站。如果用户未登录,则不允许访问该 url。有外部直接下载 url,它们通过 htaccess 规则从我们网站的http://www.ourwebsite.com/resources url 重定向。

我试过这段代码,但没有用。我把它放在functions.php上:

require('../wp-load.php');  // modify to reflect where your PHP file is in relation to Wordpress
$roles = wp_get_current_user()->roles;  // get current users role

if (!in_array('alloweduserrole',$roles)) {  // modify to match your roles that are allowed to download

    header('Location: http://www.ourwebsite.com/');
    exit;

}  // end of if user does not have the proper role

我找不到任何 php 文件。添加了上面的代码,但什么也没发生。没有改变。也许我应该选择 wp-load.php 以外的另一个 php 文件?

标签: javascriptwordpress.htaccessurlredirect

解决方案


我从您的要求中了解到,您想检查 WordPress 网站上的登录状态,如果不是,您将用户重定向到另一个 url,对吗?如我错了请纠正我。因此,如果您正在检查主题内部functions.php,则无需require('../wp-load.php');

您可以使用以下代码片段:

add_action("init", "check_user");
function check_user() {
  if (! is_user_logged_in()) {
    wp_redirect("your redirect url", 301);
    //You can skip the 301, if the url is not a external link
    exit;
  }
}

}


推荐阅读