javascript - 如何检查访问重定向下载 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 文件?
解决方案
我从您的要求中了解到,您想检查 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;
}
}
}
推荐阅读
- .net - .Net Core 3.1 _Layout.cshtml 中的托管环境错误:CS0841:在声明之前无法使用局部变量“hostingEnv”
- arrays - 如何对 python 列表中的每一对数字求和?
- amazon-web-services - AWS EMR 上的 spark-submit 引发异常
- mysql - 在不构建项目的其余部分的情况下重建特定的共享对象 (libndbclient.so)?
- character-encoding - 使用Datastage,Sql server的法语字符编码问题
- excel - VBA for Excel 在 .xlam 中出现错误“91”,但在 .xlsm 中工作正常
- javascript - 有没有办法从其他文件向 HTML 添加代码?
- google-sheets - 根据多个标准谷歌表对唯一值求和
- pdf - 使用 Active PDF Toolkit 中的合并文件功能时未传输 T26 到 T37 字体
- swift - 带有labels.isHidden的tableview/tableviewcell奇怪的行为