• Resolved GarrukR

    (@ehjosh)


    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() in functions.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 in print_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 the pmpro_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

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi there. I believe you may need to use init instead of wp_login to call this code, since the cookie needs to be set before anything else (the init action comes right after WordPress loads but before headers are loaded). I think the $membership_level doesn’t need print_r, so I think you should try it without; in any case, I think print_r would be too much, since you’re calling a string and not an array by pulling out the ID by itself. Lastly, WordPress itself may be using ‘id’ in some way, so as a last resort I’d use a more unique name.

    I would give this a try:

    add_action('init', '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( 'pmpro_id', $membership_level, time()+300, '/' );
    }

    For removing the cookie, if setcookie doesn’t work, give the unset option a try:

    add_action('wp_logout', 'custom_remove_login_cookie');
    function custom_remove_login_cookie() {
    	if( isset( $_COOKIE['pmpro_id'] ) ) {
    		unset( $_COOKIE['pmpro_id'] );
    	}
    }

    I hope that helps. ??

    • This reply was modified 3 years, 9 months ago by Sharon.
    Thread Starter GarrukR

    (@ehjosh)

    Thanks for the response. I maybe wasn’t fully clear about this (my bad), but I was hoping to set the cookie specifically at/after login and not at initialization. I’m not sure if that is not recommended? But based on some other discussion I’ve seen regarding this, setting the cookie at login (using the wp_login and wp_logout is not uncommon.

    However, switching init in for wp_login now seems to be doing the trick! I might have to play around with it a little more, but because users will have to navigate to another page under the domain to use the other app that the cookie need to be set for anyway, I think this may work since the cookie isn’t need immediately after login. Kind of odd that the cookie was setting but the value wasn’t being retained at login but is at initialization. Anyway, thanks for the help!

    I’ve built quite a few cookies, and the wp_login doesn’t seem to save all the info properly or even execute at the right time in order to get the cookie generated (‘init’ is also recommended if you look on www.remarpro.com for ‘setcookie’).

    I’m glad it’s working! ??

    Thread Starter GarrukR

    (@ehjosh)

    Alright, working through this a little more, this is what I needed. Thanks again for the help.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Set membership level in cookie?’ is closed to new replies.