Page 1 of 1

Form registration field with CCaps to determine access?

PostPosted: October 30th, 2011, 3:50 pm
by stebesplace
I couldn't think of a better title honestly. I've been searching all morning on this, and couldn't quite find what I was looking for. I'll try to make this brief.

I have a custom membership configuration which requires users with lvl #0 (subscriber) to be logged in to view a specific page. The page contains a form that they fill out as a "task", so to speak. The page, is only visible to people logged in. So far so good.

I've added a few custom entries to the registration form, some of which include Gender, State, Country, etc.

What I can't figure out, is the relationship between a registration field, such as Gender, and my ability to use CCaps as a way to restrict people within a certain gender, to view a specific page. So if a user is Male, they can get access to a page to fill out a form and complete their task.

The bigger goal here, is the ability to restrict specific pages for registration values such as geographic location. That allows me to subset users in a region such as San Francisco, that can view one page, versus people in New York City, who would view another. Make sense?

Hopefully I was able to get the concept across here, and I think CCaps is the method to use, since all users are only a lvl #0. I'll keep searching, but if anyone has any ideas, let me know. Thanks :)

Re: Form registration field with CCaps to determine access?

PostPosted: October 30th, 2011, 4:05 pm
by Bruce C
Hi stebesplace,

The custom registration forms are not the same thing as custom capabilities. In order to get custom registration forms, you will need to use s2Get. You can find what you are looking for under Dashboard->s2Member->API/Scripting->Simple/Shortcode Conditionals In example 4.

You will have to run a check to see if it equals male or female.

Cheers!

Re: Form registration field with CCaps to determine access?

PostPosted: October 30th, 2011, 4:19 pm
by stebesplace
Okay, I get the lack of a direct relationship between a custom registration field, and ccaps. So that makes sense. If I look in that section under simple/shortcode conditions, example 4, I see something along these lines: [s2Get user_field="country_code" /] which I believe could be used on the specific page where country_code would be replaced with, say, user_gender.

So a hypothetical setup could be, as shortcodes on that specific page that I'm requiring only "female" to be able to view:

Code: Select all
[s2If current_user_is(s2member_level0) AND (what conditional statement would go here in order to validate that the registration field entered was female?)]

Content for logged in user who is female goes here

[/s2If]


Thanks!

Re: Form registration field with CCaps to determine access?

PostPosted: October 30th, 2011, 4:29 pm
by Bruce C
hmm, I will take a look at this some more. I believe yo may be able to pass it through PHP. I'll try it a few ways for you though.

Re: Form registration field with CCaps to determine access?

PostPosted: October 30th, 2011, 4:52 pm
by stebesplace
Right on. Hopefully I was able to explain the concept enough to get the ball rolling. I think this functionality could work, especially for those who need to limit content based on a form field from the user registration for geographical region restrictions, gender, and other identifiers that can segment users.

Otherwise, one would have to implement a group setup using another 3rd party tool to clump people together. The problem with that approach, is that it would need some level of customization to automatically put people based on identifiers into groups, or manually adding users in. But I think what I described above should work in theory keeping the system pretty clean.

Thanks Ace!

Re: Form registration field with CCaps to determine access?

PostPosted: October 30th, 2011, 5:06 pm
by Bruce C
If found it in the source code.

You're going to need a PHP Execution Plugin Installed.

In order to get a custom value without using the [s2Get] shortcode, you're going to have to get the database array, and JSON decode it:

Code: Select all
<?php
//This gets the user fields
$fields = json_decode(S2MEMBER_CURRENT_USER_FIELDS, true);
//Then, use the $fields variable to get your data.
$fields["my_unique_field_id"];//Replace this with yours.
?>


To see if it equals what you want, you'll need to do this sort of thing:

Code: Select all
<?php
$fields = json_decode(S2MEMBER_CURRENT_USER_FIELDS, true);
if (current_user_is('s2member_level0') && $fields["user_gender"] === 'female')
{?>
Things for females
<?php}else if (current_user_is('s2member_level0') && $fields["user_gender"] === 'male'){?>
Things for males
<?php}else{?>
Things for non-members
<?php}?>


If this isn't what you're looking for, let me know.

Re: Form registration field with CCaps to determine access?

PostPosted: October 30th, 2011, 5:10 pm
by stebesplace
Okay, let me look at this here over the next couple of mins, and potentially implement it. Thanks!

Re: Form registration field with CCaps to determine access?

PostPosted: October 30th, 2011, 5:14 pm
by Bruce C
Cool, lemme know how it goes!

Re: Form registration field with CCaps to determine access?

PostPosted: October 30th, 2011, 5:26 pm
by stebesplace
Okay, had to correct an error with your second block of code (spacing)

Code: Select all
<?php
$fields = json_decode(S2MEMBER_CURRENT_USER_FIELDS, true);
if (current_user_is('s2member_level0') && $fields["Gender"] === 'female')
   {
   ?>
   Things for females
   <?php
}
else if (current_user_is('s2member_level0') && $fields["Gender"] === 'male')
   {
   ?>
   Things for males
   <?php
}
else
   {
   ?>
   Things for non-members
   <?php
}
?>


Placed that code on my page, specifically enabling the execute php plugin as well, and my output seems to be the "Things for non-members, though my current logged in user is an admin (me) and I have set the gender to be male. Also placed the first block of code on the page above the second block too.

So i have a custom registration field in S2 called "Gender" exactly as spelled there (Gender is a unique_id), setup as a radio button. The user has Male checked (one of the options).

Looking into this more, but that's where I'm at so far.

Re: Form registration field with CCaps to determine access?

PostPosted: October 30th, 2011, 5:32 pm
by Bruce C
that's because you're using the current_user_is function. If you would like to see it also, use current_user_can

Re: Form registration field with CCaps to determine access?

PostPosted: October 30th, 2011, 5:35 pm
by stebesplace
K, let me try that out here. Thanks!

Re: Form registration field with CCaps to determine access?

PostPosted: October 30th, 2011, 5:43 pm
by stebesplace
Okay, did a test on the page to ensure the field is working properly:

[s2Get user_field="Gender" /]

Gave me an output of Male, on the page. So the field is working.

Made the change to current_user_can, no modification to the output. Still shows the final else condition.

I'm wondering if S2MEMBER_CURRENT_USER_FIELDS is returning the correct results for my custom fields? See this thread (albeit back in 2010 and apparently resolved) viewtopic.php?f=4&t=378 to see what I am talking about.

Also, if I echo $fields, I get nothing: echo $fields["Gender"];

Regardless, still trying to get this working, but I think it's really close!

Re: Form registration field with CCaps to determine access?

PostPosted: October 30th, 2011, 5:46 pm
by Bruce C
Can you print_r $fields and see what pops up?

Re: Form registration field with CCaps to determine access?

PostPosted: October 30th, 2011, 5:52 pm
by stebesplace
Well, print_r gave me some results:

Array
(
[id] => 1
[ip] => 76.254.57.97
[reg_ip] => 99.35.230.170
[email] => user.name@gmail.com
[login] => uname
[first_name] => User
[last_name] => Name
[display_name] => User Name
[subscr_id] =>
[subscr_or_wp_id] => 1
[subscr_gateway] =>
[custom] =>
[gender] => Male
[marital_status] => Single
[student] => No
[education] => Some Highschool
[employment] => Full
[household_income] => 0-25,000
)

So it's deff able to pull in those custom fields, that's resolved. I thought perhaps it might be case sensitive, so tried gender instead of Gender, not the case.

Re: Form registration field with CCaps to determine access?

PostPosted: October 30th, 2011, 6:06 pm
by Bruce C
Sorry for the delayed response.

It looks as though it's not picking up the response right.

Try changing === to ==

If that doesn't work, try changing ["gender"] to [gender]

Re: Form registration field with CCaps to determine access?

PostPosted: October 30th, 2011, 6:07 pm
by stebesplace
No worries, your responses have been amazingly quick!

Boom, I found the issue.

current_user_can('s2member_level0') needs to be current_user_can('access_s2member_level0')

And what would you know, it worked!!!!!!

I'm going to test some more on other fields, but I'm wicked impressed. So far so good! Thanks Ace!!!!

Re: Form registration field with CCaps to determine access?

PostPosted: October 30th, 2011, 6:11 pm
by stebesplace
I hope that this finds it's way into a how-to, or at the least, people searching will find this post who have a similar requirement. This saved me having to implement another 3rd party "grouping" system based on defined information. Being able to pull registration data, and display specific pages based on that data is extremely powerful. Thanks!

So a final wrapup:

1) Implement the PHP Execution plugin.
2) Create a custom registration field.
3) Create a Wordpress Page.
4) Implement the first block of code on the page:
Code: Select all
<?php
$fields = json_decode(S2MEMBER_CURRENT_USER_FIELDS, true);
$fields["custom_field"];
?>

5) Implement second conditional block of code on the page:
Code: Select all
<?php
$fields = json_decode(S2MEMBER_CURRENT_USER_FIELDS, true);
if (current_user_can('access_s2member_level0') && $fields["custom_field"] === 'field_value')
   {
   ?>
   Content for this condition goes here.
   <?php
}
else if (current_user_can('access_s2member_level0') && $fields["custom_field"] === 'another_field_value')
   {
   ?>
   Content for this other condition goes here.
   <?php
}
else
   {
   ?>
   Dump public content here, or add additional conditions.
   <?php
}
?>


Hope this helps people looking.

Re: Form registration field with CCaps to determine access?

PostPosted: October 30th, 2011, 6:37 pm
by Bruce C
Thank-you for posting this in the forum, I certainly hope it helps someone else.

Also, for anyone working with this:

$fields is an array, and therefore cannot be echo'ed out. It must be shown using print_r.

Cheers!