admin管理员组文章数量:1332383
I'm working on a single page application built with WordPress and Vue.js, I used add_rewrite_rule
to redirect all sub-pages of projects/
to index.php
. Only logged-in users can access the application. If a user isn't logged-in a redirect to wp-login.php
will be returned.
Here's the code I used in functions.php
to achieve what I mentioned above:
// Redirect users who are not logged-in
add_action('template_redirect', function () {
// Front end only and allow access to ajax functions
if (is_admin() || wp_doing_ajax()) {
return;
}
// Redirect all pages to the login page if user isn't logged in
if (!is_user_logged_in()) {
wp_redirect(wp_login_url());
}
});
// Remove all default WP template redirects/lookups
remove_action('template_redirect', 'redirect_canonical');
add_action('init', function () {
// Redirect homepage or all requests that start with /projects to index.php so the Vue app
// is loaded and 404s aren't thrown
add_rewrite_rule('^projects((/.*)?)', 'index.php', 'top');
});
The issue is whenever I access any sub-page /projects/page/etc
I do get redirected to wp-login.php
even though I'm logged in! And the strange thing is this happens only on the production server, development and staging servers all work fine.
I used a var_dump(is_user_logged_in())
inside the callback of the template_redirect
action and the result is false
.
本文标签: phpisuserloggedin returns false when I access certain pages
版权声明:本文标题:php - is_user_logged_in returns false when I access certain pages 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742294068a2448367.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论