【问题标题】:wordpress code snippet php restricted pages accesswordpress 代码片段 php 限制页面访问
【发布时间】:2020-12-16 23:56:55
【问题描述】:

我正在开发一个现有的 wordpress 项目,并且我正在使用终极会员插件。我认为,如果用户已登录,则要限制对页面的访问,我们必须为额外的模块付费。所以我尝试了php代码sn-ps。我尝试使用 XYZ PHP 代码 sn-p,但虽然 sn-p 代码处于活动状态,但它不起作用。我想我必须添加在某处创建的 sn-p 但不知道 wordpress 在哪里加上法语。我可以直接进入页面编辑并添加 sn-p 这是代码 sn-p :

<?php
if ( !is_user_logged_in() ) {
wp_redirect( 'https://xxxxir.com/register' ); 
  //auth_redirect();
}

xyz-ips sn-p="重定向"

谢谢。

【问题讨论】:

    标签: php wordpress code-snippets


    【解决方案1】:

    如果你想限制特定页面,你可以这样做。 您要限制的页面应该有 slug,例如 xxx

    // Put these codes in function.php
    // xxx is slug of your page
    
    add_action('wp_head', function(){
    
        global $post;
    
        $post_slug = $post->post_name;
    
        if ( $post_slug == 'xxx' && !is_user_logged_in() ) {
          wp_redirect( 'https://xxxxir.com/register' ); 
        }
            
    });
    

    【讨论】:

    • 好的,谢谢,但我听说插件代码 sn-p 可以防止您在更改或更新主题时丢失工作。但如果我别无选择,我会用这个。一个问题在哪里可以找到蛞蝓? id 在 admin 的 url 中,但是 slug ??
    • 编辑页面时可以找到slug。 编辑页面 > 文档选项卡(右侧边栏)> 固定链接 > URL Slug
    • 你也可以使用 get_the_ID() 代替 post slug
    • 没关系,但有人知道登录后返回上一页的函数 auth_redirect() 吗?我们可以用吗?
    猜你喜欢
    • 1970-01-01
    • 2012-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多