将查询变量添加到 Wordpress 'page_link' 过滤器会导致 PHP 致命错误


Adding query var to Wordpress 'page_link' filters causes PHP Fatal error

我正在构建一个Wordpress网站,我使用查询字符串来根据用户在网站上的旅程来设置页面颜色的主题。

为了将当前配色方案传递到下一页以增加连续性,我正在过滤页面链接并添加当前查询字符串(如果存在)。通过此代码:

function append_query_string( $url, $post ) {
  if( !empty( $_GET['type'] ) ){
    return add_query_arg( 'type', $_GET['type'], $url );
  } elseif ( is_page( array( 'sound', 'vfx' ) ) ) {
    return add_query_arg( 'type', get_the_slug(), $url );
  }
}
add_filter( 'page_link', 'append_query_string', 9, 2 );
add_filter( 'post_type_link', 'append_query_string', 10, 2 );

两个过滤器都工作正常。除非在导致内存问题的页面上。我已将其缩小为由"page_link"过滤器引起的:

mod_fcgid: stderr: PHP Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 93 bytes)

我已经编辑了内存限制,不幸的是这没有帮助,当它有足够的内存来避免内存错误(这是 512MB - 我又把它放回去了)时,我仍然没有看到该页面。

我已经没有了可能导致它的想法。我是第一个相信我的代码有问题的人,但因此不明白为什么该网站除了一页之外的所有页面上都运行良好。

附言该页面创建视频幻灯片,如果我禁用"page_link"过滤器,则可以正常工作

您可以使用 cookie:

add_action('init', 'myfunction');
function myfunction() {
    // You should sanitize 'type' before using it
    $type = $_GET['type'];
    if( !empty( $type ) ) {
        setcookie('current_color', $type, strtotime('+1 day'));     
    } elseif ( is_page( array( 'sound', 'vfx' ) ) ) {
        setcookie('current_color', get_the_slug(), strtotime('+1 day'));    
    }
}

然后,您可以使用$_COOKIE['current_color']访问cookie