• I’m trying to use data from one page that has a custom field and pass it to any other future pages that visitor goes to.

    In this case, the client has local branches all over the country, so if a visitor goes to the page for a particular branch, I want that phone number to then replace the number throughout the rest of the site.

    So, on the subpage, I have:

    <?php $_COOKIE['localphone'] = get_post_meta($post->ID, 'phone', true); ?>
    <p class="hdr-phone"><?php echo $_COOKIE['localphone']; ?></p>

    Which works fine on that page, but when I go to another page, the data that was in $_COOKIE[‘localphone’] is gone. What can I do?

    [ Please do not bump, that’s not permitted here. ]

Viewing 10 replies - 1 through 10 (of 10 total)
  • Moderator bcworkz

    (@bcworkz)

    You’re just changing the php variable. To actually store the cookie via the user’s browser, you need to use setcookie() before any content is sent.

    Thread Starter jlknauff

    (@jlknauff)

    Right, I’ve tried placing that in a bunch of location; on the page, in the header, even in functions.php but once I go back to one of the other pages, that value is no longer in the variable.

    As example below from functions.php:

    function set_newuser_cookie() {
        if (!isset($_COOKIE['localphone'])) {
    	$localphone = get_post_meta($post->ID, "phone", true);
            setcookie('localphone', $localphone, time()+3600);
        }
    }
    add_action( 'init', 'set_newuser_cookie');

    Is there something else I should be aware of?

    Moderator bcworkz

    (@bcworkz)

    It needs to be sent to the browser before any content is sent, meaning the setcookie() must execute before even the page’s <!DOCTYPE html> is sent out. Not sure if the init action qualifies or not. This is a ridiculous restriction. You might consider setting it with javascript instead. You might investigate how WP sets cookies, I kind of doubt all are set ahead of any content.

    I haven’t had the need to set cookies via php yet, I’ve always used javascript. I’ve now pretty much exhausted all my knowledge of setting cookies with php, sorry I can’t help more.

    Thread Starter jlknauff

    (@jlknauff)

    Hmmm…the WP flow seems to be what’s screwing it up (based on my capabilities, nothing “wrong” with WP), might JS be a better option in your opinion?

    Thread Starter jlknauff

    (@jlknauff)

    The reason I ask is I’ve tried sending it via WP, using the functions.php, which is loaded before any of the template files, but data is still always deleted from the cookie the next page I go to.

    Moderator bcworkz

    (@bcworkz)

    “deleted from the cookie” — does that mean the cookie exists on the browser, but $_COOKIE has no values? Or just poor choice of words, there is no cookie on the browser? Any possibility of the browser refusing cookies for some reason?

    In any case, I’m inclined to use JS, as it is client side and does not matter when the code is executed. If one really wanted to use php, a possible approach is to set the cookie during wp_redirect. You could either hook the filter, or redefine the pluggable function. The default function uses header() which has the same restriction of needing to be sent before content, so this should work, though I haven’t tested it. Rather silly to need to redirect to another page just to store a cookie though.

    Thread Starter jlknauff

    (@jlknauff)

    The cookie is there, as I am logged into WP and remain logged in from page to page, but the $_COOKIE[‘localphone’] no longer exists.

    No, not redirecting to another page to store the cookie – when a visitor arrives on a page for a local branch, the page plugs the local data into the cookie, in this case, the local phone #, and as long as you nav to the sub-pages of that page, it works fine, but when you go to one of the pages above it, the cookie loses $_COOKIE[‘localphone’].

    Does that make better sense?

    When you mention “hooking” the filter, I have a basic idea of what you’re saying, but I’m not sure how. I had put the php to set the cookie into the functions.pp; shouldn’t that do it?

    Moderator bcworkz

    (@bcworkz)

    Makes sense now. That means setcookie() is working. And that I’ve thrown you a red herring about execution placement, my apologies.

    Putting code in functions.php is almost always a good place to do so. By hooking a filter, I mean to use add_filter() and add_action() to cause code to execute at certain points. You have hooked the ‘init’ action in one of your snippets.

    When you nav to a parent page, the cookie is on the browser, and the value is still there, but PHP is not populating $_COOKIE, yes? Any reason why the browser wouldn’t send the cookie data? Different sub-domain? Sounds like your code is working as expected, but you’re running afoul of some sort of security/privacy restriction. This is outside of my expertise, so I’m probably not of much more help ??

    FWIW, I’m still leaning towards a JS solution. It’s local data on the client, to be only displayed on the client, why get your server involved? Other than to send the branch phone# initially of course.

    Thread Starter jlknauff

    (@jlknauff)

    Hmmm…well, I have add_action() but not add_filter(). Should both be there? If so, where should add_filter() go?

    Right, when I nav to the appropriate page, it works fine, and it works fine on all of its sub pages, but when I go back up to page further up the tree, then that data goes away.

    I’m open to using JS if that would be a better option…

    I’ve found this https://www.w3schools.com/js/js_cookies.asp which looks like it *should* fit the mission, then I just need to pass the data to php so WP can use it. If I’m on the right track with that, let me know. I know next to nothing about JS.

    Thanks for your help so far!

    Moderator bcworkz

    (@bcworkz)

    No, just one or the other, depending on which hook you’d want to use. If you don’t have an example indicating which to use, you need to find the code that initiates the hook to determine if it’s a filter or action.

    That JS tutorial should be all you need to get something working. But if the browser isn’t sending cookie data for some reason, JS may still not work. If it does work, it points to something odd with the server and PHP. PHP super globals are cool, they just magically work. Except when they don’t, then it’s mind numbing to figure out why.

    My thought on using JS didn’t involve sending cookie data to the server at all. All phone fields on every page would have the same unique ID, and the field by default has the central phone number. On every page load, a script would run, check for the cookie, if there’s data, replace the default phone with the local phone. This function could be initiated from an onload action in the <body> tag, so your header template will need to be modified a bit.

    There’s any number of possible solutions, that was my thought, but I may be missing something precluding this. You will need to use wp_enqueue_script() to enable JS on your WP pages, executed in a function called by hooking the action ‘wp_enqueue_scripts’. I enqueued my JS file from my child theme’s functions.php. My util.js file is in themes/mychildtheme/js/, I simply did wp_enqueue_script('util'); and it found the file and loaded it without any path spec.

    I’ll be AFK for sometime, so this may be my last post on this thread. Good luck with JS. It can be handy once in a while. If you find a few uses for it, check out JQuery, it makes accessing html elements much easier, and has many cool features.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘How to use cookies within WP?’ is closed to new replies.