Page 1 of 1

Code "If" or "Switch?"

PostPosted: October 24th, 2010, 9:55 pm
by KirkWard
Would this work?

Code: Select all
<?php
switch (current_user_is($i) {
    case s2member_level4:
      Some premium content for Level 4 Members.
      break;
    case s2member_level3:
      Some premium content for Level 3 Members.
      break;
    case s2member_level2:
      Some premium content for Level 2 Members.
      break;
    case s2member_level1:
      Some premium content for Level 1 Members.
      break;
    case s2member_level0:
      Some content for Free Subscribers.
      break;
    default:
      Some public content.;
} ?>

Re: Code "If" or "Switch?"

PostPosted: October 25th, 2010, 2:54 am
by Jason Caldwell
Hi Kirk. Thanks for the excellent question.

Please try it like this instead:

Code: Select all
<?php if (current_user_is("s2member_level4")){ ?>
    Some premium content for Level 4 Members.
<?php } else if (current_user_is("s2member_level3")){ ?>
    Some premium content for Level 3 Members.
<?php } else if (current_user_is("s2member_level2")){ ?>
    Some premium content for Level 2 Members.
<?php } else if (current_user_is("s2member_level1")){ ?>
    Some premium content for Level 1 Members.
<?php } else if (current_user_is("s2member_level0")){ ?>
    Some content for Free Subscribers.
<?php } else { ?>
    Some public content.
<?php } ?>
Documented in your Dashboard under: s2Member -> API Scripting -> Advanced Conditionals.

Or, if you really like the switch() function, do it this way:
Code: Select all
<?php
switch 
(ws_plugin__s2member_user_access_level ())
    {
        case 4:
            echo 'Some premium content for Level 4 Members.';
            break;
        case 3:
            echo 'Some premium content for Level 3 Members.';
            break;
        case 2:
            echo 'Some premium content for Level 2 Members.';
            break;
        case 1:
            echo 'Some premium content for Level 1 Members.';
            break;
        case 0:
            echo 'Some content for Free Subscribers.';
            break;
        default:
            echo 'Some public content';
    }
?>


If you'd like to do this without using PHP, here is a Shortcode equivalent:
Code: Select all
[s2If current_user_is(s2member_level4)]
   Some premium content for Level 4 Members.
[/s2If]

[s2If current_user_is(s2member_level3)]
   Some premium content for Level 3 Members.
[/s2If]

[s2If current_user_is(s2member_level2)]
   Some premium content for Level 2 Members.
[/s2If]

[s2If current_user_is(s2member_level1)]
   Some premium content for Level 1 Members.
[/s2If]

[s2If current_user_is(s2member_level0)]
   Some content for Free Subscribers.
[/s2If]

[s2If !current_user_can(access_s2member_level0)]
   Some public content.
[/s2If]
Perfect for the WordPress® Visual Editor.
Documented in your Dashboard, under: s2Member -> API Scripting -> Simple Conditionals

Re: Code "If" or "Switch?"

PostPosted: October 25th, 2010, 10:10 am
by KirkWard
Found notes on the shortcode after I posted and plan on using that.

Don't like strings of "if" statements as it's too easy for old brains to get confused. We have enough trouble remembering how to turn our computers on.

Re: Code "If" or "Switch?"

PostPosted: October 25th, 2010, 12:11 pm
by KirkWard
Hi Jason,

Expanding on your suggested usage above, and what I saw in the video about using advanced conditionals, would this work to allow me to show specific content, such as buttons on the member options page?

Code: Select all
    <?php if (is_user_logged_in && current_user_can ("s2member_level4")){ ?>
        Some premium content for Level 4 Members, but not for members of any other level.

    <?php } else if (is_user_logged_in && current_user_can ("s2member_level3")){ ?>
        Some premium content for Level 3 Members, but not for members of any other level.

    <?php } else if (is_user_logged_in && current_user_can ("s2member_level2")){ ?>
        Some premium content for Level 2 Members, but not for members of any other level.

    <?php } else if (is_user_logged_in && current_user_can ("s2member_level1")){ ?>
        Some premium content for Level 1 Members, but not for members of any other level.

    <?php } else if (is_user_logged_in && current_user_can ("s2member_level0")){ ?>
        Some content for Free Subscribers, but not for members of any other level.

    <?php } else { ?>
        Some public content, not for members of any other level.
    <?php } ?>

Re: Code "If" or "Switch?"

PostPosted: October 25th, 2010, 6:28 pm
by KirkWard
edit: Cancel responding to this ... I was using a script other than EXEC-php. I thought the "Raw HTML" script worked the same way. It didn't.

Thanks anyway.

I'm leaving the message intact, because I put it here and didn't want to delete what was up.

***************************************

Jason,

I copied your code from above and wrapped my "home" page in the public content. Then I logged out and went ot my home page. The content was not displayed. Did I miss something?

What I used ...
Code: Select all
    <?php if (current_user_is("s2member_level4")){ ?>
        Some premium content for Level 4 Members.
    <?php } else if (current_user_is("s2member_level3")){ ?>
        Some premium content for Level 3 Members.
    <?php } else if (current_user_is("s2member_level2")){ ?>
        Some premium content for Level 2 Members.
    <?php } else if (current_user_is("s2member_level1")){ ?>
        Some premium content for Level 1 Members.
    <?php } else if (current_user_is("s2member_level0")){ ?>
        Some content for Free Subscribers.
    <?php } else { ?>
        Content from my home page went here.
    <?php } ?>

Re: Code "If" or "Switch?"

PostPosted: October 25th, 2010, 11:17 pm
by Jason Caldwell
Awesome. Thanks for reporting back on this.
~ Much appreciated.