Session Cookie for membership levels?

Is there a way to set a session cookie based on the membership level, eg
Membership ID=1, set cookie AGE18=True, session, expires 30 minutes.
Membership ID=2, set cookie Pending=True, session, expires 30 minutes.
No Membership, set no cookies.

  • Adam Czajczyk

    Hello Thor,

    I hope you're well today and thank you for your question!

    There's currently no "out of the box" support for that but Membership 2 Pro includes an API so I believe that could be handled with some custom "mix" of core WP hooks and API calls. I have asked one of my colleagues (who's better familiar with Membership 2 Pro API) for a little help and I'll update this thread as soon as I get more information.

    Please keep track of this topic!

    Best regards,

  • Adam Czajczyk

    Hello Thor!

    It seems that you came up with a solution quite similar to what I was going to suggest :slight_smile:

    Instead of putting the code into the template file you could make use of an MU plugin as it seems to be a perfect case for it :slight_smile:

    function maybe_set_membership_cookie() {
    	$user_id = get_current_user_id();
    	$member = MS_Factory::load( 'MS_Model_Member', $user_id);
    	if ($member->has_membership() && !$member->has_membership(1682))  {

    Note please that the "setcookie()" line is a bit different than in your code. I replaced "true" value with "1" because cookies are storing values in a plain text and while "true" should always reflect in value of "1" there are known cases when it fails and also using "false" could cause that to break.

    I have also added "COOKIEPATH" and "COOKIE_DOMAIN" that are defined by WordPress and should be used in this case.

    In order to apply that code to your site create an empty file with a .php extension (e.g. "my-membership-cookies.php") and paste the code inside, then upload the file to the "/wp-content/mu-plugins" folder of your WordPress install.

    If there's no "mu-plugins" folder inside "/wp-content" folder, just create it.

    I hope that helps!
    Best regards,

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.