【问题标题】:WordPress filters - Filter to control which page is being loadedWordPress 过滤器 - 过滤器以控制正在加载的页面
【发布时间】:2016-08-11 21:32:47
【问题描述】:

我正在尝试在我的网络上开发密码角色保护。我需要实现的是将访问者重定向到登录wp-login.php,如果他们是订阅者,则当他们尝试进入特定页面时。 WordPress 没有给我们这个选项,所以我必须自己开发(我不想使用外部插件)。

我有角色过滤器,但我找不到过滤器来控制正在加载的页面以重定向访问者。

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    将此代码添加到您的 Function.php 文件中。 using template_redirect action hook

    这个动作挂钩在 WordPress 确定要加载的模板页面之前执行。如果您需要在完全了解已查询内容的情况下进行重定向,这是一个很好的钩子。

    function template_redirect_fn()
    {
        //add you logic to perform your task  
        //Redirect page to login page 
        if(is_page ('about-us'))
        {
    
            $loginUrl = "LOGIN URL";
            wp_redirect($loginUrl);
             exit(); 
        }
        //if more then one page then used this 
    
        if( is_page( array( 'about-us', 'contact', 'management' ) )
          // about us, or contact, or management page is in view
    
    }
    add_action( 'template_redirect', 'template_redirect_fn' );
    

    is_page(Page ID OR title OR slug of you want to check )

    【讨论】:

    • 这条线是我需要的add_action( 'template_redirect', 'template_redirect_fn' );。非常感谢,它可以完美地工作。一个问题,这个template_redirect过滤器,什么时候调用?
    • @ProgramadorAdagal 检查此链接codex.wordpress.org/Plugin_API/Action_Reference/…
    • 即使您已经登录,该功能不会每次都重定向吗?是否存储了任何会话信息来阻止这种情况发生?
    • @Lee 这是用于演示的不完美代码,您可以添加 && ! is_user_logged_in() in if 条件
    • @Lee 我在功能检查中添加了注释“添加你的逻辑来执行你的任务”
    猜你喜欢
    • 2011-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-17
    • 2012-11-28
    相关资源
    最近更新 更多