PriMoThemes — now s2Member® (official notice)

This is now a very OLD forum system. It's in READ-ONLY mode.
All community interaction now occurs at WP Sharks™. See: new forums @ WP Sharks™

Access custom registratation fields through PHP and display

s2Member Plugin. A Membership plugin for WordPress®.

Access custom registratation fields through PHP and display

Postby talkdownunder » May 19th, 2011, 11:19 pm

Hi,

I want to have separate Member Directory on the website. While having 12 custom registration fields, only few of the member information will be displayed in that Member Directory (which will be accessible to all the members).

Since custom registration fields are not actual mysql table fields, what will be the best way of accessing them to filter and display required member information on the Member Directory?

Thanks heaps.
Nad
User avatar
talkdownunder
Registered User
Registered User
 
Posts: 19
Joined: May 17, 2011

Re: Access custom registratation fields through PHP and disp

Postby Cristián Lávaque » May 20th, 2011, 12:18 am

You can grab an s2Member Custom Field like this

Code: Select all
$field = get_user_field('my_custom_field', $user_id); 


Or get all the fields in an array

Code: Select all
$fields = get_user_option('s2member_custom_fields', $user_id); 
Cristián Lávaque http://s2member.net
Is s2Member working for you? Please rate it Image at WordPress.org. Thanks! :)
User avatar
Cristián Lávaque
Developer
Developer
 
Posts: 6836
Joined: December 22, 2010

Re: Access custom registratation fields through PHP and disp

Postby talkdownunder » May 20th, 2011, 12:32 am

Holy mother of Jesus!!! :shock:
Who the hell are you Cristian ???

Its that easy ???
Yeh it works.

I wanna hug you man.

This line works as piece of cake.
Code: Select all
echo $data['ID']." - ".get_user_field('first_name', $data['ID'])." ".get_user_field('last_name', $data['ID'])."<br/>";


How I get this one to work? please. How can I extract "first_name" out of it?
Code: Select all
$fields = get_user_option('s2member_custom_fields', $data['ID']);
User avatar
talkdownunder
Registered User
Registered User
 
Posts: 19
Joined: May 17, 2011

Re: Access custom registratation fields through PHP and disp

Postby Cristián Lávaque » May 20th, 2011, 2:09 am

:lol:

I'm glad that solved your problem!

Regarding your question, you see, the first name is not really an s2Member custom field, so it won't be in that array. That array will only have s2Member's custom fields you created that the user's profile has, you can print_r the array to see the contents.

Code: Select all
<pre><?php print_r($fields); ?></pre>


get_user_field is an s2Member function, doesn't come with WordPress, and will give you first_name, last_name and user_email although they aren't custom fields, to make it more convenient. Sorry about the confusion because of that.

Actually I think it's better that you use the array rather than query each custom field individually, with the array you do a single query for all the fields and then use the ones you need.

Actually, since you don't just need the custom fields but also other data about the user, it's even better if you use the WordPress function get_userdata, which also returns an array with first_name, last_name and much more, including s2Member's custom fields.

Code: Select all
$user_data = get_userdata($user_id);
print_r($user_data); 


The line of code you mention above would be like this

Code: Select all
echo $data['ID'] . ' - ' . $user_data['first_name'] . ' ' . $user_data['last_name'] . '<br/>';
  


An s2Member custom field would be here

Code: Select all
echo 'My custom field is ' . $user_data['wp_s2member_custom_fields']['my_custom_field'];
  


Now, you're using $data['ID'], how did you create the $data array? If you used get_userdata, then you already have it all in $data.
Cristián Lávaque http://s2member.net
Is s2Member working for you? Please rate it Image at WordPress.org. Thanks! :)
User avatar
Cristián Lávaque
Developer
Developer
 
Posts: 6836
Joined: December 22, 2010

Re: Access custom registratation fields through PHP and disp

Postby talkdownunder » May 23rd, 2011, 12:12 am

Hi Cristan,

I much appreciate your support and good manner in supporting users of s2Member.

The way I managed to get get_userdata() to work is bit different from what you mentioned though. I list it here if anyone interested.

This is how I've access db field using user_data
Code: Select all
$user_data->first_name


And s2Member custom field
Code: Select all
$user_data->wp_s2member_custom_fields['mobile']


Anyway thumbs up for you Cristan.


Cheers
Nad
User avatar
talkdownunder
Registered User
Registered User
 
Posts: 19
Joined: May 17, 2011

Re: Access custom registratation fields through PHP and disp

Postby Cristián Lávaque » May 23rd, 2011, 2:17 am

Cool. :)
Cristián Lávaque http://s2member.net
Is s2Member working for you? Please rate it Image at WordPress.org. Thanks! :)
User avatar
Cristián Lávaque
Developer
Developer
 
Posts: 6836
Joined: December 22, 2010

Re: Access custom registratation fields through PHP and disp

Postby Sim2K » October 12th, 2011, 9:24 pm

How can I get this working on the Authors page ? I'd like to show some Custom Fields on the Author page.
User avatar
Sim2K
Registered User
Registered User
 
Posts: 6
Joined: August 19, 2011

Re: Access custom registratation fields through PHP and disp

Postby Sim2K » October 13th, 2011, 8:55 am

These 2 worked for me on my Author page!

<?php echo get_user_field('Contact_Number', $author); ?>
<?php echo get_user_field('Contact_Number', get_the_author_meta( 'ID' )); ?>

"Contact_Number" is a Custom Field I made to hold a telephone number.
I made that in 'General Options', 'Registration/Profile Fields and Options' and then 'Add new Field'

Admin, please correct if wrong but this worked OK for me.
User avatar
Sim2K
Registered User
Registered User
 
Posts: 6
Joined: August 19, 2011


Return to s2Member Plugin

Who is online

Users browsing this forum: Exabot [Bot] and 2 guests

cron