Community Support Forums — WordPress® ( Users Helping Users ) — 2011-07-31T22:41:35-05:00 http://www.primothemes.com/forums/feed.php?f=4&t=6546 2011-07-31T22:41:35-05:00 http://www.primothemes.com/forums/viewtopic.php?t=6546&p=29549#p29549 <![CDATA[Re: Getting Custom Field values on the frontend]]>
However, as I've just basic knowledge of php, if I intend to implement your latest hack here above, where should I put it? Is this considered to be inserted in the template file (i.e. single.php) or do I need to hack into the function.php too?

Moreover, does ['id'] means that I have to enter my own custom field names here instead of id? For example, "customer_phone", "customer_zipcode", etc.?

Appreciate your concern.

P.S. For your information and just to give an example regarding getting values from custom fields, Advanced custom fields plugin has suggested different hacks regarding different fields. (http://plugins.elliotcondon.com/advanced-custom-fields/field-types/)

Statistics: Posted by ZebEl — July 31st, 2011, 10:41 pm


]]>
2011-06-02T19:32:42-05:00 http://www.primothemes.com/forums/viewtopic.php?t=6546&p=16582#p16582 <![CDATA[Re: Getting Custom Field values on the frontend]]>
I'm glad you found the function good enough. Here's a slightly improved version for you:

Code:
function get_s2member_custom_fields($user_id = '') {
    $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;
}
  


I guess it's fine to have the option's value as the key, since it's more useful to get a particular label. I realized that the way it was, it's uncomfortable to search the values to know which option we were looking for. This way, though, you can use the value as the key to know the label or search for the label to find its value.

Code:
$s2_custom_fields = get_s2member_custom_fields($user_id); 

$option_label 
= $s2_custom_fields[$field_name]['options'][$option_value];

$option_value = array_search($option_label, $s2_custom_fields[$field_name]['options']);

foreach ($s2_custom_fields[$field_name]['options'] as $option_value => $option_label) {
    // ...
} 


Regarding the RC, exactly which one is it? There were two last week. Could you post a screenshot of the missing radio option?

Statistics: Posted by Cristián Lávaque — June 2nd, 2011, 7:32 pm


]]>
2011-06-02T16:19:17-05:00 http://www.primothemes.com/forums/viewtopic.php?t=6546&p=16576#p16576 <![CDATA[Re: Getting Custom Field values on the frontend]]>
Also I did make a mistake there I just copied and pasted the $user_id =1 where it should have just been a single value being passed in.

Finally, this should probably be in a different thread but related.. I was doing some testing on 1.6 RC and found that radio option was missing! Maybe it's fixed now..

thanks
T

Statistics: Posted by totomobile — June 2nd, 2011, 4:19 pm


]]>
2011-06-02T13:16:38-05:00 http://www.primothemes.com/forums/viewtopic.php?t=6546&p=16558#p16558 <![CDATA[Re: Getting Custom Field values on the frontend]]>
I know the output may seem weird, but I actually thought a lot about it and how it'd be used.

I first did it array(field_label => field_value) or array(field_label => array(option_value => option_label)). This had a few problems because I had as the array keys data we want to use, which made it more uncomfortable and inefficient using array functions. The fields array didn't mention the field's name anywhere, only the label and value, so it was hard to use it. If you didn't know the option's value, then getting a specific label was impractical.

The way it is now it seems odd, but I think is more practical to use. Options has to be an array because fields with options can have multiple choices selected, not just one.

How do you find it indirect and how would you improve it? I'm very interested in your opinion, since you're using it.

By the way, you just enter the user's ID without defining a var in the function call:

Code:
$s2_custom_fields = get_s2member_custom_fields(1); 

Statistics: Posted by Cristián Lávaque — June 2nd, 2011, 1:16 pm


]]>
2011-06-02T12:37:28-05:00 http://www.primothemes.com/forums/viewtopic.php?t=6546&p=16556#p16556 <![CDATA[Re: Getting Custom Field values on the frontend]]>
Code:
$var get_s2member_custom_fields($user_id 1);
echo 
$var['user_preference']['options'][0]['label']; 


I think you've done enough to help though, anyone with basic coding skills can use or customize what you've put together. Perhaps for the next version of s2 you guys can have this as part of the core code as it'd be very useful!

thanks
T

Statistics: Posted by totomobile — June 2nd, 2011, 12:37 pm


]]>
2011-06-01T21:26:43-05:00 http://www.primothemes.com/forums/viewtopic.php?t=6546&p=16531#p16531 <![CDATA[Re: Getting Custom Field values on the frontend]]> Statistics: Posted by Cristián Lávaque — June 1st, 2011, 9:26 pm


]]>
2011-05-27T18:51:36-05:00 http://www.primothemes.com/forums/viewtopic.php?t=6546&p=16104#p16104 <![CDATA[Re: Getting Custom Field values on the frontend]]>
Code:
function get_s2member_custom_fields($user_id = '') {
    $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'][] = array (
                        'label' => $options[$choice],
                        'value' => $choice,
                    );
            }
        }
    }
    return $return;
}
  


Let me know if you find any errors, I haven't really tested it much. :)

Statistics: Posted by Cristián Lávaque — May 27th, 2011, 6:51 pm


]]>
2011-05-27T12:51:57-05:00 http://www.primothemes.com/forums/viewtopic.php?t=6546&p=16083#p16083 <![CDATA[Re: Getting Custom Field values on the frontend]]>
As a test case:

Array ( [type] => radios [label] => Test Radio [id] => test_radio [required] => no [options] => value1|This is the label for value 1 value2|This is the label for value 2

So while the get_s2member_custom_fields() function that Cristian wrote will work fine for textareas, inputs, and checkboxes ( I tested these), it won't work for radio buttons, and perhaps dropdowns as they have the value|label setup instead of just value. So if I want to get the value(radio label) on the frontend, I have to try to parse out the "|" chars and do some sanitizing etc.

It's more of a nice to have, but in the next version of s2, if we could have a built in function which takes care of all this logic, that would be great.

thanks
T

Statistics: Posted by totomobile — May 27th, 2011, 12:51 pm


]]>
2011-05-26T21:13:02-05:00 http://www.primothemes.com/forums/viewtopic.php?t=6546&p=16027#p16027 <![CDATA[Re: Getting Custom Field values on the frontend]]>
For the benefit of other readers.
By default, s2Member makes some of this possible without any hacks required.

Code:
<?php
echo get_user_field
("street_address"); // Value of Custom Field with ID "street_address", for the current User.
?>

Or, if you're searching for details about a specific User:
Code:
<?php
$user_id 
= 123;
echo get_user_field("street_address", $user_id); // Value of Custom Field with ID "street_address".
?>


get_user_field() is a function that comes with s2Member.

Statistics: Posted by Jason Caldwell — May 26th, 2011, 9:13 pm


]]>
2011-05-26T21:00:16-05:00 http://www.primothemes.com/forums/viewtopic.php?t=6546&p=16026#p16026 <![CDATA[Re: Getting Custom Field values on the frontend]]>
Code:
function get_s2member_custom_fields($user_id = '') {
    $values = get_user_option('s2member_custom_fields', $user_id);
    foreach ((array)json_decode($GLOBALS['WS_PLUGIN__']['s2member']['o']['custom_reg_fields'], true) as $field)
        $custom_fields[$field['id']] = array(
            'label' => $field['label'],
            'value' => isset($values[$field['id']]) ? $values[$field['id']] : '',
        );
    return $custom_fields;
}
 


Toto, since we're getting a value, I thought we may as well get them all, the way these arrays are and the WP function works, it's about the same work for the script, so it's better to just get everything into an array in one go and then use what you need.

You can use it like this

Code:
$custom_fields = get_s2member_custom_fields();
echo 'My ', $custom_fields['favorite_color']['label'], ' is ', $custom_fields['favorite_color']['value'], '.';
  

Statistics: Posted by Cristián Lávaque — May 26th, 2011, 9:00 pm


]]>
2011-05-26T20:50:49-05:00 http://www.primothemes.com/forums/viewtopic.php?t=6546&p=16025#p16025 <![CDATA[Re: Getting Custom Field values on the frontend]]>

Statistics: Posted by Cristián Lávaque — May 26th, 2011, 8:50 pm


]]>
2011-05-26T20:48:20-05:00 http://www.primothemes.com/forums/viewtopic.php?t=6546&p=16024#p16024 <![CDATA[Re: Getting Custom Field values on the frontend]]> Statistics: Posted by Jason Caldwell — May 26th, 2011, 8:48 pm


]]>
2011-05-26T20:42:42-05:00 http://www.primothemes.com/forums/viewtopic.php?t=6546&p=16023#p16023 <![CDATA[Re: Getting Custom Field values on the frontend]]>
Code:
function get_s2member_custom_fields($user_id = '') {
    $custom_fields = array();
    $values = get_user_option('s2member_custom_fields', $user_id);
    foreach ((array)json_decode($GLOBALS['WS_PLUGIN__']['s2member']['o']['custom_reg_fields'], true) as $field)
        $custom_fields[$field['label']] = isset($values[$field['id']]) ? $values[$field['id']] : '';
    return $custom_fields;
}
 


Do you see anything that can be improved in it, Jason? More efficient, versatile, or to prevent an error?

Statistics: Posted by Cristián Lávaque — May 26th, 2011, 8:42 pm


]]>
2011-05-26T16:45:52-05:00 http://www.primothemes.com/forums/viewtopic.php?t=6546&p=16011#p16011 <![CDATA[Re: Getting Custom Field values on the frontend]]>
s2member_get_custom_field('favorite_color', $memberID);

which would return "Yellow" (which is the label for "yellow"). I hope this makes more sense now!

Statistics: Posted by totomobile — May 26th, 2011, 4:45 pm


]]>
2011-05-26T16:10:37-05:00 http://www.primothemes.com/forums/viewtopic.php?t=6546&p=15998#p15998 <![CDATA[Re: Getting Custom Field values on the frontend]]> Got it. Thanks guys.
@TODO: Custom Fields API function that does more than just collect value.

For the benefit of other readers:
Until then, please see the example above.

Statistics: Posted by Jason Caldwell — May 26th, 2011, 4:10 pm


]]>
2011-05-26T15:40:18-05:00 http://www.primothemes.com/forums/viewtopic.php?t=6546&p=15992#p15992 <![CDATA[Re: Getting Custom Field values on the frontend]]>
Code:
s2member_get_custom_field('favorite_color'); 
// outputs array('Favorite Color' => 'yellow') 

Statistics: Posted by Cristián Lávaque — May 26th, 2011, 3:40 pm


]]>
2011-05-26T12:19:31-05:00 http://www.primothemes.com/forums/viewtopic.php?t=6546&p=15980#p15980 <![CDATA[Re: Getting Custom Field values on the frontend]]>
s2member_get_custom_field('custom_gender');

Which would return "Male" if the customer had selected that (radio), or if it was a textbox it would just return the text. So just a way of grabbing custom fields on the frontend, thanks!!

Statistics: Posted by totomobile — May 26th, 2011, 12:19 pm


]]>
2011-05-25T19:34:52-05:00 http://www.primothemes.com/forums/viewtopic.php?t=6546&p=15924#p15924 <![CDATA[Re: Getting Custom Field values on the frontend]]> Thanks for the follow-up.
Just to confirm, you want something like:
Code:
s2member_get_custom_field($value) 

Returns Field Label? Like in the example above?

Statistics: Posted by Jason Caldwell — May 25th, 2011, 7:34 pm


]]>
2011-05-25T12:40:33-05:00 http://www.primothemes.com/forums/viewtopic.php?t=6546&p=15894#p15894 <![CDATA[Re: Getting Custom Field values on the frontend]]>
I saw this was stored in the [options] array inside the big array and I made an attempt to modify your function, but the way the data is returned it needs too much cleaning and verifying to be used in production code so I'm going to go about it an easier way (just caps the first letter of the value), but in future versions it would be nice to have this function available, aka s2_member_get_custom_field($value);

thanks
T

Statistics: Posted by totomobile — May 25th, 2011, 12:40 pm


]]>
2011-05-25T08:38:25-05:00 http://www.primothemes.com/forums/viewtopic.php?t=6546&p=15852#p15852 <![CDATA[Re: Getting Custom Field values on the frontend]]> Thanks for the excellent question.

That's correct, by default you only get the field IDs in the associative array, and the values for each Custom Field key in the array. If you want the actual configuration options for each of these Custom Registration Fields, you can pull that from the options array, and match them up with the Unique ID ( i.e. the [id] key ).

Code:
<?php
$fields_configured 
= json_decode($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["custom_reg_fields"], true);
print_r($fields_configured);
?>

Code:
Array
(
[0] => Array
(
[section] => no
[sectitle] =>
[id] => country_code
[label] => Choose Country
[type] => text
[deflt] =>
[options] =>
[expected] =>
[required] => yes
[levels] => all
[editable] => yes
[classes] =>
[styles] =>
[attrs] =>
)

[1] => Array
(
[section] => no
[sectitle] =>
[id] => street_address
[label] => Street Address
[type] => text
[deflt] =>
[options] =>
[expected] =>
[required] => yes
[levels] => all
[editable] => yes
[classes] =>
[styles] =>
[attrs] =>
)

)


For example.
Code:
<?php
$user_id 
= 123; /* A specific User ID. */
$street_address = get_user_field ("street_address", $user_id); /* Value of Custom Field: street_address */
$street_address_label = my_s2_custom_field_label ("street_address"); /* Might return: Street Address */
/*
Custom utility function to pull Labels.
*/
function my_s2_custom_field_label ($field_id)
    {
        foreach ((array)json_decode ($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["custom_reg_fields"], true) as $field_configured)
            {
                if ($field_configured["id"] == $field_id)
                    {
                        return $field_configured["label"];
                    }
            }
    }
?>

Statistics: Posted by Jason Caldwell — May 25th, 2011, 8:38 am


]]>
2011-05-24T16:10:52-05:00 http://www.primothemes.com/forums/viewtopic.php?t=6546&p=15792#p15792 <![CDATA[Getting Custom Field values on the frontend]]>
Currently I am using

$custom_field = get_usermeta( bp_loggedin_user_id(), 'wp_s2member_custom_fields' );

but this only gets me the value and not the label of the custom field.

thanks
T

Statistics: Posted by totomobile — May 24th, 2011, 4:10 pm


]]>