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

330 views Asked by At

I'm building a Wordpress site where I use a a query string to theme the page colours dependant on the user's journey through the site.

In order to pass the current colour scheme onto the next page to add continuity, I am filtering the page links and adding the current query string if it exists. Via this code:

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 );

Both filters work fine. Except on one page where it causes memory issues. I have narrowed it down to being caused by the 'page_link' filter:

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

I have edited the memory limit and unfortunately that didn't help, when it had enough memory to avoid the memory error (which was 512MB - I've dropped it back again) I still didn't see the page.

I've run out of ideas for what could cause it. I'm the first to believe something is wrong with my code, but don't understand therefore why the site functions beautifully on all but one page.

P.S. That page creates a slideshow of videos, works fine if I disable the 'page_link' filter

1

There are 1 answers

0
Alan On

You could use cookies:

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'));    
    }
}

Then you could access the cookie with $_COOKIE['current_color']