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.

Get custom profile field values

Home Forums Community Forum Get custom profile field values

This topic contains 13 replies, has 3 voices. Last updated by  Cristián Lávaque 4 years, 9 months ago.

Topic Author Topic
Posted: Monday Apr 9th, 2012 at 10:51 am #10358
Simon Cooke
Username: Simon23

Augh, help

I have spent hours trawling the S2 support forums and Codex for an answer but my head is hurting a great deal now.

I am trying to create front-end user Profiles (not editable) read-only – so other members can view a members key profile data.

I am editing a WP theme template (author.php), adding custom php code to get profile data for a specific user (passed as a GET parameter – /?author=6)

I need to get s2member custom profile field data and print it into the page – some of the custom fields are multi-option checkboxes.

Can anyone give me the code to do this ?

Thanks

joe

List Of Topic Replies

Viewing 13 replies - 1 through 13 (of 13 total)
Author Replies
Author Replies
Posted: Monday Apr 9th, 2012 at 5:01 pm #10376
Simon Cooke
Username: Simon23

bump

Posted: Monday Apr 9th, 2012 at 5:44 pm #10389
Raam Dev
Username: Raam
Staff Member

Hi Simon,

Extracting s2Member Custom Profile Field data via PHP is possible, but it’s not going to be pretty. Please see this thread for more information.

Posted: Monday Apr 9th, 2012 at 6:17 pm #10392
Simon Cooke
Username: Simon23

Thanks Raam

I had already read that thread – its not really aimed at what I am trying to achieve

Or, to put it another way, it relates to a method of searching users for custom fields and I am not clever enough to work out how to extract from it the information I need to:

Print/echo data from user meta fields (using an author ID)

I have tried this:

(it breaks the page)

and this:

(it returns ‘Array’)

buyer_country_usa is a multi-option checkbox field

Cheers

Joe

Posted: Monday Apr 9th, 2012 at 6:20 pm #10393
Simon Cooke
Username: Simon23

oops

first code snippet:

$user_data = get_userdata($user_id);
echo $user_data['wp_lss2member_custom_fields']['buyer_country_usa'];

second code snippet:

echo get_user_field('buyer_country_usa', $theUserID);

ps – I have an unusual database table prefix

Posted: Monday Apr 9th, 2012 at 6:41 pm #10399
Raam Dev
Username: Raam
Staff Member

Hi Simon,

I understand the frustration in working with the Custom Registration field data within the database and we’re working to improve the way that information is stored in future versions.

As per our support policy, we unfortunately cannot assist with custom coding (please see s2Member® » Support Policy » Outside Scope). If you’re not comfortable working with PHP, we recommend hiring a PHP developer via jobs.wordpress.net.

Posted: Monday Apr 9th, 2012 at 6:53 pm #10401
Simon Cooke
Username: Simon23

I understand but if you keep user meta data in a serialised array you could help me to extract that data in a meaningful way – that’s after all, at least partly what the support forums are about…no ?

I have posted some custom code that has made an attempt to get the data I need but is clearly not the full solution.

What I am asking for is help completing what I have attempted to start myself. This is consistent with other posts where users have subsequently had a good level of support for their custom coding.

If you don’t want to help me can you ask Jason to post me an answer, as I am not aware that an answer to this specific question exists anywhere at this time in your support forums.

If it does, simply point me to it

Thanks

Posted: Tuesday Apr 10th, 2012 at 2:14 am #10430

Simon, this function I wrote some time ago may help you displaying the s2Member custom fields. http://www.primothemes.com/forums/viewtopic.php?p=16582#p16582

I hope it helps. :)

Posted: Tuesday Apr 10th, 2012 at 3:46 am #10437
Simon Cooke
Username: Simon23

That’s awesome, thanks Cristian.

Well what do you know:

A) the info already existed
B) you do help people to do things like this in the forums

cheers – I’ll post something back when I have a working solution

Posted: Tuesday Apr 10th, 2012 at 4:48 am #10439

Glad it helped. :)

Yeah, the custom fields can be a bit frustrating when you start using them in PHP, since they’re in a serialized array. We’re aware that it’s not optimum and we’ll have this solved soon with the coming major update of the plugin.

Posted: Tuesday Apr 10th, 2012 at 4:52 am #10440

By the way, I just remembered having seen a function in the codex that’d do something similar, and it has the same name as the function I posted last year. Jason told me he had added it to s2Member, but I had forgotten.

Please read about it here and play with it to see what the output is like:

http://www.s2member.com/codex/stable/s2member/api_functions/package-functions/#src_doc_get_s2member_custom_fields%28%29

Posted: Tuesday Apr 10th, 2012 at 5:13 am #10444
Simon Cooke
Username: Simon23

Cristian, is there a bug in this code – it causes my page to go blank when I include it like this (with a user ID)

function get_s2member_custom_fields($user_id = $theUserID) {
    $return = array();
    $user = get_user_option('s2member_custom_fields', $user_id);

    foreach ((array)json_decode($GLOBALS['WS_PLUGIN__']['s2member']['o']['custom_reg_fields'], true) as $field) {
        if (isset($user[$field['id']])) {
            $return[$field['id']]['label'] = $field['label'];

            if (empty($field['options']))
                $return[$field['id']]['value'] = $user[$field['id']];
            else {
                $field['options'] = strpos($field['options'], "\n") ? explode("\n", $field['options']) : (array)$field['options'];
                foreach ($field['options'] as $option) {
                    $option = explode('|', $option);
                    $options[$option[0]] = $option[1];
                }
                foreach ((array)$user[$field['id']] as $choice)
                    $return[$field['id']]['options'][$choice] = $options[$choice];
            }
        }
    }
    return $return;
}
  
Posted: Tuesday Apr 10th, 2012 at 7:03 am #10447
Simon Cooke
Username: Simon23

This is what I have in my author.php (gives me a blank page)

function get_s2member_custom_fields($user_id = $theUserID) {
    $return = array();
    $user = get_user_option('s2member_custom_fields', $user_id);

    foreach ((array)json_decode($GLOBALS['WS_PLUGIN__']['s2member']['o']['custom_reg_fields'], true) as $field) {
        if (isset($user[$field['id']])) {
            $return[$field['id']]['label'] = $field['label'];

            if (empty($field['options']))
                $return[$field['id']]['value'] = $user[$field['id']];
            else {
                $field['options'] = strpos($field['options'], "\n") ? explode("\n", $field['options']) : (array)$field['options'];
                foreach ($field['options'] as $option) {
                    $option = explode('|', $option);
                    $options[$option[0]] = $option[1];
                }
                foreach ((array)$user[$field['id']] as $choice)
                    $return[$field['id']]['options'][$choice] = $options[$choice];
            }
        }
    }
    return $return;
}
$s2_custom_fields = get_s2member_custom_fields($theUserID); 
echo $var['buyer_country_usa']['options'][0]['label'];
Posted: Tuesday Apr 10th, 2012 at 2:45 pm #10487

It’s probably a PHP error because of the existing function with the same name that I linked to in my last post. Try using the function already in s2Member or, if you prefer the one I wrote, give it unique name. That should do it.

Viewing 13 replies - 1 through 13 (of 13 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.