问题:如果每个帖子的 URL 参数为空,则 Wordpress 重定向

所以有些人直接访问我的网站(不是通过我使用 Voluum 获得的跟踪链接),因此他们无法点击链接,我也无法将它们视为我的统计数据的一部分。如何将没有/?voluumdata=BASE64...URL 参数的用户重定向到被跟踪的 URL,并为每篇博文设置不同的重定向?

我一直在测试并寻找插件/ .htaccess 技巧几个小时,但似乎没有任何帮助。

编辑:我找到了一个我确定会起作用的解决方案,但由于某种原因它没有:

[insert_php]

if(empty($_GET['voluumdata'])) 
{ 
  header('Location: REDIRECT_URL');
  exit; 
}
[/insert_php]

也试过:

[insert_php]
if(!isset($_GET['voluumdata'])) 
{ 
  header('Location: REDIRECT_URL');
  exit; 
}
[/insert_php]

两者都只是破坏页面加载过程。

解答

不幸的是,我无法理解您在问题中输入的代码的目的是什么。我的意思是不清楚你使用标签[insert_php]的原因。

解决您的问题的方法可能如下。

function redirect_direct_access( ) {
    // You may use the code:
    //
    //     global $wp_query
    //
    // in order to determine in which pages you should run your
    // redirection code. If you only check for the token existence
    // then you will be faced with redirection loop. I don't explain in
    // depth how to use the $wp_query as it is not part of your question
    // but you always have the opportunity to check what is the contents
    // of this variable using the code:
    //
    //    echo "<pre>";
    //    print_r( $wp_query );
    //    echo "</pre>";
    //
    // This way you will be able to build your specific if statement
    // for the page you like to test.

    if ( 
        ! isset( $_GET[ 'voluumdata' ] ) ||
        empty( $_GET[ 'voluumdata' ] ) 
    ) { 
        wp_redirect( home_url( '/page/to/redirect/' ) );
        exit();
    }
}

add_action( 'template_redirect', 'redirect_direct_access' );

您可以在与模板_redirect挂钩相关的 WordPress 文档中找到更多信息。

Logo

React社区为您提供最前沿的新闻资讯和知识内容

更多推荐