Set membership level in cookie?
-
I’ve spent a decent amount of time trying to figure this out. I’m quite new to php, so I have a feeling my ignorance is contributing to this. Basically, I need to set a cookie that includes a logged-in member’s membership level. The site I’m working on includes a non-WordpPress application under the main domain that needs to be able to read this cookie to determine user access. The issue I’m running into is how to best pass the membership level to
setcookie()
infunctions.php
. I’ve so far been able to set and remove the cookie at login/logout. I’ve also been able to pass the membership level as a value in the cookie (wrapping the object inprint_r()
for testing purposes – this would be encrypted outside of testing). However, when I update membership level to a different level, the cookie does not change. Additionally, again for testing purposes, I have not been able to pass any other fields from thepmpro_getMembershipLevelForUser()
function (for instance “name” or “subscription”) to the cookie value. Here is the current code I’m using in my functions.php script:add_action('wp_login', 'custom_set_login_cookie'); function custom_set_login_cookie() { global $current_user; $current_user_membership_level = pmpro_getMembershipLevelForUser($current_user->ID); $membership_level = $current_user_membership_level->ID; setcookie('id', print_r($membership_level), time()+300, "/"); }
add_action('wp_logout', 'custom_remove_login_cookie'); function custom_remove_login_cookie() { setcookie('id', '', time() - 3600, "/"); }
For what it’s worth, the code to show the level/name etc. seems to work perfectly fine when running and displaying on the front-page, for instance. When I update the membership level, the level is updated when displayed on the page. It’s just the cookie that does not update. Any help or advice is much appreciated! I apologize if this is poorly explained or if my php code doesn’t make sense. The plugin so far has been great. Thanks
- The topic ‘Set membership level in cookie?’ is closed to new replies.