Support Forum

Custom Profile Field

44 Answers

New Answer

MP Mr Papa
Mr Papa
Member

sp_CustomProfileFieldsDisplay(‘Zip Code’, $spThisPost->user_id);

assuming the custom profile field is name Zip Code

SP SPQC
SPQC
Member

Ok got it but this does not output the Custom field name so I tried to add it by looking at other lines around and tried this :

 

__sp(‘Zip Code :<br />’);

sp_CustomProfileFieldsDisplay(‘Zip Code’, $spThisPost->user_id);

 

But Zip Code text does not show up.  I will also ask right away that I want to center all this, just like the Member since line and I would guess ‘spCenter’ is needed somwhere?

 

Finally,  I tried looking at it with Firebug because the text is too big and had hope to find how to set the size but I can’t target it like other lines, I only get a big box when I hover it.

MP Mr Papa
Mr Papa
Member

of course all the users have to fill in the zip code on their sp profile form…

you can simply do:

echo ‘<div class=”spCenter”>Zip Code:<br />’;

of course, div could be p, span or any other html element…

and make sure Zip Code including case matches the name you entered on the custom profile fields admin panel…

SP SPQC
SPQC
Member

Finally got it to work!  And now I’m trying to not show it if it’s empty and I’m almost there but there is something wrong.

 

if (sp_CustomProfileFieldsDisplay(‘Code ami ‘, $spThisPost->user_id)) {
                                echo ‘<p style=”font-size: 80%;” class=”spCenter”>Zip Code : ‘;
                                sp_CustomProfileFieldsDisplay(‘Zip Code’, $spThisPost->user_id);
                                echo ‘</p>’;
                                }

Before adding the if statement, the echo was showing, now it does not.

UN
Unknown
Member

I’ve got something working, though I’m very much a php novice so someone else may have a more elegant solution to suggest. Here’s my code:

 

#Start buffer to capture output of sp_CustomProfileFieldsDisplay function

ob_start(); 

#execute the function

sp_CustomProfileFieldsDisplay(‘Zip Code’, $spThisPost->user_id); 

#assign the function output to a variable

$zip_code = ob_get_contents(); 

#dump the buffer

ob_end_clean(); 

 

# Use isset to check if the contents of the variable are blank; if not echo variable, else echo error message (or anything else)

if (isset($zip_code[‘Zip Code’])) {

echo ‘

‘;

echo $zip_code;

echo ”;

}

else {

echo “No zip code found”;

}

 

I’ve been trying to do something very similar to SPQC, so many thanks to the helpful insight in this thread; its helped enormously. 

YS Yellow Swordfish
Yellow Swordfish
Member

This is all good but – a couple of points.

There should be no need whatsoever to use the output buffer. You should just be able to perform the $zip_code variable assignment without that.

Rather than using isset() I would recommend using empty(). More reliable as you have created the $zip_code variable so it does exist.

SP SPQC
SPQC
Member

Hey VikingBrent, thanks for the help!  Nice it serve others too.

 

Following you suggestion I came to this :

 

$ZipCode=sp_CustomProfileFieldsDisplay(‘Code ami ‘, $spThisPost->user_id);
                            if (!empty($ZipCode[‘Zip Code’])) {
                                echo ‘<p style=”font-size: 80%;” class=”spCenter”>Zip Code : $ZipCode</p>’;
                                }

 

But again it looks the the echo does not work BUT I see the ZipCode when not empty.  So it seems the if is executed but the echo is not except that the variable $ZipCode is echoed? 

YS Yellow Swordfish
Yellow Swordfish
Member

That’s because that is incorrect use of the variable.

If you want to embed a variable in a string of text like that then the whole string has to be in double quotes not single.

Use:

Zip Code : ‘ . $ZipCode . ‘

instead is easiest = or – swap all the quotes around.

VI VikingBrent
VikingBrent
Member
Thank you Yellow Swordfish for the clarification and suggestion.
However, I’m going to have to retract my previous post, as the code I shared is most certainly not working.

 It appears to function as I saw the correct value stored in my custom field, displayed in the appropriate location in the spUserSection div. However, upon further scrutiny, I discovered that the custom field value was only being echoed in the user’s first post in the topic. If the user has more than one reply, the custom field value is missing from all but the first entry.

 I discovered that the variable is apparently not being defined at all. For troubleshooting purposes, I’ve stripped the code down to its barest form. Here’s what I’m working with (these are the particulars from my own work, which is why there’s no reference to the $ZipCode variable from prior posts):

 $SteamID=sp_CustomProfileFieldsDisplay(‘Steam ID’, $spThisPost->user_id);

echo ‘Steam: ‘;
echo $SteamID;

 What appears to be happening is the $SteamID from line 1 is being ignored entirely and the output that I’m seeing (in my first post in the topic) is coming from the sp_CustomProfileFieldsDisplay(‘Steam ID’, $spThisPost->user_id); function.

 The first echo which outputs the string “Steam:” is working as expected and showing up on every reply. But the second echo outputs nothing. So here is the result:

 1ST POST

custom-field-valueSteam:

 2ND POPST

Steam:

 I double checked the code in a clean install of the css-only theme to verify that this wasn’t the result of something I botched in my custom theme and the results are the same. As I stated before, I am a PHP novice so perhaps this is a simple syntax mistake.

 To check myself, I copied SPQC’s code from reply #22, changing the improperly embedded variable, and substituted my own custom field name. Again, I stripped the ‘if’ statement for the sake of simplicity and got the same result: The output of sp_CustomProfileFieldsDisplay(‘Steam ID’, $spThisPost->user_id); is displayed on the first post only.

 This code is being placed into the spTopicView.php file around line 112, before the start of the user-identities section.

 Any insight is most welcome.

 

SP SPQC
SPQC
Member

I see VikingBrent have replied but still waiting moderation.  You should subscribe!  Maybe you have the answer?  wink

 

Ok the echo don’t work at all and the simple fact of having this line :

 

$ZipCode = sp_CustomProfileFieldsDisplay(‘Zip Code’, $spThisPost->user_id);

 

output the zip code…  I tried a few things but I’m out of ideas.

MP Mr Papa
Mr Papa
Member

where are you guys putting this in your spTopicView template file?

also the template tag echos the output itself, so you cannot assign it to a variable…  nothing is returned…

sp_CustomProfileFieldsDisplay(‘Zip Code’, $spThisPost->user_id);

will just output the zip code stored for the user…

if you precede that with

echo ‘zip: ‘;

you will end up with something like

zip: 54545

I put them right after:

sp_PostIndexUserName(‘tagClass=spPostUserName spCenter’);

in my topic view to verify it works fine…

SP SPQC
SPQC
Member

Mr Papa said

also the template tag echos the output itself, so you cannot assign it to a variable…  nothing is returned…

We were trying to see how to achieve that if user filled Zip Code, then display Zip Code: xxxx, if not then show nothing at all.

MP Mr Papa
Mr Papa
Member

okay, a little secret… eh, not really…  but there is an object that contains most of the data in this case being inside the post class with the $spThisPost user…

so in this case, you dont really have to use the template tag… its already in the user class…  but we try to generically help…

so for this exact case, and not globally, just do:

if (!empty($spThisPostUser->zipcode)) echo ‘zip: ‘.$spThisPostUser->zipcode;

you obviously could have replace the $spThisPostUser->zipcode; part with a call to the template tag… but since in our post loop, its already been prefilled…

SP SPQC
SPQC
Member

Mr Papa said

so for this exact case, and not globally, just do:
if (!empty($spThisPostUser->zipcode)) echo ‘zip: ‘.$spThisPostUser->zipcode;

hmmm..  I don’t understand.  Dunno if my english is the problem.  Where is the call to properly target the desired CustumProfileField we want to display?

MP Mr Papa
Mr Papa
Member

lol. not your English, but my knowledge of SP…

i just happen to know that whenever the user class object is loaded (in this case $spThisPostUser), we also load all user meta info including any custom profile fields into the user object…  so

if (!empty($spThisPostUser->zipcode)) echo ‘zip: ‘.$spThisPostUser->zipcode;

is the same as:

if (!empty($spThisPostUser->zipcode)) {
    echo ‘zip: ‘;
    sp_CustomProfileFieldsDisplay(‘Zip Code’, $spThisPost->user_id);
}

why? because as I said, we load the user object with all the info about that user… and the when you generate a slug from your custom field ‘Zip Code’, it becomse zipcode…  so I can just grab it from the user object instead of using the template tag…

if you were ever to look at the object $spThisPostUser, you would see lots of data about that user…