I agree that passing the query string from one page to another page is the most reliable solution, and you can, for example, use filters like post_link
and term_link
to add the content
query string to post/category links.
However, it requires hooking to various filters like you can see here. And in addition, multi-page URLs like example.com/page/2?content=multipage
won’t work without extra coding.
So it’s probably better/easier for you to store the multi-page status in the browser’s cookies, just as what you’ve attempted — the problem in your code is, that you’re just setting the cookie, but nowhere reading it. And you can use $_COOKIE['content']
to read the cookie’s value, just as you can see in the example below.
The Code/Solution
First off, remove this from your code:
//**IF Statement that should set the cookie**//
if ( empty( $_GET['content'] ) || "multipage" !== $_GET['content'] ) {
//**Only removes onepage() when content=multipage**//
add_action('wp','onepage');
setcookie( 'content', 'multipage', time()+3600, COOKIEPATH, COOKIE_DOMAIN );
}
And add the code after the //**PAGEBREAK SECTION - END**//
in your code:
add_action( 'init', 'set_check_content_cookie', 0 );
function set_check_content_cookie() {
if ( is_admin() ) {
return;
}
$is_multipage = false;
// If the query string "content" is not set, check if the "content" cookie value is exactly "multipage".
if ( ! isset( $_GET['content'] ) && isset( $_COOKIE['content'] ) && 'multipage' === $_COOKIE['content'] ) {
$is_multipage = true;
}
// If the query string "content" is set and its value is "multipage", set the cookie.
elseif ( isset( $_GET['content'] ) && 'multipage' === $_GET['content'] ) {
setcookie( 'content', 'multipage', time()+3600, COOKIEPATH, COOKIE_DOMAIN );
$is_multipage = true;
}
// Hook onepage() to `wp`, if applicable.
if ( ! $is_multipage ) {
add_action( 'wp', 'onepage' );
}
}
Note: You should remember that using this solution means that cookies must be enabled in the browser (i.e. supported and that the user is accepting cookies).