• Hello everyone,

    I‘m struggling with a noob issue here again:

    I‘m trying to make my site look specific, when someone with a certain GET variable comes in. Currently, the code in my functions.php looks like this:

    First I store the GET variable in a Cookie and a Session (native session plugin is installed):

    function set_cookie_session(){
    	if ( isset($_GET['abc']) ){
    		global $wp_session;
    		if( ! session_id() ) {
            	session_start();
        	}
    	$_SESSION["abc"] = $_GET['abc'];
    	setcookie("abc", $_COOKIE["abc"], time() + 60 * 60 * 24 * 30, "/", "my-site.com", true, true );
    		
    	}
    }
    add_action( 'init', 'set_cookie_session', 9 );

    and then I try to add the specific style to the header:

    function customize_abc() {
    	
    	if( isset($_COOKIE["abc"] || isset($_SESSION["awc"]) ) {
    		echo "<style>…..</style>";
       }
    }
    add_action( 'wp_head', 'costomize_abc', 20 );
    

    I can see the cookie in my browser. But this 2nd piece of code neither detects the cookie nor the session variable. When I try to print them they are both empty. I even moved the cookie setting into the header.php but same issue.

    Where is my mistake and how can I do this better?

    Thanks all!

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • You have made a mistake with the function name that should be called via wp_head.

    costomize_abc != customize_abc

    Thread Starter Matthias Jünger

    (@mjuenger)

    Thanks for the hint ????

    Unfortunately, this typo only happened when I anonymized the function.
    I have correct the type, but still not fixed.

    Inside the customize_abc I tried:

    echo "<script>alert('Session/Cookie: ".$_SESSION['abc'].$_COOKIE['abc']."');</script>";

    It’s both empty.

    I hoped there is probably some obvious mistake that I just did not see.

    There are more errors in the code. You would notice them if you used an editor with syntax highlighting. Both are in this line:

    if( isset($_COOKIE["abc"] || isset($_SESSION["awc"]) ) {

    Error 1: there is a “)” missing for the first isset(). This should actually trigger a PHP error for you.
    Error 2: the name of the session is misspelled.

    Because of these errors, I’m not sure whether you’re showing us the right codes or just copied ones. You should debug the code you use properly to solve this. Take a look at the tips in the PHP manual: https://www.php.net/manual/en/debugger.php

    Dion

    (@diondesigns)

    Your code makes no sense.

    • The global $wp_session; declaration is unnecessary; WordPress does not support true sessions. What WordPress calls “sessions” are what other software calls autologin keys. True sessions would, for example, allow WordPress to generate a list of online logged-in users.
    • PHP sessions work by setting cookies, so using one in addition to setting your own cookie is redundant. Just set your own cookie! This will also make WordPress happier because its REST API was written in a way that causes conflicts with PHP sessions.

    Thread Starter Matthias Jünger

    (@mjuenger)

    Thanks @threadi. Yes, I tried to simplify the code for here. Unfortunately, I got a lot of typos in it instead. Sorry.

    Thanks @diondesigns for your explanation.

    I have almost solved it: The cookie was not working because WP Rocket cached it. I added it as an exception and there, it works perfectly fine now.

    The Session is not working, it seems, since Sessions just don’t work in WP.

    I saw some threads online that people installed the Native PHP Sessions for WordPress Plugin to work with Sessions. It seems, it does not for my site.
    @diondesigns Is there anything like Sessions in WP, where I can store a value for a session or a certain amount of time, that the visitor brings with GET when he enters my page? I am thinking to use this as a backup, in case the visitor blocks cookies.

    Thanks a lot for your support.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Customize design using GET variable’ is closed to new replies.