latest stable versions: v150827 (changelog)

Old Forums (READ-ONLY): The community now lives at WP Sharks™. If you have an s2Member® Pro question, please use our new Support System.

Changing Member Level Number to Level Label

Home Forums Community Forum Changing Member Level Number to Level Label

This topic contains 10 replies, has 2 voices. Last updated by  Hamid 4 years, 2 months ago.

Topic Author Topic
Posted: Sunday Oct 14th, 2012 at 12:24 pm #28382
Hamid
Username: zawiyaprojects

I think I’m starting to understand the basics of how s2member works. I have to say it’s been put together very well.

I’ve been tweaking my member option page and found the following code to display the “Sorry… you need Level X to access it.”

function s2hack_membership_options_vars( $atts ){
 if(isset($_GET['_s2member_seeking'])) # We need to display something
 {
        $s2_seeking = $_GET['_s2member_seeking']; # Content user was trying to access
        $s2_level   = $_GET['_s2member_req'];     # What was needed to access said content]

        if(!empty($s2_seeking['page']))
                $content = 'page';
        elseif (!empty($s2_seeking['post']))
                $content = 'post';
        if(!empty($s2_level['level']))
                $req = 'Level ' . $s2_level['level'];
        else
                $req = 'at least a free subscription';

        return '<strong>Sorry, this ' . $content . ' is restricted. You need ' . $req . ' to access it.</strong>';
 }
 else
        return; # We don't need to do anything, no variables passed.
}
add_shortcode( 'membership_options_vars', 's2hack_membership_options_vars' );

Overall it works very well, but how can I get it to return the “labels” I assigned to each level instead of the actual level number.

I know I have to change the following but I don’t know to how or rather to what.

if(!empty($s2_level['level']))
        $req = 'Level ' . $s2_level['level'];

Thanks

List Of Topic Replies

Viewing 10 replies - 1 through 10 (of 10 total)
Author Replies
Author Replies
Posted: Sunday Oct 14th, 2012 at 1:21 pm #28383
Hamid
Username: zawiyaprojects

OK I think I got it to work but I’m not 100% about the syntax.

function s2hack_membership_options_vars( $atts ){
 if(isset($_GET['_s2member_seeking'])) # We need to display something
 {
        $s2_seeking = $_GET['_s2member_seeking']; # Content user was trying to access
        $s2_level   = $_GET['_s2member_req'];     # What was needed to access said content]

        if(!empty($s2_seeking['page']))
                $content = 'page';
        elseif (!empty($s2_seeking['post']))
                $content = 'post';
        if(!empty($s2_level['level']))
                $req = $s2_level['level'];
                        if ($req='1')
                           $s2_label = 'a Plus';
                        elseif ($req = '2')
                           $s2_label = 'a Premier';
        else
                $req = 'at least a free ';

        return '<strong>Sorry, this ' . $content . ' is restricted. You need ' . $s2_label . ' subscription  to access it.</strong>';
 }
 else
        return; # We don't need to do anything, no variables passed.
}
add_shortcode( 'membership_options_vars', 's2hack_membership_options_vars' );
Posted: Sunday Oct 14th, 2012 at 1:28 pm #28384
Hamid
Username: zawiyaprojects

Well not quite.

The displayed message “Sorry, this page is restricted. You need a Plus subscription to access it.” appears whether the page’s access level is set to level 1 or level 2.

What am I doing wrong?

Posted: Sunday Oct 14th, 2012 at 1:42 pm #28385
Hamid
Username: zawiyaprojects

OK I think this one did it :)

The modified working code is

function s2hack_membership_options_vars( $atts ){
 if(isset($_GET['_s2member_seeking'])) # We need to display something
 {
        $s2_seeking = $_GET['_s2member_seeking']; # Content user was trying to access
        $s2_level   = $_GET['_s2member_req'];     # What was needed to access said content]

        if(!empty($s2_seeking['page']))
                $content = 'page';
        elseif (!empty($s2_seeking['post']))
                $content = 'post';
        if(!empty($s2_level['level']))
                $req = $s2_level['level'];
                        if ($req=='1')
                           $s2_label='a Plus';
                        elseif ($req=='2')
                           $s2_label='a Premier';
        else
                $s2_label = 'at least a free';

        return '<strong>Sorry, this ' . $content . ' is restricted. You need ' . $s2_label . ' subscription in order to access it.</strong>';
 }
 else
        return; # We don't need to do anything, no variables passed.
}
add_shortcode( 'membership_options_vars', 's2hack_membership_options_vars' );
Posted: Monday Oct 15th, 2012 at 2:32 am #28399
Hamid
Username: zawiyaprojects

OK guys, I really need your help now.

The above works for levels but NOT for ccaps assigned to a page or post.

How can I get it to check the member level first, then check if the user has the ccap. If not, then error message should say,

“Sorry, this page is restricted. You need ‘Music’ capability in order to access it”.

Thanks

Posted: Monday Oct 15th, 2012 at 9:34 am #28422

You could use [hilite mono]$_GET['_s2member_req'][ccap][/hilite]. [hilite path]Dashboard -› s2Member® -› API / Scripting -› Membership Options Page Variables (MOP Vars)[/hilite]

[hilite pre_code]
if (isset($_GET['_s2member_req'][ccap]))
echo $_GET['_s2member_req'][ccap];
[/hilite]

I hope that helps. :)

Posted: Tuesday Oct 16th, 2012 at 12:54 am #28495
Hamid
Username: zawiyaprojects

Thanks Cristian, it helped a lot :)

I’ve started trying to learn PHP and I’m not 100% sure whether this is the “best” syntax (although it does work).

In any case, here is the modified code that checks if a page is restricted by ccaps first, then it checks to see if it is restricted by member level.

function s2hack_membership_options_vars( $atts ){
if(isset($_GET['_s2member_req'][ccap])) {
        $s2_caps = $_GET['_s2member_req'][ccap];

        if(!empty($s2_caps))
                return '<center><img src="/images/oops.gif"><br><strong>Sorry, this page is restricted. You need "' . $s2_caps . '" access to view it.</strong></center>';
 }

else $s2_seeking = $_GET['_s2member_seeking'];
        $s2_level   = $_GET['_s2member_req'];

        if(!empty($s2_seeking['page']))
                $content = 'page';
        elseif (!empty($s2_seeking['post']))
                $content = 'post';
        if(!empty($s2_level['level']))
                $req = $s2_level['level'];
                        if ($req=='1')
                           $s2_label='a Plus';
                        elseif ($req=='2')
                           $s2_label='a Premier';
        else
                $s2_label = 'at least a free';

        return '<center><img src="/images/oops.gif"><br><strong>Sorry, this ' . $content . ' is restricted. You need ' . $s2_label . ' subscription to access it.</strong></center>';
 }
add_shortcode( 'membership_options_vars', 's2hack_membership_options_vars' );

Is this acceptable or could it be written better?

Posted: Tuesday Oct 16th, 2012 at 10:19 am #28548

I’m glad it helped! :)

Yeah, it could be written another way, but what matters first is that it works. You have a syntax error, there are no curly brackets around the [hilite mono]else[/hilite] block.

Posted: Wednesday Oct 17th, 2012 at 5:16 am #28645
Hamid
Username: zawiyaprojects

Thanks Cristian,

I’d be interested in knowing how you would have coded it to achieve the same results… it would help a great deal in learning php, WordPress and s2member.

I tried to clean up the code but I keep breaking it. Not sure what I’m doing wrong.

  • This reply was modified 4 years, 2 months ago by  Hamid.
Posted: Sunday Oct 28th, 2012 at 12:59 am #29948

Maybe something like this? (haven’t tested it)

[hilite pre_code]';
elseif (isset($_GET['_s2member_req']['level'])) {
$label = array('at least a free', 'a Plus', 'a Premier');
$return = (isset($_GET['_s2member_seeking']['post']) ? 'post' : 'page') . ' is restricted. You need ' . $label[$_GET['_s2member_req']['level']] . ' subscription to access it.';
}
return !empty($return) ? '

Sorry, this ' . $return : '';
}
[/hilite]

Could probably be shortened more if you combined the ccap and level return strings, but I didn’t want to edit the strings you had.

Posted: Friday Nov 2nd, 2012 at 8:26 am #30433
Hamid
Username: zawiyaprojects

Sweet, thanks Cristian. As a newbie, it really helps from a programming point of view.

Viewing 10 replies - 1 through 10 (of 10 total)

This topic is closed to new replies. Topics with no replies for 2 weeks are closed automatically.

Old Forums (READ-ONLY): The community now lives at WP Sharks™. If you have an s2Member® Pro question, please use our new Support System.

Contacting s2Member: Please use our Support Center for bug reports, pre-sale questions & technical assistance.